djrichwz Posted July 11, 2007 Share Posted July 11, 2007 Hi I am trying to display items from a mysql database in a table but I need it to start a new table row each time multiples of 3 rows of the database table have been displayed like the following example: item 1 item 2 item3 item 4 item 5 item 6 Does anybody know how I can make this work using php. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 11, 2007 Share Posted July 11, 2007 <?php print '<table cellspacing="0" cellpadding="2" border="1">'; $query=mysql_query("SELECT items FROM table"); while ($row=mysql_fetch_assoc($query)) { print '<tr><td>'.$row[0].'</td><td>'.$row[1].'</td><td>'.$row[2].'</td></tr>'; } print '</table>'; ?> Quote Link to comment Share on other sites More sharing options...
rameshfaj Posted July 11, 2007 Share Posted July 11, 2007 I think the above post gives the solution. But u can also initialize a variable and increase it to the no of times u wanted to display the rows. U should check if the variable is still below the required rows then enter inside the loop. Quote Link to comment Share on other sites More sharing options...
djrichwz Posted July 11, 2007 Author Share Posted July 11, 2007 thanks that should do the trick then ive got to go work now but ill try it out later Thanx in advance Quote Link to comment Share on other sites More sharing options...
djrichwz Posted July 12, 2007 Author Share Posted July 12, 2007 Hi I tried the code Below with my database table inserted instead and it did not show any of my results have you any idea why this wouldnt work. I want to display each row of my table until it gets to three rows then start a new table row. Hope somebody can help? <?php print '<table cellspacing="0" cellpadding="2" border="1">'; $query=mysql_query("SELECT msg FROM news"); while ($row=mysql_fetch_assoc($query)) { print '<tr><td>'.$row[0].'</td><td>'.$row[1].'</td><td>'.$row[2].'</td></tr>'; } print '</table>'; ?> Quote Link to comment Share on other sites More sharing options...
bluebyyou Posted July 12, 2007 Share Posted July 12, 2007 I tried to figure out your problem using a nested for loop. I was not able to get it to work since my inside loop only works once. However this is in the direction of what you need. Perhaps you can work from this. If someone else can figure out why the inner loop only works once, then this will work. I would definately like to know. To test this just change: $total_table_rows = ceil($total_results / $max_rows); to $total_table_rows = 15; (or any number you like) <?php $max_colums = 3; //Set the number of columns you want in your table $total_results = mysql_result($result,0); //Get the number of results from your query $total_table_rows = ceil($total_results / $max_rows); //Figure out how many rows needed in table ?> <table> <?php for($i = 1; $i <= $total_table_rows; $i++) { $from = (($i * $max_rows) - $max_rows); echo "<tr><td>$i</td>"; //Show which table row it is for ( $j = $from; $j < $max_rows ; $j++) { echo "<td>result: $j</td>"; } echo "</tr>\n"; } ?> </table> Quote Link to comment Share on other sites More sharing options...
bluebyyou Posted July 12, 2007 Share Posted July 12, 2007 Does anyone know why the inner loop in my attempted solution above is not working? Quote Link to comment Share on other sites More sharing options...
bluebyyou Posted July 14, 2007 Share Posted July 14, 2007 I don't know where djrichwz is at, but im still curious about this. Heres what the output of my "solution" looks like: <table border="1"> <tr><td>1</td><td>result: 0</td><td>result: 1</td><td>result: 2</td></tr> <tr><td>2</td></tr> <tr><td>3</td></tr> <tr><td>4</td></tr> <tr><td>5</td></tr> </table> It is still stopping after only working once. Quote Link to comment Share on other sites More sharing options...
djrichwz Posted July 16, 2007 Author Share Posted July 16, 2007 sorry guys i had to go work and had to go to my mums birthday at the weekend i will try it now. Quote Link to comment Share on other sites More sharing options...
djrichwz Posted July 16, 2007 Author Share Posted July 16, 2007 no im afraid this does not work all i get is: Warning: Division by zero in /home/www/djrichwz.100webspace.net/test4.php on line 12 1 2 3 I cant figure this one please does anybody have any idea of what i can do. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.