pagination within image - IF problem

Thread Solved

Join Date: Apr 2008
Posts: 48
Reputation: helraizer is an unknown quantity at this point 
Solved Threads: 3
helraizer helraizer is offline Offline
Light Poster

pagination within image - IF problem

 
0
  #1
Jun 2nd, 2008
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:

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

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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 100
Reputation: petr.pavel is an unknown quantity at this point 
Solved Threads: 14
petr.pavel's Avatar
petr.pavel petr.pavel is offline Offline
Junior Poster

Re: pagination within image - IF problem

 
0
  #2
Jun 10th, 2008
Hi there,
the bug is in this if statement. Try using parentheses to make your intentions more clear:
  1. if ( (!isset($filter)) && ($page == 1) )

Frankly I would just write this instead of the whole if/else if bunch:
  1. if ($page == 1)
  2. imagegif($image, "user/" . $user . ".gif");
  3. imagegif($image);

Also, you could make the initial testing a bit more simple:
  1. $page = max(intval($_GET['page']), 1);
I guess you start paging with 1, which is a bit impractical.
If you start with zero you could do with a plain intval($_GET['page']);
Last edited by petr.pavel; Jun 10th, 2008 at 7:13 am.
Petr 'PePa' Pavel

The more information you give the more relevant answer you get.
Please consider using "Add to ... Reputation" and mark your thread as Solved if you found what you were looking for. By giving feedback you help others.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 48
Reputation: helraizer is an unknown quantity at this point 
Solved Threads: 3
helraizer helraizer is offline Offline
Light Poster

Re: pagination within image - IF problem

 
0
  #3
Jun 10th, 2008
Hey, thanks for the input.

I found a solution a while ago.

  1. if (isset($filter))
  2. {
  3.  
  4. imagepng($image);
  5.  
  6. }
  7. elseif ($page == 1)
  8. {
  9.  
  10. imagepng($image);
  11. imagepng($image, "./user/".$user.".png");
  12.  
  13.  
  14.  
  15.  
  16. }
  17. else
  18. {
  19. imagepng($image);
  20.  
  21. }

if ($page == 1)
imagegif($image, "user/" . $user . ".gif");
imagegif($image);
$page is always set, even if filter is on. Since it's for use as a signature, with the code above, if someone uses the filter then it'll change the image in the signature, which isn't good. Therefore if filter is set (regardless of whether page is set) then the image isn't saved; if filter isn't saved but page is, then it is saved and if $page != 1 then it isn't saved either.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC