By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,312 Members | 1,323 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,312 IT Pros & Developers. It's quick & easy.

pagination within image - IF problem

100+
P: 118
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:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. if (isset($_GET['page']) && is_numeric($_GET['page'])) {
  4.  
  5.     $page = mysql_real_escape_string(htmlspecialchars($_GET['page']));
  6.  
  7. } else {
  8.     $page = 1;
  9. }
  10.  
  11. //other code - not relevant for this purpose
  12.  
  13. if ($page == 1) {
  14.     imagegif($image); // paint the image in browser
  15.     imagegif($image, "user/" . $user . ".gif"); //export as gif file
  16. } elseif(!isset($filter) && $page == 1) {
  17.     imagegif($image); // paint the image in browser
  18.     imagegif($image, "user/" . $user . ".gif"); //export as gif file
  19. } elseif($page != 1) {
  20.     imagegif($image); // paint the image in browser
  21. } elseif(isset($filter)) {
  22.     imagegif($image); // paint the image in browser
  23. } else {
  24.  
  25. }
  26. ?>
  27.  
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
Jun 2 '08 #1
Share this Question
Share on Google+
3 Replies


pbmods
Expert 5K+
P: 5,821
Heya, Sam.

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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. if (isset($_GET['page']) && is_numeric($_GET['page'])) {
  4.  
  5.     $page = mysql_real_escape_string(htmlspecialchars($_GET['page']));
  6.  
  7. } else {
  8.     $page = 1;
  9. }
  10.  
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.

Expand|Select|Wrap|Line Numbers
  1. if ($page == 1) {
  2.     imagegif($image); // paint the image in browser
  3.     imagegif($image, "user/" . $user . ".gif"); //export as gif file
  4.  
So far so good.
Expand|Select|Wrap|Line Numbers
  1. } elseif(!isset($filter) && $page == 1) {
  2.  
By this point, you already know that $page == 1 because of the previous condition.
Expand|Select|Wrap|Line Numbers
  1. } elseif($page != 1) {
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:
Expand|Select|Wrap|Line Numbers
  1. 3 == true == 1  // But 3 != 1 except in rare instances.
  2. 3 !== true !== 1
  3.  
This should do just fine:
Expand|Select|Wrap|Line Numbers
  1. if( $page === 1 )
  2. {
  3.     // display and save
  4. }
  5. else
  6. {
  7.     // display only
  8. }
  9.  
Jun 3 '08 #2

100+
P: 118
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.

Expand|Select|Wrap|Line Numbers
  1. if ($page == 1) {
  2.     imagegif($image); // paint the image in browser
  3.     imagegif($image, "user/" . $user . ".gif"); //export as gif file
  4.  
So far so good.
Expand|Select|Wrap|Line Numbers
  1. } elseif(!isset($filter) && $page == 1) {
  2.  
By this point, you already know that $page == 1 because of the previous condition.
Expand|Select|Wrap|Line Numbers
  1. } elseif($page != 1) {
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:
Expand|Select|Wrap|Line Numbers
  1. 3 == true == 1  // But 3 != 1 except in rare instances.
  2. 3 !== true !== 1
  3.  
This should do just fine:
Expand|Select|Wrap|Line Numbers
  1. if( $page === 1 )
  2. {
  3.     // display and save
  4. }
  5. else
  6. {
  7.     // display only
  8. }
  9.  
Yeah, I realised that. Thanks for that, I made some changes; I had to use this in the end:
Expand|Select|Wrap|Line Numbers
  1.  
  2. if (isset($filter)) {
  3.     imagegif($image); // paint the image in browser
  4. } elseif ($page == 1) {
  5.     imagegif($image); // paint the image in browser
  6.     imagegif($image, "user/" . $user . ".gif"); //export as gif file
  7. } else {
  8.     // Here, $page != 1 && !isset($filter)
  9.     imagegif($image); // paint the image in browser
  10. }
  11.  
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
Jun 3 '08 #3

pbmods
Expert 5K+
P: 5,821
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
Jun 4 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.