pagination within image - IF problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • helraizer1
    New Member
    • Mar 2007
    • 118

    pagination within image - IF problem

    Hey folks,

    I have made an image-based shoutbox and now users can view older and newer message on the shoutbox depending on the $_GET['page'] - pagination - that works. However, since it's image based and can be linked in forums etc. on the Internet I only want it to save the gif image if $page == 1, so the newest shouts are saved onto the image. Yet the users can still view the other messages on site without it changing the saved gif image.

    What I've tried so far is this:

    [code=php]<?php

    if (isset($_GET['page']) && is_numeric($_GE T['page'])) {

    $page = mysql_real_esca pe_string(htmls pecialchars($_G ET['page']));

    } else {
    $page = 1;
    }

    //other code - not relevant for this purpose

    if ($page == 1) {
    imagegif($image ); // paint the image in browser
    imagegif($image , "user/" . $user . ".gif"); //export as gif file
    } elseif(!isset($ filter) && $page == 1) {
    imagegif($image ); // paint the image in browser
    imagegif($image , "user/" . $user . ".gif"); //export as gif file
    } elseif($page != 1) {
    imagegif($image ); // paint the image in browser
    } elseif(isset($f ilter)) {
    imagegif($image ); // paint the image in browser
    } else {

    }
    ?>
    [/code]

    Which, to me means that when $page == 1 then it loads the image in the browser and also saves user/helraizer.gif and if $page != 1 then it only loads it to the browser and doesn't save the gif. But even if $page == 3 then it saves that as the image and therefore the image hosted on a forum will keep changing as each user views a different thing.

    I know that $page works because it changes the contents of the image, but it doesn't work within the if statement.

    Can you see what's/if anything's wrong with the code? Would a switch statement be a better solution?

    Thanks,
    Sam
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Sam.

    Let me get out my nitpicking needles here....

    Originally posted by helraizer1
    [code=php]<?php

    if (isset($_GET['page']) && is_numeric($_GE T['page'])) {

    $page = mysql_real_esca pe_string(htmls pecialchars($_G ET['page']));

    } else {
    $page = 1;
    }
    [/code]
    If you've established that $_GET['page'] is numeric, you don't need to do any SQL or HTML escaping. A simple $page = (int) $_GET['page'] will do.

    [code=php]
    if ($page == 1) {
    imagegif($image ); // paint the image in browser
    imagegif($image , "user/" . $user . ".gif"); //export as gif file
    [/code]
    So far so good.
    [code=php]
    } elseif(!isset($ filter) && $page == 1) {
    [/code]
    By this point, you already know that $page == 1 because of the previous condition.
    [code=php]
    } elseif($page != 1) {[/code]
    This is effectively an else block.

    And the rest of the code will never get executed.

    Try using the identity operator, since PHP does some screwy things when you use the equality operator on a value that could evaluate to (bool) true.

    In other words:
    [code=php]
    3 == true == 1 // But 3 != 1 except in rare instances.
    3 !== true !== 1
    [/code]

    This should do just fine:
    [code=php]
    if( $page === 1 )
    {
    // display and save
    }
    else
    {
    // display only
    }
    [/code]

    Comment

    • helraizer1
      New Member
      • Mar 2007
      • 118

      #3
      Originally posted by pbmods
      Heya, Sam.

      Let me get out my nitpicking needles here....



      If you've established that $_GET['page'] is numeric, you don't need to do any SQL or HTML escaping. A simple $page = (int) $_GET['page'] will do.

      [code=php]
      if ($page == 1) {
      imagegif($image ); // paint the image in browser
      imagegif($image , "user/" . $user . ".gif"); //export as gif file
      [/code]
      So far so good.
      [code=php]
      } elseif(!isset($ filter) && $page == 1) {
      [/code]
      By this point, you already know that $page == 1 because of the previous condition.
      [code=php]
      } elseif($page != 1) {[/code]
      This is effectively an else block.

      And the rest of the code will never get executed.

      Try using the identity operator, since PHP does some screwy things when you use the equality operator on a value that could evaluate to (bool) true.

      In other words:
      [code=php]
      3 == true == 1 // But 3 != 1 except in rare instances.
      3 !== true !== 1
      [/code]

      This should do just fine:
      [code=php]
      if( $page === 1 )
      {
      // display and save
      }
      else
      {
      // display only
      }
      [/code]
      Yeah, I realised that. Thanks for that, I made some changes; I had to use this in the end:
      [code=php]

      if (isset($filter) ) {
      imagegif($image ); // paint the image in browser
      } elseif ($page == 1) {
      imagegif($image ); // paint the image in browser
      imagegif($image , "user/" . $user . ".gif"); //export as gif file
      } else {
      // Here, $page != 1 && !isset($filter)
      imagegif($image ); // paint the image in browser
      }
      [/code]

      I had to use the $filter one first because the $page variable is still in the url even when $filter is set, so if $page was it the first IF, it would still save even if $filter was set. The way I have it now it works poifectly.

      Thanks for your help once again. :)

      Sam

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Glad to hear you got it working! Good luck with you project, and you need anything else... well, you've heard this speech before :P

        Comment

        Working...