Problem with if statement. Please help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ciprian Ilie

    Problem with if statement. Please help!

    Hi there,

    If you look at the code below, you will see that I am using a template
    in order to display some photos on my website.
    I also have "previous" and "next" buttons/link which increment the
    PhotoId in my table so you can easily navigate.

    I only have 35 rows in this particular table, but when you get to the
    35th picture, clicking on next takes you to 36 which does not exist.
    Same when you are on id 1 and click previous.

    I am trying to prevent this with my poor excuse of an "if" statement,
    but it doesn't seem to do the trick.

    Please advise.

    Regards,
    Ciprian


    <html>
    <head>
    <?php
    $db = mysql_connect(" localhost","use r","pass");
    mysql_select_db ("mydb",$db) ;
    $request = "SELECT * FROM photos WHERE PhotoId=".$Phot oId;
    $result = mysql_query ($request,$db);
    $photos =mysql_fetch_ob ject($result);
    mysql_free_resu lt($result);
    ?>
    <title>Photos 2004 - <?php echo $photos->PhotoDesc ?> </title>
    </head>

    <body>
    <table width="600" border="0" cellspacing="0" cellpadding="2"
    align="center">
    <tr>
    <td align="center">
    <img src="<?php echo $photos->PhotoFileNam e ?>" alt="<?php echo
    $photos->PhotoDesc ?>" title="<?php echo $photos->PhotoDesc ?>"
    border="0">
    </td>
    </tr>
    <tr>
    <td align='center'>
    <?php echo $photos->PhotoDesc ?>
    </td>
    </tr>
    <tr>
    <td align='center'>
    <a href='index.php ?PhotoId=<?php echo $photos->PhotoId-1
    ?>'>Previous</a> | <a href="index.php ?PhotoId=1">Ind ex</a> | <a
    href='index.php ?PhotoId=<?php echo $photos->PhotoId+1 ?>'>Next</a>
    </td>
    </tr>
    </table>
    <?php
    if (&PhotoId > 35)
    print "1";
    ?>
    </body>
    </html>
  • Pedro Graca

    #2
    Re: Problem with if statement. Please help!

    Ciprian Ilie wrote:[color=blue]
    > Hi there,
    >
    > If you look at the code below, you will see that I am using a template
    > in order to display some photos on my website.
    > I also have "previous" and "next" buttons/link which increment the
    > PhotoId in my table so you can easily navigate.
    >
    > I only have 35 rows in this particular table, but when you get to the
    > 35th picture, clicking on next takes you to 36 which does not exist.
    > Same when you are on id 1 and click previous.
    >
    > I am trying to prevent this with my poor excuse of an "if" statement,
    > but it doesn't seem to do the trick.
    >
    > Please advise.
    >
    > Regards,
    > Ciprian
    >
    >
    ><html>
    ><head>
    ><?php
    > $db = mysql_connect(" localhost","use r","pass");
    > mysql_select_db ("mydb",$db) ;
    > $request = "SELECT * FROM photos WHERE PhotoId=".$Phot oId;
    > $result = mysql_query ($request,$db);
    > $photos =mysql_fetch_ob ject($result);
    > mysql_free_resu lt($result);
    > ?>
    ><title>Photo s 2004 - <?php echo $photos->PhotoDesc ?> </title>
    ></head>
    >
    ><body>
    ><table width="600" border="0" cellspacing="0" cellpadding="2"
    > align="center">
    ><tr>
    > <td align="center">
    > <img src="<?php echo $photos->PhotoFileNam e ?>" alt="<?php echo
    > $photos->PhotoDesc ?>" title="<?php echo $photos->PhotoDesc ?>"
    > border="0">
    > </td>
    ></tr>
    ><tr>
    > <td align='center'>
    > <?php echo $photos->PhotoDesc ?>
    > </td>
    ></tr>[/color]

    You have to put your if() inside the <tr> for navigation

    <tr>
    <td align='center'>
    <?php
    // print "Previous" link if PhotoID>1
    if ($PhotoID>1) { ?>
    <a href='index.php ?PhotoId=<?php echo $photos->PhotoId-1; ?>'>Previous</a>
    <?php } ?>
    | <a href="index.php ?PhotoId=1">Ind ex</a> |
    <?php
    // print "Next" link if PhotoID<35 (really should not be a constant here)
    if ($PhotoID<35) { ?>
    <a href='index.php ?PhotoId=<?php echo $photos->PhotoId+1 ?>'>Next</a>
    <?php } ?>
    </td>
    </tr>


    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • Ciprian Ilie

      #3
      Re: Problem with if statement. Please help!

      Hi Pedro,

      Thanks a lot for your help, but it still does not look right.
      The "previous" link is missing alltogether, regardless of the fact the
      PhotoId is bigger than 1, and the "Next" link is always present even
      if the PhotoId is 35 or 36 etc

      Any advice? I could really do with getting this sorted

      Regards,
      Ciprian

      Comment

      • Pedro Graca

        #4
        Re: Problem with if statement. Please help!

        Ciprian Ilie wrote:[color=blue]
        > The "previous" link is missing alltogether, regardless of the fact the
        > PhotoId is bigger than 1, and the "Next" link is always present even
        > if the PhotoId is 35 or 36 etc[/color]

        Maybe there's some confusion here with 'standard' variables and
        objects ???

        is it $PhotoID or $Photo->PhotoID?


        I'm not used to using objects. When I see too many '->' I get all
        nervous. Maybe I said something that led you astray. If I did, I am
        truly sorry.

        Please repost your relevant code, and I'm sure you'll be helped to
        understand why it doesn't work the way you want.


        PS: don't assume we all know what this is about.
        Keep some of the previous post (what is related to the new one)
        to let us in on what you're talking about [I had to repull the
        old posts to understand what you were talking about] :)
        --
        --= my mail box only accepts =--
        --= Content-Type: text/plain =--
        --= Size below 10001 bytes =--

        Comment

        • Rahul Anand

          #5
          Re: Problem with if statement. Please help!

          Try this modified code. Hope it will work (Not Tested)

          As you are using PhotoId which i think will be a autoincrement field
          so you can not rely on its continuity, as some rows might get deleted
          with time.

          So, you are required to fetch the previous and next ids from database.

          There are better and more efficient solutions, which you can try, but
          this is the easiest i guess.

          [SNIP]

          <html>
          <head>
          <?php
          $db = mysql_connect(" localhost","use r","pass");
          mysql_select_db ("mydb",$db) ;
          $request = "SELECT PhotoId FROM photos WHERE PhotoId < '$PhotoId'
          ORDER BY PhotoId DESC";
          if($row =mysql_fetch_ob ject(mysql_quer y ($request,$db)) )
          $prevId = $cntrow->PhotoId;
          else
          $prevId = '';

          $request = "SELECT PhotoId FROM photos WHERE PhotoId > '$PhotoId'
          ORDER BY PhotoId";
          if($row =mysql_fetch_ob ject(mysql_quer y ($request,$db)) )
          $nextId = $cntrow->PhotoId;
          else
          $nextId = '';



          $request = "SELECT * FROM photos WHERE PhotoId=".$Phot oId;
          $result = mysql_query ($request,$db);
          $photos =mysql_fetch_ob ject($result);
          mysql_free_resu lt($result);

          ?>
          <title>Photos 2004 - <?php echo $photos->PhotoDesc ?> </title>
          </head>

          <body>
          <table width="600" border="0" cellspacing="0" cellpadding="2"
          align="center">
          <tr>
          <td align="center">
          <img src="<?php echo $photos->PhotoFileNam e ?>" alt="<?php echo
          $photos->PhotoDesc ?>" title="<?php echo $photos->PhotoDesc ?>"
          border="0">
          </td>
          </tr>
          <tr>
          <td align='center'>
          <?php echo $photos->PhotoDesc ?>
          </td>
          </tr>
          <tr>
          <td align='center'>
          <?php
          if(!empty($prev Id))
          {
          ?>
          <a href='index.php ?PhotoId=<?php echo $prevId?>'>Prev ious</a> |
          <?
          }
          ?>
          <a href="index.php ?PhotoId=<?php echo $photos->PhotoId?>">Ind ex</a>
          <?php
          if(!empty($next Id))
          {
          ?>
          | <a href='index.php ?PhotoId=<?php echo $nextId?>'>Next </a>
          <?
          }
          ?>

          </td>
          </tr>
          </table>
          </body>
          </html>

          [/SNIP]


          Pedro Graca <hexkid@hotpop. com> wrote in message news:<c08t3f$13 58d1$1@ID-203069.news.uni-berlin.de>...[color=blue]
          > Ciprian Ilie wrote:[color=green]
          > > The "previous" link is missing alltogether, regardless of the fact the
          > > PhotoId is bigger than 1, and the "Next" link is always present even
          > > if the PhotoId is 35 or 36 etc[/color]
          >
          > Maybe there's some confusion here with 'standard' variables and
          > objects ???
          >
          > is it $PhotoID or $Photo->PhotoID?
          >
          >
          > I'm not used to using objects. When I see too many '->' I get all
          > nervous. Maybe I said something that led you astray. If I did, I am
          > truly sorry.
          >
          > Please repost your relevant code, and I'm sure you'll be helped to
          > understand why it doesn't work the way you want.
          >
          >
          > PS: don't assume we all know what this is about.
          > Keep some of the previous post (what is related to the new one)
          > to let us in on what you're talking about [I had to repull the
          > old posts to understand what you were talking about] :)[/color]

          Comment

          • Ciprian Ilie

            #6
            Re: Problem with if statement. Please help!

            Thanks again for all your help, but this does not work.
            Would have liked to get some useful advise of where am I going wrong
            rather than code which is not working and I haven't got a clue how to
            debug

            Cheers

            Comment

            • Pedro Graca

              #7
              Re: Problem with if statement. Please help!

              Ciprian Ilie wrote:[color=blue]
              > Thanks again for all your help, but this does not work.
              > Would have liked to get some useful advise of where am I going wrong
              > rather than code which is not working and I haven't got a clue how to
              > debug[/color]

              As I said before, you need to test for 1 and 35 individually right
              before printing (or not) the 'Previous' and 'Next' link.

              So, if you're at PhotoID 19 you want to display

              <link to Photo 18> | <index> | <link to Photo 20>

              but if you're viewing Photo 1 you want that to be

              <index> | <link to Photo 2>

              and similarly for Photo 35

              <link to Photo 34> | <index>

              It's easy to identify the parts that need special treatment, namely the
              "<link to previous Photo> | " and the " | <link to next Photo>"


              So your pseudo code could be something like

              if (ok to print link to previous photo) print "<previous link> | "
              print "<index>"
              if (ok to print link to next photo) print " | <next link>"

              Now you just have to come up with a working way of saying that in PHP :)
              --
              --= my mail box only accepts =--
              --= Content-Type: text/plain =--
              --= Size below 10001 bytes =--

              Comment

              Working...