speedy33417 Posted September 17, 2006 Share Posted September 17, 2006 I'm working on a photo album and I have a small problem with an if statement that use when I'm displaying the thumbs of my pics. There should be 4 pics in a row, like so:12345678Right now I'm only working on displaying the first row with the 4 picsMy code works fine like this: [code]<?php $numcol = 1; ?> blah blah <div align="center"> <table border="0" width="648" cellpadding="5" cellspacing="2"> <tr> <?php while ($numcol < 5) { echo "<td width=\"162\" bgcolor=\"#e6e6e6\" valign=\"top\"><p align=\"center\">"; echo "<img border=\"0\" src=\"images/somepic.jpg\" width=\"150\" height=\"113\"><br>"; echo "<span class=\"text1\"><b>some text</b><br>some text<br>some text<br></span>"; echo "</p></td>"; $numcol++; } ?> </tr> </table> </div>[/code]However this code will only work properly if I have exactly 4 pics left to display. For this reason I added an if statement with an empty <td></td> to be poked in if there's no pic left to display to fill up my row with 4 td'sHere's my code with the if statement that doesn't work:[code]<?php $numpicsleft=2; $numcol = 1; ?> blah blah <div align="center"> <table border="0" width="648" cellpadding="5" cellspacing="2"> <tr> <?php while ($numcol < 5) { if($numpicsleft > 1) { echo "<td width=\"162\" bgcolor=\"#e6e6e6\" valign=\"top\"><p align=\"center\">"; echo "<img border=\"0\" src=\"images/somepic.jpg\" width=\"150\" height=\"113\"><br>"; echo "<span class=\"text1\"><b>some text</b><br>some text<br>some text<br></span>"; echo "</p></td>"; $numcol++; $numpicsleft--; } else { echo "<td width=\"162\"></td>"; } } ?> </tr> </table> </div>[/code]Can anyone see why? It seems to be going into an eternal loop for some reason...Thanks. Quote Link to comment Share on other sites More sharing options...
Wintergreen Posted September 17, 2006 Share Posted September 17, 2006 [code]$numcol = 0;$numpics = 7; // this is however many pictures total you want displayedfor($i = 0; $i < $numpics; $i++) { if($numcol == 0) { echo "<tr>"; } echo "<td width=\"162\" bgcolor=\"#e6e6e6\" valign=\"top\"><p align=\"center\">"; echo "<img border=\"0\" src=\"images/somepic.jpg\" width=\"150\" height=\"113\"><br>"; echo "<span class=\"text1\"><b>some text</b><br>some text<br>some text<br></span>"; echo "</p></td>"; $numcol++; if($numcol == 3) { echo "</tr>"; $numcol = 0; }}if($numcol != 0) { echo "</tr>";}[/code]So each time it displays a pic, $numcol goes up one. It starts out at 0, meaning it will put a <tr> in. When it reaches 4 (after having displayed 4 pics) it will put a </tr> and reset to 0. Then, if there are still more pics to be displayed, it will post another <tr> and then continue going. The last thing I do is check the value of $numcol when it's done with the for loop. If it is anything but 0 it means that there needs to be a </tr> echoed. Quote Link to comment Share on other sites More sharing options...
speedy33417 Posted September 17, 2006 Author Share Posted September 17, 2006 Thanks for your help.Not really at I was looking for though.My code is only part of what it should be. It should do the following when numpics=4[code]<td>display pic</td><td>display pic</td><td>display pic</td><td>display pic</td>[/code]and should do the following if numpics=2[code]<td>display pic</td><td>display pic</td><td></td><td></td>[/code]I still need to echo 2 empty td's for my table to look good.Thanks again. Quote Link to comment Share on other sites More sharing options...
Wintergreen Posted September 17, 2006 Share Posted September 17, 2006 if($numcol != 0) { $temp = $numpics % 4; $left = 4 - $temp; for($i = 0; $i < $left; $i++) { echo "<td></td>"; } echo "</tr>";}Replace the last part with this. Quote Link to comment Share on other sites More sharing options...
speedy33417 Posted September 17, 2006 Author Share Posted September 17, 2006 I'm not sure what I'm describing wrong, but I don't need a tr in my code. The part I need debuged is the code I have on top, but I'll post it again:[code]<?php $numpicsleft=2; $numcol = 1; ?> blah blah <div align="center"> <table border="0" width="648" cellpadding="5" cellspacing="2"> <tr> <?php while ($numcol < 5) { if($numpicsleft > 0) { echo "<td width=\"162\" bgcolor=\"#e6e6e6\" valign=\"top\"><p align=\"center\">"; echo "<img border=\"0\" src=\"images/somepic.jpg\" width=\"150\" height=\"113\"><br>"; echo "<span class=\"text1\"><b>some text</b><br>some text<br>some text<br></span>"; echo "</p></td>"; $numcol++; $numpicsleft--; } else { echo "<td width=\"162\"></td>"; } } ?> </tr> </table> </div>[/code]Basically the idea is that there's 4 cells in the row. Ideally there's a pic and some description in each cell. That's when $numpicsleft=4. However if $numpicsleft=2, then only the first two cells are posting pictures, but the last two cells are still created only it's empty (<td></td>)Please don't post totally new coding, because I'm new to php. I only intend to find out what I did wrong in mine.Thanks again. Quote Link to comment Share on other sites More sharing options...
Wintergreen Posted September 17, 2006 Share Posted September 17, 2006 Two things with your code, then. What happens if there are more pics than just the first row? Your TRs aren't in any sort of loop.The problem with your code is your if else statement in the loop. You're incrementing the $numcol only in the IF part, but not in the ELSE part, which is what would give you problems. [code] while ($numcol < 5) { if($numpicsleft > 0) { echo "<td width=\"162\" bgcolor=\"#e6e6e6\" valign=\"top\"><p align=\"center\">"; echo "<img border=\"0\" src=\"images/somepic.jpg\" width=\"150\" height=\"113\"><br>"; echo "<span class=\"text1\"><b>some text</b><br>some text<br>some text<br></span>"; echo "</p></td>"; $numpicsleft--; } else { echo "<td width=\"162\"></td>"; } $numcol++; } [/code] Quote Link to comment Share on other sites More sharing options...
speedy33417 Posted September 17, 2006 Author Share Posted September 17, 2006 That worked! Great. Thank you.Btw, I was starting out slow, that's why I left my TR's out of the loop. I wanted TD's to work fine before I added another loop for the TR's as well.I knew it was just a small glitch somewhere, but I just couldn't figure out what for the life of me...Thanks again. Quote Link to comment Share on other sites More sharing options...
speedy33417 Posted September 17, 2006 Author Share Posted September 17, 2006 Actually one more question :DWhen I do a view source of my code, the echoed code becomes very long with no line break. Which makes it hard to check for mistakes in my generated code. How do I add a line break to the end of my echo code?Thanks. Quote Link to comment Share on other sites More sharing options...
Wintergreen Posted September 17, 2006 Share Posted September 17, 2006 When I do like </td> or any closing tag I'll add \n which is newline, and then tab it in a bit with \tSo it looks like[code]echo "</td>" . "\n" . "\t\t";[/code] Quote Link to comment 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.