triovia Posted November 24, 2007 Share Posted November 24, 2007 Hello, I have a very basic question. I want to display 4 images in a <td> from a database. Once the loop gets to 4 images I want to start the 5th one on a new line. Can someone help me with this. This is what I have so far (obvisously it keeps creating new <td>, but no <tr>... <table width="100%" border="0" cellspacing="0" cellpadding="4"> <tr> <?php do { ?> <td><img src="/photos/sm-display/<?php echo $row_display_cat_photos['small_url']; ?>" /></td> <?php } while ($row_display_cat_photos = mysql_fetch_assoc($display_cat_photos)); ?> </tr> </table> Thanks for your help! Link to comment Share on other sites More sharing options...
0x00 Posted November 24, 2007 Share Posted November 24, 2007 Not the most elegant but: table width="100%" border="0" cellspacing="0" cellpadding="4"> <tr> <?php $c = 0; do { print '<td><img src="/photos/sm-display/<?php echo $row_display_cat_photos['small_url']; ?>" /></td>'; if(($c % 4) > 4) { $c = 0; print '</tr><tr>'; } $c++; } while ($row_display_cat_photos = mysql_fetch_assoc($display_cat_photos)); print '</tr></table>'; ?> P.S. This is untested... Link to comment Share on other sites More sharing options...
triovia Posted November 24, 2007 Author Share Posted November 24, 2007 Thanks for the quick response, I tried doing that and It doesn't seem to be doing anything. Any other ideas? When I tried to paste that in I got "Parse error: syntax error, unexpected T_STRING" So I changed it to this and didn't get any errors, but like I said, it's still putting 5 images across rather than starting a new <tr> <table width="100%" border="0" cellspacing="0" cellpadding="4"> <tr> <?php $c = 0; do { ?> <td><img src="/photos/sm-display/<?php echo $row_display_cat_photos['small_url'] ;?>" /></td> <? if(($c % 4) > 4) { $c = 0; print '</tr><tr>'; } $c++; } while ($row_display_cat_photos = mysql_fetch_assoc($display_cat_photos)); print '</tr></table>'; Thanks Again! Link to comment Share on other sites More sharing options...
xyn Posted November 24, 2007 Share Posted November 24, 2007 are you trying to produce a table, by X columns? while displaying data into it. if so try my code: This code below, will generate a table with 5 columbs, with a 10 px separation between them. and automatically detects when a new row is needed. This also adds EMPTY rows and columns to complete the table. You need to modify the following: if(count($arr) < 5) { $i = 5; } else { $i = count($arr); } to the amount of items you have. And... if(!$arr[$ii][0]) { $photo_name = ""; } elseif(get_photo_data($arr[$ii][0], "image") == "0") { $photo_name = "<a href=\"#\"><img src=\"/images/uploads/default_folder.jpg\" border=\"0\" width=\"100\" height=\"98\" class=\"thumbnail\"></a>"; } else { $photo_name = "<a href=\"#\"><img src=\"/images/uploads/".get_photo_data($arr[$ii][0], "image")."\" border=\"0\" width=\"100\" height=\"98\" class=\"thumbnail\"></a>"; } to your actual elements you're adding. <?php echo("<table width=\"607\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\r"); if(count($arr) < 5) { $i = 5; } else { $i = count($arr); } for($ii=0; $ii<$i; $ii++) { if(!$arr[$ii][0]) { $photo_name = ""; } elseif(get_photo_data($arr[$ii][0], "image") == "0") { $photo_name = "<a href=\"#\"><img src=\"/images/uploads/default_folder.jpg\" border=\"0\" width=\"100\" height=\"98\" class=\"thumbnail\"></a>"; } else { $photo_name = "<a href=\"#\"><img src=\"/images/uploads/".get_photo_data($arr[$ii][0], "image")."\" border=\"0\" width=\"100\" height=\"98\" class=\"thumbnail\"></a>"; } if($iii == 0) { echo("<tr>"); } if($iii <= 5) { echo("<td width=\"110\" height=\"120\" align=\"center\" class=\"albumtitle\">".$photo_name."<br>".$arr[$ii][1]."</td>\r"); if($iii < 5) { echo("<td width=\"14\" height=\"120\" align=\"center\" class=\"text\"> </td>\r"); } $iii++; } if($iii == 5) { echo("</tr> <tr> <td height=\"5\" width=\"607\" colspan=\"9\"></td> </tr>"); $iii = 0; } } while( ($iii > 0) && ($iii < 5) ) { echo("<td width=\"110\" height=\"120\"></td>\r"); if($iii < 5) { echo("<td width=\"14\" height=\"120\"></td>\r"); } $iii++; } echo("</table>"); ?> Link to comment Share on other sites More sharing options...
0x00 Posted November 24, 2007 Share Posted November 24, 2007 Here's another way based on your code: <?php print "<table width='100%' border='0' cellspacing='0' cellpadding='4'>\n<tr>\n"; $c = 0; do { $c++; print "<td><img src='"./photos/sm-display/<?php echo $row_display_cat_photos['small_url']."' alt='IMG'></td>\n"; if($c > 3) { $c = 0; print "</tr><tr>\n"; } } while ($row_display_cat_photos = mysql_fetch_assoc($display_cat_photos)); print "</tr></table>\n"; ?> Here's the phpfreaks snippit way: http://www.phpfreaks.com/forums/index.php/topic,95426.0.html Link to comment Share on other sites More sharing options...
xyn Posted November 24, 2007 Share Posted November 24, 2007 Here's another way based on your code: <?php print "<table width='100%' border='0' cellspacing='0' cellpadding='4'>\n<tr>\n"; $c = 0; do { $c++; print "<td><img src='"./photos/sm-display/<?php echo $row_display_cat_photos['small_url']."' alt='IMG'></td>\n"; if($c > 3) { $c = 0; print "</tr><tr>\n"; } } while ($row_display_cat_photos = mysql_fetch_assoc($display_cat_photos)); print "</tr></table>\n"; ?> This wont work. because: 1. $c++ starts the array at 1. (this misses 0 resulting in the first object not appearing). 2. do you think <?php inside a print will seriously work? try exiting the print. 3. if you're only adding rows and not columns you could do this code... print "<table>"; while ($row_display_cat_photos = mysql_fetch_assoc($display_cat_photos)){ print "<tr>\n"; print "<td> body here</td>"; print "<tr>"; } print "</table>"; Link to comment Share on other sites More sharing options...
0x00 Posted November 24, 2007 Share Posted November 24, 2007 ? Have you tried it ? What do you mean by point 2? Where? + Your extra code doesn'y do it! P.S. $c doesn't actually refer to an object, it's just a counter, yes you could rewrite it to get rid of one ++ per row, but hey it's Saturday night and i'm at the movies... Link to comment Share on other sites More sharing options...
xyn Posted November 24, 2007 Share Posted November 24, 2007 Ok. $c = 0; then you do... do { $c++; print meaning as soon as that loop starts it adds 1 to 0. so it starts at 1 not 0. Pooint two. print "<td><img src='"./photos/sm-display/<?php echo $row_display_cat_photos['small_url' you cant have <?php inside a print. it must be print "<td><img src='/photos/sm-display/".$row_display_cat_photos['small_url' Link to comment Share on other sites More sharing options...
0x00 Posted November 24, 2007 Share Posted November 24, 2007 Point 2, yep, didn't see that. It was a copy paste once back in forum. Point 1, yep, move it to end of section and change logic to >= or subtract one from it... Whichever position you come from, it's still a whole lotta code less than yours! Link to comment Share on other sites More sharing options...
xyn Posted November 24, 2007 Share Posted November 24, 2007 Ok, well yours is less code. but wont execute due to the errors. Once fixed. your do loop will take longer than a while loop. My longer coding. actually takes the data from the database and produces X amount of Columns per row. it then notices when a new row is needed. it adds a separation between columns, and fills empty columns correctly to finish the table. I then realised its only a ROW needed which this is THE BEST process: print "<table>"; while ($row_display_cat_photos = mysql_fetch_array($display_cat_photos)){ print "<tr>\n"; print "<td> body here</td>"; print "<tr>"; } print "</table>"; 1. its 6 lines. 2. and the fetch_array, gives an associative and non-associative array. [: Link to comment Share on other sites More sharing options...
triovia Posted November 24, 2007 Author Share Posted November 24, 2007 Thanks everyone for your help! This is the piece of code I used: print "<table width='100%' border='0' cellspacing='0' cellpadding='4'>\n<tr>\n"; $c = 0; do { $c++; print "<td><img src='/photos/sm-display/".$row_display_cat_photos['small_url']."' alt='IMG'></td>\n"; if($c > 3) { $c = 0; print "</tr><tr>\n"; } } while ($row_display_cat_photos = mysql_fetch_assoc($display_cat_photos)); print "</tr></table>\n"; It seems to work out great! That long post didn't make much sense to me ??? sorry I'll have to look into that a bit further if I have problems down the road, for now this works. You guys are great! Thanks for the help. Link to comment Share on other sites More sharing options...
AtomicNetwork Posted November 24, 2007 Share Posted November 24, 2007 triovia, I would like to take this opportunity to mention that xyn's last post is infact better, and more efficient then the code you have selected to use. Regards. Link to comment Share on other sites More sharing options...
0x00 Posted November 25, 2007 Share Posted November 25, 2007 As you'll also notice, it's not really one's code, yet a slight amendment. It succinctly meets the requirement outlined in said brief. If you wanna get atomic, then your 'echo's' don't need brackets, why use escape sequences when there are adequate alternatives. You use multiple if statements which could be incorporated with else's. You $arr multiple times, a bit wasteful don't you think? In C your told you only ever need to use for statements, so both the do I used and your while are wasteful when we get down to it. Yes with my classes I avoid associative arrays for multiple reasons and agree with yours. If we want to get finitikie then you should of setup some css tags for table definitions and others would say, why waste your time with tables at all... To finish, as stated in first post, I wasn't even at a computer with server to test with, and if I hadn't had a few, wouldn't of even risen to this... Just glad i'm also not using my real user! FN: Your boss has just posted to support you, he also should read whole post, and also remember this is a teaching forum. Not so highbrow... Merry Christmas! Link to comment Share on other sites More sharing options...
xyn Posted November 25, 2007 Share Posted November 25, 2007 1. echos dont need brackets, just like while loops dont need a DO { }. This is the best way. print "<table>"; while ($row_display_cat_photos = mysql_fetch_array($display_cat_photos)){ print "<tr>\n"; print "<td> body here</td>"; print "<tr>"; } print "</table>"; 2. my multiple arrays is because you dont understand my coding. or the objective. that snippet i pasted will create Rows, and the columns including colums to space each other columns. and rows to space other rows. which is an include of my template engine. where the multple array comes into play. 3. you say my boss. he and i are self employed php developers. he also does coldfusion. and I quote "this is a teaching forum." my question to that is. why are you teaching people. errored coding? Everyone will agree my coding in this post is the more efficient coding for the "brief" so why don't accept it. and learn something. instead of insist your right. when blatenly you're not! And a happy new year to you too. Link to comment Share on other sites More sharing options...
0x00 Posted November 25, 2007 Share Posted November 25, 2007 are you trying to produce a table, by X columns? while displaying data into it. if so try my code: This code below, will generate a table with 5 columbs, with a 10 px separation between them. and automatically detects when a new row is needed. This also adds EMPTY rows and columns to complete the table. You need to modify the following: if(count($arr) < 5) { $i = 5; } else { $i = count($arr); } to the amount of items you have. And... if(!$arr[$ii][0]) { $photo_name = ""; } elseif(get_photo_data($arr[$ii][0], "image") == "0") { $photo_name = "<a href=\"#\"><img src=\"/images/uploads/default_folder.jpg\" border=\"0\" width=\"100\" height=\"98\" class=\"thumbnail\"></a>"; } else { $photo_name = "<a href=\"#\"><img src=\"/images/uploads/".get_photo_data($arr[$ii][0], "image")."\" border=\"0\" width=\"100\" height=\"98\" class=\"thumbnail\"></a>"; } to your actual elements you're adding. <?php echo("<table width=\"607\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\r"); if(count($arr) < 5) { $i = 5; } else { $i = count($arr); } for($ii=0; $ii<$i; $ii++) { if(!$arr[$ii][0]) { $photo_name = ""; } elseif(get_photo_data($arr[$ii][0], "image") == "0") { $photo_name = "<a href=\"#\"><img src=\"/images/uploads/default_folder.jpg\" border=\"0\" width=\"100\" height=\"98\" class=\"thumbnail\"></a>"; } else { $photo_name = "<a href=\"#\"><img src=\"/images/uploads/".get_photo_data($arr[$ii][0], "image")."\" border=\"0\" width=\"100\" height=\"98\" class=\"thumbnail\"></a>"; } if($iii == 0) { echo("<tr>"); } if($iii <= 5) { echo("<td width=\"110\" height=\"120\" align=\"center\" class=\"albumtitle\">".$photo_name."<br>".$arr[$ii][1]."</td>\r"); if($iii < 5) { echo("<td width=\"14\" height=\"120\" align=\"center\" class=\"text\"> </td>\r"); } $iii++; } if($iii == 5) { echo("</tr> <tr> <td height=\"5\" width=\"607\" colspan=\"9\"></td> </tr>"); $iii = 0; } } while( ($iii > 0) && ($iii < 5) ) { echo("<td width=\"110\" height=\"120\"></td>\r"); if($iii < 5) { echo("<td width=\"14\" height=\"120\"></td>\r"); } $iii++; } echo("</table>"); ?> That doesn't cover the brief. It's inefficient, yes use's some efficiency in places. But my answer to which you (in a lot of posts) say 'try my code', only added 2/3 short lines, less than 2 mins whilst watching 'The Company'. There's some good posts in linuxquestions about efficient coding, for me I have classes for making tables, forms, pagination, email, img uploads and i've not had to touch them (other than php upgrades) for over 3 years, and are very pier reviewed, by experts, university lecturers, banks and the such like. So when you say everyone, do you mean you at 19 and your pal at 18, I've been coding longer than you are old, I remember ZX when it came out, lollipop. P.S. Voodoo just calls you kiddie scripters... Link to comment Share on other sites More sharing options...
xyn Posted November 25, 2007 Share Posted November 25, 2007 Ok. explain this. print "<table>"; while ($row_display_cat_photos = mysql_fetch_array($display_cat_photos)){ print "<tr>\n"; print "<td> body here</td>"; print "<tr>"; } print "</table>"; your DO {} is better / more efficient than the above coding HOW? and your bragging about coding doesnt make you look good. considering you cant even fix errors. "cut and paste" when you declared it as "your work". Anyway. i'm only proving my work is good, it works, its what most developers use. unless your still back on php 3. i suggest you just leave this convo just like i am. Link to comment Share on other sites More sharing options...
Barand Posted November 25, 2007 Share Posted November 25, 2007 Timeout called. Link to comment Share on other sites More sharing options...
Recommended Posts