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!
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:
<?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?
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 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()?
<?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.
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.
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.