Bizarre PHP Querystring error

Post July 27th, 2005, 8:06 pm

OK, I'm writing my own web app that will create a thumbnail gallery from a database, and Im in the beginning stages (PS, dont bother linking me to an extant app like this, Im interested in getting better at this on my own.) I decided to use querystrings to acheive my results, but during my testing phases there was a truly bizarre error. 'view' and 'picStart' are the two variables in the querystring. This code is within html <body> tags....

Code: [ Download ] [ Select ]
<?php
$picStartNew = $picStart - 20;

function createNavBar(){
    if($picStartNew < 1)
        {echo "<span 
                    class=\"navbar\">Previous</span>\n";}
    else
        {echo "<a href=\"URL?
                   picStart=$picStartNew&view=gallery\"><span
                   class=\"navbar\">Previous</span></a>\n";}

        }
echo "<br>\n";
echo "<br>\n";
createNavBar();

echo "<h1>$picStart $view</h1>";
?>
  1. <?php
  2. $picStartNew = $picStart - 20;
  3. function createNavBar(){
  4.     if($picStartNew < 1)
  5.         {echo "<span 
  6.                     class=\"navbar\">Previous</span>\n";}
  7.     else
  8.         {echo "<a href=\"URL?
  9.                    picStart=$picStartNew&view=gallery\"><span
  10.                    class=\"navbar\">Previous</span></a>\n";}
  11.         }
  12. echo "<br>\n";
  13. echo "<br>\n";
  14. createNavBar();
  15. echo "<h1>$picStart $view</h1>";
  16. ?>


Im trying to make it so that the "previous" button will always be printed, but if there are no pictures previous to the ones shown, it will not hyperlink to a previous page.

To say the results are bizarre is an understatement. At first test, the if statement always prevailed no matter what picStart value I tried. 'Previous' was always printed with no hyperlink.
I don't remember what I did next but I got it to work, but it hyperlinked when picStart was equal to 20. I reset the if statement, came up with the code I have now, and now not only does the if statement always prevail, but the values that are printed show up on different lines on the page depending on the value of picStart. This might all sound sort of convoluted, but please try and help. I've been staring at this crap for half an hour. Thanks guys
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post July 27th, 2005, 8:06 pm

  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Joined: Jun 15, 2003
  • Posts: 4584
  • Loc: New York
  • Status: Offline

Post July 27th, 2005, 8:11 pm

Ok, first off you are sure your server has regestered globals on? Sometimes using $_REQUEST['url_variable'] works better anyways.

Also, how is picStart set?
Pixel Acres V2

Post July 27th, 2005, 8:33 pm

picStart is set in the querystring

gallery.php?picStart=22&view=gallery would be an example

Post July 27th, 2005, 8:42 pm

FullOfQuestions wrote:
picStart is set in the querystring

gallery.php?picStart=22&view=gallery would be an example


Right, but how are you getting it in php? As b_heyer said, if register_globals is off on your server, simply referencing $picStart won't do.

Php uses the $_GET[] array to reference GET vars (like your using), $_POST[] array to reference POST vars, or $_REQUEST[] array to access either.

If register globals is off, you will need to set picStartNew like this:
PHP Code: [ Download ] [ Select ]
$picStartNew = $_GET['picStart'] - 20;


otherwise, picStart will always be NULL, and subtracting 20 from NULL gives you -20, which is less than 1, which is probably why your if statement always evaluates to true.

Post July 27th, 2005, 9:18 pm

I wish it was that simple....
I set all references to 'view' and 'picStart' to $_REQUEST[''] and Im getting even weirder errors. The code is a little different now as I was testing other stuff out since. You wont believe the errors Im getting...
Code: [ Download ] [ Select ]
</head>

<body>


<?php
define (PICTURES_PER_PAGE, 20);
$pictures = range(0,33);
$picStartNew = $_REQUEST['picStart'] - PICTURES_PER_PAGE;
$totalElements = sizeof($pictures);
$pages = $totalElements/PICTURES_PER_PAGE;
$pages = ceil($pages);

