Reputation: 600
I use the following code to strip the product number from a page url so that i can query a database to check for a custom canonical link.
$qs = explode('&',$_SERVER['QUERY_STRING']);
$prodid = explode('=',$qs[0]);
$prod_ID = $prodid[count($prodid)-1];
As an example, $qs contains Array ( [0] => products_id=1121 [1] => cPath=180_182 [2] => )
I then decided i also needed to be able to specify custom canonicals for the categories as well so i added the following code:
$catid = explode('=',$qs[1]);
$lastnmbr = explode('_',$catid[1]);
$cat_ID = $lastnmbr[count($lastnmbr)-1];
This gives me $prod_ID of 1121 and $cat_ID of 182.
However, here is the problem. If i drop back to the product list rather than a specific product page the array held in $qs becomes Array ( [0] => cPath=180_182 [1] => ) which means that
$catid = explode('=',$qs[1]);
is no longer the correct array position to collect the category id and in fact it is collected by
$prodid = explode('=',$qs[0]);
which makes all the following code think it's a product.
I think i need to somehow use the information held before the = symbol to ensure that if it says cPath it assigns it to $catid and if it says products_id it assigns it to $prodid.
Is there a "correct" way to do this? I did try making a new var of
$catchk = explode($qs[0], '=');
thinking this would return the data BEFORE the = symbol but it didn't so i obviously have the wrong end of the stick here.
To make the solution clear, i used the suggestion from Tyr (Thank you) and finished up with
$catid = $_GET['cPath'];
$lastnmbr = explode('_',$catid);
$cat_ID = $lastnmbr[count($lastnmbr)-1];
$prod_ID = $_GET['products_id'];
It was necessary to be able to strip the set of numbers following the last _ (underscore) as subcategories are shown as 180_182 etc
Upvotes: 0
Views: 63
Reputation: 2810
What if you trying $_GET['product_id'] and $_GET['cPath'] instead of $_SERVER['QUERY_STRING'] usage?
You are avoiding the problems of having the correct array path.
Upvotes: 3