Jump to content

[SOLVED] Displaying table results in multi column table


Recommended Posts

hey, im sure this is prety simple, i have just never done it before so I was hoping someone can help me out.

i want to display the results of my database table in a html table that has three columns and if there are not enough results to complete the final row it shouw echo &nbsp like this:

 

--------------------------------

|  $val1  |    $val2    |  $val3  |

--------------------------------

|  $val4  |    $val5    |  $val6  |

--------------------------------

|  $val7  |    &nbsp  |  &nbsp |

--------------------------------

 

I hope someone has any ideas that could help me!

some like this

<?php
$count = 0;
echo '<table border="3">';
for ($i=0;$i<10;$i++){ //start loop
if ($count == 0) echo '<tr>';
$count++;
echo '<td>',$i,'</td>';// echo some data in td tag
if ($count == 3){
	echo '</tr>';
	$count=0;
}
} //end loop
if ($count>0){
for (;$count<3;$count++) echo '<td> </td>';
echo '</tr>';
}
echo '</table>';
?>

Hey thanks for the quick replies! i really appreciate it

 

i user a mysql query to get the results of the databse table and thanks to sasa i can echo the results in a table with 3 columns and x number of rows.

 

the problem is if the number of results is not divisible by 3 i will have some cells empty. how can i make it so that the script counts the number of results, checks to see if it is divisible by 3, and if not then insert &nbsp to complete the table. for example if there are 4 results i will need the first row with the first 3 results and the second row with the 4th result and two &nbsp

 

$query = "SELECT * FROM tbl_products";
$result = mysql_query($query)
	or die ("Couldn't execute query");
$counter = 0;
while ($row = mysql_fetch_array($result))
{

extract($row);

for ($i=0;$i<10;$i++){ //start loop
if ($count == 0) echo '<tr>';
$count++;
	echo '<td>',$product_name,'</td>';// echo some data in td tag
	if ($count == 3){
	echo '</tr>';
$count=0;
}
} //end loop

sorry here is my updated code:

 

                        <?php
$query = "SELECT * FROM tbl_products";
$result = mysql_query($query)
	or die ("Couldn't execute query");
$row = mysql_fetch_array($result);	
$num_rows = mysql_num_rows($result);


for ($i=0;$i<$num_rows;$i++){ //start loop
if ($count == 0) echo '<tr>';

extract($row[$i]);

$count++;
	echo '<td>',$product_name,'</td>';// echo some data in td tag
	if ($count == 3){
	echo '</tr>';
$count=0;
}
} //end loop


           					
				?>

have you looked at this thread in the FAQ/Code Snippet Repository board?

clean up table part must be

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

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.