473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

pagination within image - IF problem

118 100+
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
3 1594
pbmods
5,821 Expert 4TB
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
helraizer1
118 100+
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
5,821 Expert 4TB
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

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

Similar topics

0
by: Don Quijote de Nicaragua | last post by:
Hello chic@s, I have problem with pagination and procedure stored, can to show information within DataGrid, but when I do click in nobody of the number which they down appear for the pagination, do...
2
by: Chris H | last post by:
I am having a problem with pagination, basically the problem is happening in the "PREV / NUMBERS / NEXT" links, it appears as if the reason is becasue the increment and decrement operators aren't...
11
by: ste | last post by:
Hi there, Further to my recent posts where I've received excellent help from Rik and Jerry, I've ended up with an image gallery on my website that displays images in a table, 3 images per row. ...
4
by: comp.lang.php | last post by:
'll try to explain this as clearly as possible, sorry if it's unclear. You have in your directory /foo 42 images You have in your database metadata for 30 out of those 42 images You have to...
1
by: OceanBreeze | last post by:
I am new to .Net. I am using ASP 2.0 and C#. I want to pupolate a data grid programatically using the values obtained from a list conating domain objects. E.g., DAL.GetEmployee() returns a...
2
by: mac | last post by:
Hi, I'm looking for a script that will allow me to upload images to a folder and shows pagination... pls can any body help me? i hav done with the uploading, but not able to do with the...
16
by: gnawz | last post by:
I have a pagination function I am using in a file called functions.php as below<? //Pagination functions function getPagingQuery($sql, $itemPerPage = 10) { if (isset($_GET) && (int)$_GET > 0) ...
1
by: vinpkl | last post by:
hi all i m using pagination to show my products results according to particular category. it shows PREV 1,2, NEXT but when i click these links then these links dont show next and prev...
4
by: ArizonaJohn | last post by:
Hello, The code below works great. The user enters a name into an HTML form, the code looks up a table with that name, and then that table is displayed. I am trying to use pagination with it,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.