alphadeltaviii Posted January 26, 2008 Share Posted January 26, 2008 Hi I have a script to pull values for an image gallery from a mysql db, but I am having trouble with the script filling a row with 3 images then moving on to the next row of the table. Any suggestions? (For example the table is 3 cells wide, then expands vertically) Thanks Quote Link to comment https://forums.phpfreaks.com/topic/87970-solved-filling-in-values/ Share on other sites More sharing options...
laffin Posted January 26, 2008 Share Posted January 26, 2008 use the modulus operator for($i=1;$i<12;$i++) echo "$i" . (($i % 3)?' . ':'<BR>'); [code] which is basicly the same as keeping a counter [code] for($i=1,$counter=0;$i<12;$i++,$counter++) { echo "$i"; if($counter<3) echo " . "; else echo "<BR>"; } [/code][/code] Quote Link to comment https://forums.phpfreaks.com/topic/87970-solved-filling-in-values/#findComment-450094 Share on other sites More sharing options...
alphadeltaviii Posted January 26, 2008 Author Share Posted January 26, 2008 So would i input this for each row of the table I want displayed? (sorry kinda a php noob) Quote Link to comment https://forums.phpfreaks.com/topic/87970-solved-filling-in-values/#findComment-450098 Share on other sites More sharing options...
alphadeltaviii Posted January 26, 2008 Author Share Posted January 26, 2008 Here is what I have so far <?php include ("dbinfo.php"); $sql = "SELECT * FROM picture ORDER BY 'ID' ASC"; $result = mysql_query($sql); print mysql_error(); if(mysql_num_rows($result)) { while($row = mysql_fetch_assoc($result)) { $id = $row['ID']; $plink = $row['link']; $ptitle = $row['title']; $palt = $row['alt']; echo ('<table width="510px">'); echo ('<tr>'); echo ('<td><a href=...'); echo ('<td><a href=...'); echo ('<td><a href=...'); } } Quote Link to comment https://forums.phpfreaks.com/topic/87970-solved-filling-in-values/#findComment-450129 Share on other sites More sharing options...
alphadeltaviii Posted January 27, 2008 Author Share Posted January 27, 2008 Could anyone help me integrate this code Thanks Quote Link to comment https://forums.phpfreaks.com/topic/87970-solved-filling-in-values/#findComment-450186 Share on other sites More sharing options...
amites Posted January 27, 2008 Share Posted January 27, 2008 you're on the right track, you're if statement might want to look like if ((mysql_num_rows($result)) >= 1 ) { a simple method of going from there would be a counter, so something like $i = 1; echo ('<table width="510px">'); while ($row = mysql_fetch_assoc($result)) { if ($i == 1) { echo '<tr>'; } $i ++; $id = $row['ID']; $plink = $row['link']; $ptitle = $row['title']; $palt = $row['alt']; echo '<td><a href=...'; if ($i == 3) { echo '</tr>'; $i = 1; } } echo '</table'; does this make sense? * edit * forgot to close the loop Quote Link to comment https://forums.phpfreaks.com/topic/87970-solved-filling-in-values/#findComment-450201 Share on other sites More sharing options...
alphadeltaviii Posted January 27, 2008 Author Share Posted January 27, 2008 yeah, it does. If i have any issues with it i'll post back Quote Link to comment https://forums.phpfreaks.com/topic/87970-solved-filling-in-values/#findComment-450209 Share on other sites More sharing options...
alphadeltaviii Posted January 27, 2008 Author Share Posted January 27, 2008 I'm having trouble finding my issue, the page isn't loading <?php include ("dbinfo.php"); $sql = "SELECT * FROM picture ORDER BY 'ID' ASC"; $result = mysql_query($sql); print mysql_error(); if(mysql_num_rows($result)) { $i = 1; echo ('<table width=\"510\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">'); while ($row = mysql_fetch_assoc($result)) { if ($i == 1) { echo ('<tr>'); } $i ++; $id = $row['ID']; $plink = $row['link']; $ptitle = $row['title']; $palt = $row['alt']; echo ('<td width=\"170px\" height=\"120px\"><a href=\"/pgallery/$plink.jpg\" rel=\"lightbox\" title=\"$title\"><img src=\"/pgallery/thumb/$plink.jpg\" /></a>'); if ($i == 3) { echo ('</tr>'); $i = 1; } echo ('</table>'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/87970-solved-filling-in-values/#findComment-450215 Share on other sites More sharing options...
alphadeltaviii Posted January 27, 2008 Author Share Posted January 27, 2008 bump Quote Link to comment https://forums.phpfreaks.com/topic/87970-solved-filling-in-values/#findComment-450244 Share on other sites More sharing options...
sasa Posted January 27, 2008 Share Posted January 27, 2008 <?php include ("dbinfo.php"); $sql = "SELECT * FROM picture ORDER BY ID ASC"; //remove ' around ID $result = mysql_query($sql); print mysql_error(); if(mysql_num_rows($result)){ $i = 1; echo ('<table width=\"510\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">'); while ($row = mysql_fetch_assoc($result)) { if ($i == 1) { echo ('<tr>'); } $i ++; $id = $row['ID']; $plink = $row['link']; $ptitle = $row['title']; $palt = $row['alt']; echo ("<td width=\"170px\" height=\"120px\"><a href=\"/pgallery/$plink.jpg\" rel=\"lightbox\" title=\"$ptitle\"><img src=\"/pgallery/thumb/$plink.jpg\" /></a></td>"); // in line befure chenge ' to " and add </td> to end if ($i == 4) {//change 3 to 4 echo ('</tr>'); $i = 1; } }// close while loop echo ('</table>'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/87970-solved-filling-in-values/#findComment-450286 Share on other sites More sharing options...
alphadeltaviii Posted January 27, 2008 Author Share Posted January 27, 2008 crud, still doesn't work Quote Link to comment https://forums.phpfreaks.com/topic/87970-solved-filling-in-values/#findComment-450289 Share on other sites More sharing options...
alphadeltaviii Posted January 27, 2008 Author Share Posted January 27, 2008 Ok, works like a charm (now). Only took 3 hours and massive troubleshooting. Apparently, I CANNOT copy the code off of the website and even remotely think it will work. It must be retyped Quote Link to comment https://forums.phpfreaks.com/topic/87970-solved-filling-in-values/#findComment-450299 Share on other sites More sharing options...
sasa Posted January 27, 2008 Share Posted January 27, 2008 does sript output anything in page (look in page source too) Quote Link to comment https://forums.phpfreaks.com/topic/87970-solved-filling-in-values/#findComment-450302 Share on other sites More sharing options...
alphadeltaviii Posted January 27, 2008 Author Share Posted January 27, 2008 well before no, nothing and it has xhtml also. now it is working good after I typed it out by hand Quote Link to comment https://forums.phpfreaks.com/topic/87970-solved-filling-in-values/#findComment-450305 Share on other sites More sharing options...
ziv Posted January 27, 2008 Share Posted January 27, 2008 simple oo php example: <?php <?php class ImageDetails { /** * all image details you need from your db. * use "mysql_fetch_object" to build your results as objects */ public function getName() { return 'x'; // image name for example... } } class ImageGallery { private $list; private $size; /** * @param array $list, list of images details * @param int $size, number of gallery collumns */ public function __construct($list, $size = 3) { $this->list = $list; $this->size = $size; } public function show() { $counter = 0; echo '<table border="1">'; foreach ($this->list as $image) { if ($counter == 0) { // open row echo '<tr>'; } $this->_printRow($image); ++$counter; if ($counter == $this->size) { // close row echo '</tr>'; $counter = 0; } } if ($counter != 0) { // padding the table $this->_paddTable($counter); } echo '</table>'; } private function _printRow($image) { // here you print the image in any form you want echo '<td>' . $image->getName() . '</td>'; } private function _paddTable($counter) { for ($i=$counter ; $i<$this->size ; ++$i) { echo '<td> </td>'; } } } // 'list of your images (ImageDetails objects) $list = array(new ImageDetails(), new ImageDetails(), new ImageDetails(), new ImageDetails(), new ImageDetails(), new ImageDetails(), new ImageDetails(), new ImageDetails(), new ImageDetails(), new ImageDetails()); $gallery = new ImageGallery($list); $gallery->show(); Quote Link to comment https://forums.phpfreaks.com/topic/87970-solved-filling-in-values/#findComment-450324 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.