Jump to content

Recommended Posts

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!  ;D

Link to comment
https://forums.phpfreaks.com/topic/42752-creating-table-cells-from-mysql-results/
Share on other sites

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

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

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  :D

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

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.