Jump to content

Can these be echoed out in a table?


hoponhiggo

Recommended Posts

Hi

 

I am learning both PHP an HTML and was wondering if i could ammend my code to display my 'member cards' in a table - two columns, unlimited rows?

 

The code in question is:

 

{ 
echo "<div id='memcontainer'>
<div id='mempic'>$img</div>
<div id='memname'><a href='members.php?user=$user[username]'>$user[username]</a></div></div><br>
"; 
}

 

each record is contained in the 'memcontainer' div

Link to comment
Share on other sites

I recommend the following:

 

1) Maybe, you should first learn how to make a table in html. (as suggested by Maq)

Ones you made a good table that looks nice and is valid. Think about what parts of the table can stay the same. and which ones increase. Like you said you want an 'infinite' amount of rows. (hint hint)

 

2) Ones you've done that, learn how to use loops in php (there is a nice tutorial here at phpfreaks under tutorials). Pretty soon you will find out which loop is the correct one to use. try it test it and play with it.

 

3) After that combine the logic of php inside your table. There is no good reason to try and combine them from the start if you can't play with them apart.

 

This way you can make sure your logic works, your html works and all you need to do is combine them.

 

As an example with a an unordered list. I could do it with a table but you wont learn anything from it.

 

<!-- 1) a normal list -->
<ul>
    <li>A</li>
    <li>B</li>
    <li>C</li>
    <li>D</li>
</ul>

<!-- 2) some php loop -->
<?php
//first an array
$myarray = array('A','B','C','E');

foreach($myarray as $value){
            echo $value;
        }


//  3) apply php logic to the html
echo '<ul>'; //begin
        foreach($myarray as $value){
            echo '<li>'.$value.'</li>';
        }
echo '</ul>'; //end

 

 

Link to comment
Share on other sites

I already have a lot of the php and bits of html & css sorted. See image....

 

This loops out the records down the page.

 

What i want is for them to be side by side. Im not even sure using a table would be the best way to do this?

 

[attachment deleted by admin]

Link to comment
Share on other sites

Yes, for tabular data you should use tables.  The format should look like:

 

echo "</pre>
<table>";
//while loop
{ 
   echo "";
   echo "$img";
   echo "{$user['username']}";
   echo "";
}
echo "</ta

 

For each iteration create a single row (tr) and 2 columns in each one (td).

Link to comment
Share on other sites

I think he actualy wants the full contents of <div id='memcontainer'> to apear in two by x format, rather than 1 by x format as opposed to using a table in place of the memcontainer object.

 

On another note - if you are calling the same <div> more than once on a given page, you really should make it a class not an id - id's are designed to be unique and as such should only appear once on a page.

Link to comment
Share on other sites

Hi Guys

 

I have tried and tried to make this work with my code, but it just doesnt work. Could somebody please take a look at my code and advise?

 

<table cellspacing="2" cellpadding="2">
    <?php
//gets all the members from the database
$getusers = mysql_query("SELECT * FROM `users` ORDER BY username ASC") or die(mysql_error());
if($getusers && mysql_num_rows($result) > 0)
{
    $i = 0;
    $max_columns = 2;
//loops there name out
while (

$user = mysql_fetch_array($getusers) )
{
$username = $user['username'];

// make the variables easy to deal with
       extract($row);

       // open row if counter is zero
       if($i == 0)
   
   if($user != "" && $user != null)
          echo "<td>$product</td>";
	  
	  // increment counter - if counter = max columns, reset counter and close row
       if(++$i == $max_columns) 
       {
           echo "</tr>";
           $i=0;

//to display image from source
$dir = "prof_pics";

$sql = "SELECT prof_pic FROM users WHERE username = '$username'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 0) die("Username not found in database.");

$row = mysql_fetch_array($res);
$pic="$dir/".$row['prof_pic'];
$img="<img src=\"$pic\" width=\"88\" height=\"88\" align=\"center\"><br>";

{ 
echo "<div id='memcontainer'>
<div id='mempic'>$img</div>
<div id='memname'><a href='members.php?user=$user[username]'>$user[username]</a></div><br>
<div class='addfriend'><a style='text-decoration:none' href='friendrequest.php?user=$user[username]'><font color='red'>Add as Friend</a></div>
</div><br>

"; 
}
}
}
}
}
// clean up table - makes your code valid!
if($i < $max_columns)
{
    for($j=$i; $j<$max_columns;$j++)
        echo "<td> </td>";
}
?>
</tr>
</table>
echo "<center>";
?>

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.