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

Link to comment
Share on other sites

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] = ???;

Link to comment
Share on other sites

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>";


?>

 

Link to comment
Share on other sites

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>";

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.