function createNavBar(){
    echo "<span class=\"navbar\">\n";
    if($picStartNew < 1)
        {echo "Previous\n";}
    else
        {echo "<a href=\"gallery.php?picStart=$picStartNew&view=gallery\">Previous</a>\n";}

    echo "</span>";

    echo "<h1>$_REQUEST['picStart'] $_REQUEST['view] </h1><h5>works</h5>";
//line32    }

createNavBar();
?>

</body>
//line 38 </html>
  1. </head>
  2. <body>
  3. <?php
  4. define (PICTURES_PER_PAGE, 20);
  5. $pictures = range(0,33);
  6. $picStartNew = $_REQUEST['picStart'] - PICTURES_PER_PAGE;
  7. $totalElements = sizeof($pictures);
  8. $pages = $totalElements/PICTURES_PER_PAGE;
  9. $pages = ceil($pages);
  10. function createNavBar(){
  11.     echo "<span class=\"navbar\">\n";
  12.     if($picStartNew < 1)
  13.         {echo "Previous\n";}
  14.     else
  15.         {echo "<a href=\"gallery.php?picStart=$picStartNew&view=gallery\">Previous</a>\n";}
  16.     echo "</span>";
  17.     echo "<h1>$_REQUEST['picStart'] $_REQUEST['view] </h1><h5>works</h5>";
  18. //line32    }
  19. createNavBar();
  20. ?>
  21. </body>
  22. //line 38 </html>

This is line 9 to 38. The code displays and errors on absolutely random numbers. For example, lines will be printed when picStart = anything from 2-4, 7, 10-13, 15-16, 18-23, etc. All values for picStart in between those return an error and nothing is printed but the error message. Whats more, the erring numbers will cite different lines for the cause of the error. Some point to 32, some to 31, some to 38 (which is completely outside of the php tags. When I hit reload on the URLs that cite line 38, the script promptly changes its mind and cites line 31 as the problem. What on God's green earth is happening to my script. In case you want to check it yourself, go to http://www.aroundthepoint.com/ellefoley/gallerytest/home.html and click on gallery. All these are tests, by the way, so theres nothing fancy at all. First person to crack this gets a six pack.
  • Rabid Dog
  • Cheese Monkey
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3188
  • Loc: South Africa
  • Status: Offline

Post July 27th, 2005, 10:38 pm

first up, you need to define your constant properly
PHP Code: [ Download ] [ Select ]
 
define ("PICTURES_PER_PAGE", 20);
 
 
  1.  
  2. define ("PICTURES_PER_PAGE", 20);
  3.  
  4.  


Next up, try and avoid embbedding variables (performance and debugging
PHP Code: [ Download ] [ Select ]
 
echo "<h1>". $_REQUEST['picStart'] . $_REQUEST['view'] . " </h1><h5>works</h5>"; // your index wasn't wrapped correctly, missing '
 
 
  1.  
  2. echo "<h1>". $_REQUEST['picStart'] . $_REQUEST['view'] . " </h1><h5>works</h5>"; // your index wasn't wrapped correctly, missing '
  3.  
  4.  


The last note is the error that your site is kicking up
My Software Development Company
Music I have recorded (fixed now :))
Image

Post July 28th, 2005, 8:48 am

Thanks guys you've been a big help so far.
I just noticed a new problem (suprise suprise :) )
$picStartNew is not being set at all... I figured that was an issue, so I tried printing the value and nothing came of it. Check it out
Code: [ Download ] [ Select ]
<?php
define ("PICTURES_PER_PAGE", 20);
$picStartNew = $_REQUEST['picStart'] - PICTURES_PER_PAGE;
$picStartNew2 = $_REQUEST['picStart'] + PICTURES_PER_PAGE;

function createNavBar(){
    echo "<h1>" .$_REQUEST['picStart']. " " .$_REQUEST['view']. "</h1>\n";
    echo "<h3>" .$picStartNew. " and " .$picStartNew2. "</h3>";
}

createNavBar();
?>
  1. <?php
  2. define ("PICTURES_PER_PAGE", 20);
  3. $picStartNew = $_REQUEST['picStart'] - PICTURES_PER_PAGE;
  4. $picStartNew2 = $_REQUEST['picStart'] + PICTURES_PER_PAGE;
  5. function createNavBar(){
  6.     echo "<h1>" .$_REQUEST['picStart']. " " .$_REQUEST['view']. "</h1>\n";
  7.     echo "<h3>" .$picStartNew. " and " .$picStartNew2. "</h3>";
  8. }
  9. createNavBar();
  10. ?>

$_REQUEST['picStart'] and the other request always print, but on the line in the <h3>'s, all that is printed is "and". Any ideas on why both $picStartNew and $picStartNew2 are null even after I set them in the beginning?
  • ryanb
  • Graduate
  • Graduate
  • No Avatar
  • Joined: May 20, 2005
  • Posts: 226
  • Loc: Oregon, US
  • Status: Offline

Post July 28th, 2005, 10:33 am

You cannot access a variable inside a function which was created outside of that function. Functions have their own variable scope, so you need to pass the variables to the function like this:

PHP Code: [ Download ] [ Select ]
define ("PICTURES_PER_PAGE", 20);
 
$picStartNew = $_REQUEST['picStart'] - PICTURES_PER_PAGE;
 
$picStartNew2 = $_REQUEST['picStart'] + PICTURES_PER_PAGE;
 
 
 
function createNavBar($picStartNew, $picStartNew2){
 
   echo "<h1>" .$_REQUEST['picStart']. " " .$_REQUEST['view']. "</h1>\n";
 
   echo "<h3>" .$picStartNew. " and " .$picStartNew2. "</h3>";
 
}
 
 
 
createNavBar($picStartNew, $picStartNew2)
  1. define ("PICTURES_PER_PAGE", 20);
  2.  
  3. $picStartNew = $_REQUEST['picStart'] - PICTURES_PER_PAGE;
  4.  
  5. $picStartNew2 = $_REQUEST['picStart'] + PICTURES_PER_PAGE;
  6.  
  7.  
  8.  
  9. function createNavBar($picStartNew, $picStartNew2){
  10.  
  11.    echo "<h1>" .$_REQUEST['picStart']. " " .$_REQUEST['view']. "</h1>\n";
  12.  
  13.    echo "<h3>" .$picStartNew. " and " .$picStartNew2. "</h3>";
  14.  
  15. }
  16.  
  17.  
  18.  
  19. createNavBar($picStartNew, $picStartNew2)


It may be better just to generate those variables inside the function if that's the only place they are being used.
  • sevster
  • Bronze Member
  • Bronze Member
  • User avatar
  • Joined: Jun 17, 2005
  • Posts: 515
  • Status: Offline

Post July 28th, 2005, 10:40 am

just to make it clear the variables inside the function are completely different from the varialbes outside the function, even though they're named the same in this case.

Code: [ Download ] [ Select ]
$picStartNew = $_REQUEST['picStart'] - PICTURES_PER_PAGE;
$picStartNew2 = $_REQUEST['picStart'] + PICTURES_PER_PAGE;
  1. $picStartNew = $_REQUEST['picStart'] - PICTURES_PER_PAGE;
  2. $picStartNew2 = $_REQUEST['picStart'] + PICTURES_PER_PAGE;


$picStartNew and $picStartNew2 are completely different from the following:

Code: [ Download ] [ Select ]
function createNavBar($picStartNew, $picStartNew2)


technically speaking you should use different names for both just to avoid confusion....but that's just style.

Sevster

Post July 28th, 2005, 12:03 pm

You've been a huge help guys. Just a quick question(s)...

Code: [ Download ] [ Select ]
<?php
global $totalElements, $pages;
...
...
?>
  1. <?php
  2. global $totalElements, $pages;
  3. ...
  4. ...
  5. ?>


If I make this statement, will these variables be available to every function within the PHP tags?
Also, if a function alters either value, will this new value hold true outside the function?
Finally, if I use define() to make a constant outside of a function, will that constant be available inside a function?
Thanks again.
  • ryanb
  • Graduate
  • Graduate
  • No Avatar
  • Joined: May 20, 2005
  • Posts: 226
  • Loc: Oregon, US
  • Status: Offline

Post July 28th, 2005, 1:14 pm

FullOfQuestions wrote:
You've been a huge help guys. Just a quick question(s)...

Code: [ Download ] [ Select ]
<?php
global $totalElements, $pages;
...
...
?>
  1. <?php
  2. global $totalElements, $pages;
  3. ...
  4. ...
  5. ?>


If I make this statement, will these variables be available to every function within the PHP tags?


Nope, but if you use that "global" keyword inside of the function you can access variables outside of the function's scope. Like this:

PHP Code: [ Download ] [ Select ]
$test = 'test1';
 
function test()
{
    global $test;
 
    $test = 'test2';
}
 
test();
 
echo $test; // returns "test2"
  1. $test = 'test1';
  2.  
  3. function test()
  4. {
  5.     global $test;
  6.  
  7.     $test = 'test2';
  8. }
  9.  
  10. test();
  11.  
  12. echo $test; // returns "test2"



FullOfQuestions wrote:
Also, if a function alters either value, will this new value hold true outside the function?


if you do it like the example above, yes. :)

FullOfQuestions wrote:
Finally, if I use define() to make a constant outside of a function, will that constant be available inside a function?


Yes.

Also, you should read this, it talks all about variable scope:

http://us3.php.net/manual/en/language.v ... .scope.php
  • Rabid Dog
  • Cheese Monkey
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3188
  • Loc: South Africa
  • Status: Offline

Post July 28th, 2005, 11:12 pm

you could also pass the values by reference
My Software Development Company
Music I have recorded (fixed now :))
Image

Post Information

  • Total Posts in this topic: 12 posts
  • Users browsing this forum: PolishHurricane and 220 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© Unmelted Enterprises 1998-2009. Driven by phpBB © 2001-2009 phpBB Group.