Jump to content

help with if statement


speedy33417

Recommended Posts

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

Here'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.
Link to comment
Share on other sites

[code]
$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>";
}
[/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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.