coolphpdude Posted July 15, 2008 Share Posted July 15, 2008 Hi guys, I touched on this a while back but never got it sorted. Im just wondering how to do this. Imagine these 2 scenario's. 1) You have 13 pics to display in a table... you output these 1 after another in a while loop vertically. The code would simply output the pictures 1 after another vertically until 13 pics were output (1 new row after another) ...This scenario is fine, i understand it and have used it. Nice and easy. But how about this one... 2) You need to output the pictures horizontally with limited horizontal space... Then what? Say i have got a table 800px wide and i can only fit 3 pictures on every row, i would need a table consisting of 3 columns and 5 rows. But i am still not sure how to go about doing this or how to insert each picture one after the other. Also what happens if you dont kno how many results would be returned... I could do a query that returns 53 pictures or 999 pics... What i am trying to get at is how do i output all of my pictures without using the verticle, one after another, method. Im quite stuck with this one!! Quote Link to comment https://forums.phpfreaks.com/topic/114885-outputting-photos-in-a-table/ Share on other sites More sharing options...
craygo Posted July 15, 2008 Share Posted July 15, 2008 What I would do is use an image resize script, which is available on this forum in the repository. Then keep the rows a static size and put the photo thumbs in it. Here is a script which will do it. Just have to substitute your image name in between the <td> tags. <html> <table width="800" border="0" cellspacing="0" cellpadding="10" align="center"> <?php require('config.php'); include('includes/mysql.php'); //set number of columns and initial column number $numcols = 4; // how many columns to display $numcolsprinted = 0; // no of columns so far // get the results to be displayed $query = "SELECT imagename FROM mytable "; $mysql_result = mysql_query($query) or die (mysql_error()); // get each row while($myrow = mysql_fetch_row($mysql_result)) { //get data $pic = $myrow[0]; if ($numcolsprinted == $numcols) { print "</tr>\n<tr>\n"; $numcolsprinted = 0; } // output row from database echo "<td width=\"25%\"><img src=\"images/$pic_name\" /></td>\n"; // bump up row counter $numcolsprinted++; } // end while loop $colstobalance = $numcols - $numcolsprinted; for ($i=1; $i<=$colstobalance; $i++) { } print "<TD></TD>\n"; ?> </table> </html> Ray Quote Link to comment https://forums.phpfreaks.com/topic/114885-outputting-photos-in-a-table/#findComment-590797 Share on other sites More sharing options...
justinjkiss Posted July 15, 2008 Share Posted July 15, 2008 as long as the images are fixed in size it wouldnt be hard to create a table to do 3 per row it would be as simple as // print HTML table echo '<tr>'; echo '<td>whatever text if any</td>'; echo '</tr>'; // iterate over record set // print each field while($row = mysql_fetch_row($result)) { echo '<tr>'; echo '<td></td>'; echo '<td></td>'; echo '<td></td>'; echo '</tr>'; } } else { // print error message echo '<tr>'; echo '<td>No pics found</td>'; echo '</tr>'; } // once processing is complete // free result set mysql_free_result($result); // close connection to MySQL server mysql_close($connection); then just use your code. Im new at php but would image that would work. I get mysql data and insert it into columns then start a new row. Might have to use some count variable. Dont know what your code is. If you dont have a standard size picture use the code from the previous post to resize then insert into a 3 column loop like i showed Quote Link to comment https://forums.phpfreaks.com/topic/114885-outputting-photos-in-a-table/#findComment-590804 Share on other sites More sharing options...
anon_login_001 Posted July 15, 2008 Share Posted July 15, 2008 Math is your friend. I suck at math, but this works: You'll have an IF statement in the loop, something like this: if ($picCount % 3 === 0) { // echo '</tr><tr>'; // start a new row! } Assuming $picCount is the number of the current picture, and you'd place that IF statement near the end of the loop. Something like that would check to see if the REMAINDER of the Count, Divided by the Number of Pictures So Far is ZERO. If the number is Zero, obviously you have a number evenly divisible by 3 (in this case)... see how that works? Quote Link to comment https://forums.phpfreaks.com/topic/114885-outputting-photos-in-a-table/#findComment-590806 Share on other sites More sharing options...
coolphpdude Posted July 15, 2008 Author Share Posted July 15, 2008 I've got set pic sizes and 3 pics would fit comfortably on every row... So basically do a pic count and row count?? Ignore the syntax but something as follows for quickness?? <table> <tr> // start the while loop within the row while ($imagesexist) { <td>$image<td> // if the the $imageexist counter exceeds 3 close off that row and begin new row if ($imageexist is a multiple of 3) { </tr> <tr> } // end while loop } //close table </table> would that work??? Quote Link to comment https://forums.phpfreaks.com/topic/114885-outputting-photos-in-a-table/#findComment-590815 Share on other sites More sharing options...
anon_login_001 Posted July 15, 2008 Share Posted July 15, 2008 Exactly. It's a simple, hard-coded way to do it... but it gets the job done. Quote Link to comment https://forums.phpfreaks.com/topic/114885-outputting-photos-in-a-table/#findComment-590829 Share on other sites More sharing options...
craygo Posted July 15, 2008 Share Posted July 15, 2008 The code i post works fine just change the number of columns you want. Quote Link to comment https://forums.phpfreaks.com/topic/114885-outputting-photos-in-a-table/#findComment-590838 Share on other sites More sharing options...
coolphpdude Posted July 15, 2008 Author Share Posted July 15, 2008 i will do craygo, so if i want 3 columns it will automatically keep recreating the rows until the output is complete?? I'll give this a go when i get to uni tomorrow. Cheers for your help guys Quote Link to comment https://forums.phpfreaks.com/topic/114885-outputting-photos-in-a-table/#findComment-590845 Share on other sites More sharing options...
kenrbnsn Posted July 15, 2008 Share Posted July 15, 2008 Take a look at this example. It takes care of partially filled rows at the end: <?php $photos = range(0,rand(10,27)); $cols = rand(3,10); echo 'Photos: ' . count($photos) . '<br>Columns: ' . $cols . '<br>'; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> </head> <body> <table border="1"> <?php $tmp = array(); $tmp2 = array(); $i = 0; foreach($photos as $ind => $v) { if ($i % $cols == 0 && !empty($tmp)) { $tmp2[] = '<tr><td>' . implode('</td><td>',$tmp) . '</td></tr>'; $tmp = array(); $tmp[] = 'image ' .$v; } else $tmp[] = 'image ' . $v; $i++; } if (!empty($tmp)) $tmp2[] = '<tr><td>' . implode('</td><td>',$tmp) . '</td></tr>'; echo implode("\n",$tmp2); ?> </table> </body> </html> Ken Quote Link to comment https://forums.phpfreaks.com/topic/114885-outputting-photos-in-a-table/#findComment-590846 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.