Jump to content

[SOLVED] Output database entries in a table


couplebb

Recommended Posts

I'm trying to display the entries in a table of two columns, one entry per table cell - in other words, two entries per row, number of rows based on number of entries in the database. But it still doesn't seem to work. It displays one entry per row... I don't know where the problem is. Here's my code:

 

echo "<table width='490' border='0' cellspacing='10' cellpadding='10'>";

$num = mysql_num_rows($result);

$numrows = round($num/2);

while ($row=mysql_fetch_array($result))

{

extract($row);

$count=1;

while ($count<$numrows){

echo "<tr>";

$count2=0;

while($count2<1){

echo "<td width='245' align='center' valign='top'><span class='content2 style2'>";

echo "<img src='$picaddy' alt='$firstname $lastname'><br>$lastname<br>$firstname</span></td>";

$count2=$count2+1;

}

$count=$count+1;

echo "</tr>";

}

}

echo "</table>";

 

Link to comment
Share on other sites

Try this. You don't need to know the number of rows, "while ($row=mysql_fetch_array($result))" will loop through every row selected. Unless there is another part that I'm not seeing. You only have 1 column in your table, if you want to have a multi column table you will have to throw in some <td></td> tags.

 

echo "<table width='490' border='0' cellspacing='10' cellpadding='10'>";
$query = mysql_query(YOUR QUERY);
while ($array = mysql_fetch_array($query)){
   $firstname = $array["firstname"];
   $lastname = $array["lastname"];
  echo("<tr><td width='245' align='center' valign='top' class='content2 style2'><img src='".$picaddy."' alt='".$firstname." ".$lastname."'>".$lastname." ".$firstname."</td></tr>");
} 
echo "</table>";

Link to comment
Share on other sites

try

echo "<table width='490' border='0' cellspacing='10' cellpadding='10'>";
//$num = mysql_num_rows($result);
//$numrows = round($num/2);
while ($row=mysql_fetch_array($result))
{
extract($row);
//$count=1;
//while ($count<$numrows){
echo "<tr>";
//$count2=0;
//while($count2<1){
echo "<td width='245' align='center' valign='top'><span class='content2 style2'>";
echo "<img src='$picaddy' alt='$firstname $lastname'> $lastname $firstname</span></td>";
//$count2=$count2+1;
if ($row = mysql_fetch_array($result)){
	extract($row);
	echo "<td width='245' align='center' valign='top'><span class='content2 style2'>";
	echo "<img src='$picaddy' alt='$firstname $lastname'> $lastname $firstname</span></td>";
} else echo '<td> </td>';
//}
//$count=$count+1;
echo "</tr>";
//}
}
echo "</table>";

Link to comment
Share on other sites

Here's a more general example. P.S. I like using for loops when I can. I guess I'm too structured, lol.

 

function printTable($mysql_result)
{
   echo "<table>\n";
   $fields = mysql_num_fields($mysql_result);
   $rows = mysql_num_rows($mysql_result);
   for( $i = 0; $i < $rows; ++$i )
   {
      echo "<tr>\n";
      $row = mysql_fetch_row($mysql_result);
      for( $j = 0; $j < $fields; ++$j )
      {
         echo "<td>" . $row[$j] . "</td>\n";
      }
      echo "</tr>\n";
   }
   echo "</table>\n";
}

 

So then usage would be something like:

 

$result = mysql_query("SELECT * FROM table");
printTable($result);

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.