k1llr0bot Posted March 14, 2007 Share Posted March 14, 2007 basically what i'm trying to do is take the results from a mysql query, store them into an array, and create a table cell for each row. the goal is to end up with a table 3 cells across and as many rows as required to display all of the results. the code i'm using is, echo "<table>"; $query="SELECT foo FROM bar"; $result=mysql_query($query); $num=mysql_numrows($result); for ($i=1; $i<=$num; $i++) $newData[]=mysql_fetch_array($result); mysql_close(); for ($i=1; $i<=$num; $i++) { $currentRow = $newData[$i]; echo "<td>$currentRow[id]</td>" ."</td>\n"; if(($i % 3 == 0) { echo "</tr>\n<tr>\n"; } } echo "</table>"; it always skips the first record, and i don't know how to clean up the table to it ends evenly. if i change the i to 0 it displays the first result but the table gets all out of whack. is there an easier way to do this or am i close? thanks! Quote Link to comment https://forums.phpfreaks.com/topic/42752-creating-table-cells-from-mysql-results/ Share on other sites More sharing options...
per1os Posted March 14, 2007 Share Posted March 14, 2007 <?php echo "<table>"; $query="SELECT foo FROM bar"; $result=mysql_query($query); $num=mysql_numrows($result); for ($i=0; $num > $i; $i++) $newData[$i]=mysql_fetch_array($result); mysql_close(); for ($i=0; count($newData) > $i; $i++) { $currentRow = $newData[$i]; echo "<td>$currentRow[id]</td>" ."</td>\n"; if(($i % 3 == 0) { echo "</tr>\n<tr>\n"; } } echo "</table>"; ?> See if that helps anything. --FrosT Quote Link to comment https://forums.phpfreaks.com/topic/42752-creating-table-cells-from-mysql-results/#findComment-207469 Share on other sites More sharing options...
boo_lolly Posted March 14, 2007 Share Posted March 14, 2007 i'd just use a while loop: $query="SELECT foo FROM bar"; $result=mysql_query($query); $i = 0; echo "<table>\n"; while($row = mysql_fetch_array($result)){ (($i % 3 == 0) ? ("<tr>\n") : ("")); echo "<td>$row['id']</td>\n"; (($i % 3 == 0) ? ("</tr>\n") : ("")); $i++; } echo "</table>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/42752-creating-table-cells-from-mysql-results/#findComment-207482 Share on other sites More sharing options...
per1os Posted March 14, 2007 Share Posted March 14, 2007 Yea or you could do it the easier and more efficient way that boo_lolly just showed you, or you can do it the hard half-assed and un-efficient way I showed you =) The choice is yours and yours alone. (nj boo) --FrosT Quote Link to comment https://forums.phpfreaks.com/topic/42752-creating-table-cells-from-mysql-results/#findComment-207493 Share on other sites More sharing options...
k1llr0bot Posted March 14, 2007 Author Share Posted March 14, 2007 Yea or you could do it the easier and more efficient way that boo_lolly just showed you, or you can do it the hard half-assed and un-efficient way I showed you =) The choice is yours and yours alone. (nj boo) --FrosT haha, thanks to you both! i tend to do things the half-assed and un-efficient way myself so i'll try boo_lolly's method as a change of pace Quote Link to comment https://forums.phpfreaks.com/topic/42752-creating-table-cells-from-mysql-results/#findComment-207495 Share on other sites More sharing options...
boo_lolly Posted March 14, 2007 Share Posted March 14, 2007 it's not that it's inefficient, it's just that there's unnecessary code there's always more than one way to skin a cat that got so fat. we're the brews. Quote Link to comment https://forums.phpfreaks.com/topic/42752-creating-table-cells-from-mysql-results/#findComment-207497 Share on other sites More sharing options...
per1os Posted March 14, 2007 Share Posted March 14, 2007 I just like being half-assed. edit: Although almost every time I code I always make it easier with functions. Such as in this case I have a function called fetchArr($sql) that returns me an array or a fetchMultiArr($sql) that fetches me a multi-dimensional array. so I do not have to duplicate that while code =) Makes it alot easier. --FrosT Quote Link to comment https://forums.phpfreaks.com/topic/42752-creating-table-cells-from-mysql-results/#findComment-207501 Share on other sites More sharing options...
boo_lolly Posted March 14, 2007 Share Posted March 14, 2007 dirka Quote Link to comment https://forums.phpfreaks.com/topic/42752-creating-table-cells-from-mysql-results/#findComment-207535 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.