Jump to content

Recommended Posts

(I am a total php newbie)

 

Hello :)

 

I would like to output the content of a one column mysql table into a grid - sort of like this...

 

apple

pear

cherry

banana

kiwi

orange

mango

peach

plum

 

as

 

apple | pear  | cherry | banana

kiwi  | orange | mango  | peach

plum  |

 

etc etc

 

so I'm thinking...

 

 

while($row = mysql_fetch_row($result)) {

    create table code here

    }

 

 

I tried the following to create a set number of columns and rows...

 

 

for ($cols=0; $cols < 4; $cols++) {

echo "<tr>";

for ($rows=0; $rows < 6; $rows++) { echo "<td> </td>"; }

echo "</tr>";

}

echo '</table>';

 

 

My problem is, how do I enter the result $row[0] in between the <TD> tags?

 

Or how do I get to 'break out' of the loop to get the next $row[0] result.

 

As it is - the same record is being printed over and over again?

 

I'm very confused :(

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/173258-outputting-mysql-as-an-html-grid/
Share on other sites

Thanks a million :)

 

There seems to be an issue with that code.

For the most part is does what I want, but if the number of results from the MySQL table is an exact multiple of $max_columns - then this bit kicks in...

 

if($i < $max_columns)
{
    for($j=$i; $j<$max_columns;$j++)
        echo "<td> </td>";
}

 

...and you get superfluous data cells added.

 

<td> </td><td> </td><td> </td></tr>

 

I know this post would be better off in the 'actual' topic - but it seems I am not allowed to post there.

 

Thanks again :)

 

 

I changed the code at the end of the table to this...

 

}  // end if 



if ($i != 0) 


if($i < $max_columns)
{
    for($j=$i; $j<$max_columns;$j++) {  echo "<td> </td>";}  echo  "</tr>";
}


?>

</table>

 

It's now working whether or not the number of rows returned is the same as $max_columns

 

As newbie code goes, I'm sure that's a sloppy solution, so if anyone can suggest something better - I'd be happy to listen :)

 

Thanks again.

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.