Jump to content

db data into a table


squiblo

Recommended Posts

I want the data from this query...

 

$get_friend_id = mysql_query("SELECT * FROM page_zebra WHERE user_id='$user_id' AND friend=1 LIMIT 0,9");
while($row = mysql_fetch_assoc($get_friend_id)){
$friend_id = $row['page_zebra_id'];

 

to be displayed in a 3x3 table

 

there will not always be exactly 9 pieces of data sometimes less, sometimes more.

 

How is this possible?

 

 

Link to comment
https://forums.phpfreaks.com/topic/190852-db-data-into-a-table/
Share on other sites

well there won't be more with "limit 0,9" in there.

 

when i've done tables like this I just keep a count of whatever row im on. mod your count every loop through by the number of columns you want. if it equals zero close your row tag and add a new row tag.

 

when the result set is done. check your count again and fill in any blanks until count mod num_columns equals zero then close the row off.

 

there is also the first case you need to check for when you only a tr tag.

another idea, how could i put the 9 pieces of data into 9 different variables or an array like

 

$friend_id[0] = ???;
$friend_id[1] = ???;
$friend_id[2] = ???;
$friend_id[3] = ???;
$friend_id[4] = ???;
$friend_id[5] = ???;
$friend_id[6] = ???;
$friend_id[7] = ???;
$friend_id[8] = ???;

something like:

<?php

$count=0;
$num_cols = 3;

echo "<table>";

while($row = mysql_fetch_array($results)){

if($count == 0 && $count % $num_cols == 0)
	echo "<tr>";

if($count != 0 && $count % $num_cols == 0)
	echo "</tr><tr>";

//echo row data in <td><td>

$count++;

}

//finish table. fill in blank cells.
while($count % $num_cols != 0){

echo "<td> </td>";
}

echo "</tr></table>";


?>

 

i am getting a never ending table with 1 row and never ending columns    :shrug:

 

$get_friend_id = mysql_query("SELECT * FROM page_zebra WHERE user_id='$user_id' AND friend=1 LIMIT 0,9");

$count=0;
$num_cols = 3;

echo "<table border='1'>";

while($row = mysql_fetch_array($get_friend_id)){

if($count == 0 && $count % $num_cols == 0)
	echo "<tr>";

if($count != 0 && $count % $num_cols == 0)
	echo "</tr><tr>";

//echo row data in <td><td>

$count++;

}

//finish table. fill in blank cells.
while($count % $num_cols != 0){

echo "<td> </td>";
}

echo "</tr></table>";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.