PHP Echo Over 3 Columns and 2 Rows

Soldato
Joined
30 Nov 2005
Posts
3,084
Location
London
Im trying to do the following in PHP with a MySQL database:

tableao2.jpg


However, I just can't get it to Echo the values from the database into the next column and so on.

Basically the first value's name is echoed above a row that echo's it's description.

Hope that makes some kind of sense.
 
sorry, it doesn't make any sense. post what you already have and someone might be able to see what you're trying to do
 
Each MySQL row has Row Name & Row Desc.

Then bascialy each of the two Values from the MySQL Row, is echoed from left to right in a Table like the one above.

It's like this code I found but can't seem to change it to do the above:

Code:
$x = 1; 
echo '<table><tr>'; 
while($row = mysql_fetch_object($result)){ 
echo "<td>$row->somevar</td>"; 
if($x%5 == 0){ 
echo '</tr><tr>'; 
} 
$x++; 
} 
echo '</tr></table>';
 
Last edited:
what you're trying to do is missing something for me - it'll be incredibly difficult to look at if you're manually restricting the amount of columns for each db row returned as your data may overlap. the following will go through your resultset and give you a row for the column name and a row underneath for the value. it makes more sense to me to do it like that. hth

Code:
echo '<table>'; 
while($row = mysql_fetch_object($result)){
	$headrow = '';
	$valrow = '';
  foreach ($result as $k => $v) {
  	$headrow .= sprintf('<td>%s</td>',$k);
  	$valrow .= sprintf('<td>%s</td>',$v);
  }
  printf('<tr>%s</tr>',$headrow);
  printf('<tr>%s</tr>',$valrow);
} 
echo '</table>';
 
Edit: Fixed change $result for $row.

However, the table comes out not as expected.

The values are coming out next to each other rather than ontop.

This is what it looks like, when I'm trying to get it like the first image at the top of this page:

table2gy9.jpg
 
Last edited:
Code:
<?
$columnsPRow = 4;

echo "<table cellpadding=0 cellspacing=0 border=1>";
while ($row=mysql_fetch_assoc($query))
{
  if ($i % $columsPRow == 0) { echo "<tr>"; }
  echo "<td>";
  echo "<table cellpadding=0 cellspacing=0 border=1>";
  echo "<tr>";
  echo "<td>".$row['name']."</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>".$row['description']."</td>";
  echo "</tr>";
  echo "</table>";
  echo "</td>";
  if ($i % $columnsPRow == $columnsPRow - 1) { echo "</tr>"; }
  $i++;
}
if ($i %columnsPRow != 0)
{
  while ($i++ % $columnsPRow != 0) 
  {
      echo '<td>&nbsp;</td>';
  }
  echo "</tr>";
}
echo "</table>";
?>
 
Last edited:
Back
Top Bottom