Jump to content


Photo

help with if statement


  • Please log in to reply
8 replies to this topic

#1 speedy33417

speedy33417
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 17 September 2006 - 05:32 PM

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:
1234
5678
Right now I'm only working on displaying the first row with the 4 pics

My code works fine like this:
<?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>

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's

Here's my code with the if statement that doesn't work:

<?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>

Can anyone see why? It seems to be going into an eternal loop for some reason...

Thanks.

#2 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 17 September 2006 - 05:46 PM

$numcol = 0;
$numpics = 7; // this is however many pictures total you want displayed
for($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>";
}


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.

#3 speedy33417

speedy33417
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 17 September 2006 - 06:11 PM

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

<td>display pic</td>
<td>display pic</td>
<td>display pic</td>
<td>display pic</td>

and should do the following if numpics=2

<td>display pic</td>
<td>display pic</td>
<td></td>
<td></td>

I still need to echo 2 empty td's for my table to look good.

Thanks again.

#4 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 17 September 2006 - 06:17 PM

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. 

#5 speedy33417

speedy33417
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 17 September 2006 - 07:41 PM

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:

<?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>

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.



#6 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 17 September 2006 - 07:49 PM

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. 

      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++; 
        } 


#7 speedy33417

speedy33417
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 17 September 2006 - 08:10 PM

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.

#8 speedy33417

speedy33417
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 17 September 2006 - 08:22 PM

Actually one more question  :D

When 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.

#9 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 17 September 2006 - 08:24 PM

When I do like </td> or any closing tag I'll add \n which is newline, and then tab it in a bit with \t
So it looks like
echo "</td>" . "\n" . "\t\t";





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users