trouble $_GETting value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

trouble $_GETting value

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

not param1=value1?param2=value2
but param1=value1&param2=value2
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

The question rather is: Why do you have && instead of &?
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

This:

Code: Select all

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

Code: Select all

isset($_GET['subcat'])
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post 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! =)
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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()
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post 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.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

De nada
Post Reply