Page 1 of 1

trouble $_GETting value

Posted: Wed Jan 17, 2007 12:48 pm
by boo_lolly
i have a shopping cart where the admin can enter items in categories, and even subcategories of existing categories. this subcategory value needs to be retrieved from the URL... ex http://www.mysite.com/index.php?cat=12?subcat=85

where cat is the parent category, and subcat is the subcategory within category 12. i'm trying to write an if statement that will check to see if subcat is in the url, if it is... then do this, if not, do that... i thought i could use the $_GET[subcat] variable to do this, because it worked for my $_GET[cat] sql query. but it worked for $_GET[cat] when there was no subcat=? in the address bar... so i don't know what's going on or why it's not working. part of my code:

Code: Select all

<?php
        if(isset($_GET[subcat])){
                $displayType = 3;
        }else{

       /*
        *check to see if there is a subcategory:
        *while loop checks for a match of the id#
        *(which is the same value as cat=? in the browser)
        *in the 'parent' column of the categories
        *table in the database. this cat id number is
        *retrieves from the browser using the $_GET[cat] variable
        *if there IS a match, then that category has
        *subcategories, hence set $displayType to some
        *matching # within the switch statement to display
        */
        $sql = mysql_query("SELECT * FROM categories");
        while($row = mysql_fetch_array($sql)){
                if($row[parent] == $_GET[cat]){
                        $displayType = 1;
                }
        }

       /*
        *if no subcategories were found
        *switch to display the items within
        *parent category
        */
        if($displayType != 1){
                $displayType = 2;
        }

   }//end else statemnet

        switch($displayType){
                case 1:
                          /*do something*/
                break;

                case 2:
                         /*do something else*/
                break;

                case 3:
                        /*do something completely different*/
                break;
       }
?>
i guess my question is, why doesn't it work? and how do i check to see if subcat is there so can change $displayType for my switch() function?

Posted: Wed Jan 17, 2007 1:23 pm
by volka
not param1=value1?param2=value2
but param1=value1&param2=value2

Posted: Wed Jan 17, 2007 1:25 pm
by boo_lolly
volka wrote:not param1=value1?param2=value2
but param1=value1&param2=value2
thanks, that's what i thought. although i have seen param1=value1&&param2=value2 with 2 ampersands. does that make a difference?

Posted: Wed Jan 17, 2007 1:27 pm
by volka
The question rather is: Why do you have && instead of &?

Posted: Wed Jan 17, 2007 1:30 pm
by boo_lolly
volka wrote:The question rather is: Why do you have && instead of &?
i don't, i'm using one '&'. but i have seen others use &&. i don't know why.

Posted: Wed Jan 17, 2007 1:33 pm
by volka
There's no reason for && - don't use it.

try

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
echo '<pre>GET: '; var_export($_GET); echo "</pre>\n";
echo '<pre>query: '; var_export($_SERVER['QUERY_STRING']); echo "</pre>\n";

if( isset($_GET['subcat']) ){
	$displayType = 3;
}
else{
...
and check wether _GET contains what you expect.

Posted: Wed Jan 17, 2007 2:14 pm
by RobertGonzalez
This:

Code: Select all

isset($_GET[subcat])
should be this:

Code: Select all

isset($_GET['subcat'])

Posted: Wed Jan 17, 2007 3:35 pm
by Ollie Saunders
There is a PHP directive you can use to change the URL parameter symbol but, as the manual describes, '&&' wouldn't work with that. So my guess is where you see '&&', it's not PHP.

Posted: Thu Jan 18, 2007 10:19 am
by boo_lolly
Everah wrote:This:

Code: Select all

isset($_GET[subcat])
should be this:

Code: Select all

isset($_GET['subcat'])
thanks everah. that didn't affect my code, i finally got everything working perfectly. but i'm curious, why does there need to be single quotes? when is it ok, and why, to not use single quotes? why is it necessary? how does the parser evaluate the variable if it does/doesnot have the singlequotes?
what about in retrieving information from a database using mysql_fetch_array()?

Code: Select all

<?php
        while($row = mysql_fetch_array($query)){
                /*is this correct?*/
                echo $row[column];

                /*or is this correct?*/
                echo $row['column'];
        }
?>
ole wrote:There is a PHP directive you can use to change the URL parameter symbol but, as the manual describes, '&&' wouldn't work with that. So my guess is where you see '&&', it's not PHP.
thanks ole! =)

Posted: Thu Jan 18, 2007 10:37 am
by aaronhall
boo_lolly wrote: thanks everah. that didn't affect my code, i finally got everything working perfectly. but i'm curious, why does there need to be single quotes? when is it ok, and why, to not use single quotes? why is it necessary? how does the parser evaluate the variable if it does/doesnot have the singlequotes?
what about in retrieving information from a database using mysql_fetch_array()?
If it does not have single quotes, PHP will first assume you are referencing a constant, issue a notice, and then go searching for a key by that name.

Here's some more information on mysql_fetch_array()

Posted: Thu Jan 18, 2007 11:40 am
by boo_lolly
aaronhall wrote:
boo_lolly wrote: thanks everah. that didn't affect my code, i finally got everything working perfectly. but i'm curious, why does there need to be single quotes? when is it ok, and why, to not use single quotes? why is it necessary? how does the parser evaluate the variable if it does/doesnot have the singlequotes?
what about in retrieving information from a database using mysql_fetch_array()?
If it does not have single quotes, PHP will first assume you are referencing a constant, issue a notice, and then go searching for a key by that name.

Here's some more information on mysql_fetch_array()
you da man. i appreciate it.

Posted: Thu Jan 18, 2007 11:41 am
by aaronhall
De nada