...

View Full Version : Dynamic Tables and PHP?



angst
07-07-2005, 07:37 PM
Hello,

I wanna build a simple photo gallery using php and mysql.

what i want to know is how to dynamicly build the tables and cell,
so say i pull 13 images from the data base.

and i want 3 columns and 4 rows.
how would i do this in php?

I'm a little confused as you may be able to tell.
an example, or if anyone knows of a good tutorial, it would be a big help!

thanks in advance for your time!
-Ken

devinemke
07-07-2005, 08:06 PM
a very basic example:


<?php
// connect to db here

$result = mysql_query('SELECT * FROM photos LIMIT 12') or exit(mysql_error());
while ($row = mysql_fetch_assoc($result))
{
$photos[$row['photo_id']] = $row;
}

$chunks = array_chunk($photos, 3);

echo '<table border="1">';
foreach ($chunks as $value)
{
echo '<tr>';
foreach ($value as $sub_value)
{
echo '<td><img src="/path/to/photos/' . $sub_value['filename'] . '"></td>';
}
echo '</tr>';
}
echo '</table>';
?>

angst
07-07-2005, 08:53 PM
kool! thats what i was looking for, but could i ask one more question.

what if i have 20 pics, and i only want to show 12 per page, then be able to move to the next 8.

can this be done with just the sql statement?


thanks again!
-Ken

devinemke
07-07-2005, 08:58 PM
search keyword "pagination"



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum