Jump to content

outputting MySQL as an html grid


tHud

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.

Archived

This topic is now archived and is closed to further replies.

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