...

View Full Version : PHP Paramenters



macwiz
07-15-2008, 09:06 PM
If I have a page:
order.php

and I need the options:
order.php?price=10
order.php?price=20
order.php?price=30
ect...

Where do I store those parameters?

Thanks.

kbluhm
07-15-2008, 09:36 PM
Where would you like them to be stored?

Fou-Lu
07-15-2008, 09:40 PM
YOU don't need to store them at all, PHP does that work for you. Anything after the '?' and separated by an & indicates a key=value pair thats stored in the _GET superglobal.


<?php

$price = 0.0;
if (isset($_GET['price']) || !empty($_GET['price'])
{
$price = (double)$_GET['price'];
}

?>

This is how I would recommend retrieving values. Two things are important - the first is that it exists (isset($_GET['price'])) checks that, and the second is that its the correct data-type, I assumed that the price could be a double. Casting that will store the value as a double and corrupt the $price if it cannot be parsed (leaving you with 0.0). You're validation later can handle that.
But as I mentioned, you don't need to retrieve these at all, $_GET will still hold the value of price for the entire page load, and since its superglobal you can use it in the scope of a function call without passing the parameter in or needing to globalize the $price variable.

If you just support the 3 options (10, 20 & 30), I'd change the above. Instead of seeking for a double, I would seek for an int that is in that array:


<?php

$validPrice = array(10, 20, 30);
if (!isset($_GET['price']))
{
throw new Exception('Price is required!');
}
if (in_array((int)$_GET['price'], $validPrice))
{
// Do something since its valid
}
?>

Does this answer you're question?

macwiz
07-15-2008, 11:00 PM
Maybe you can take a look here. I have something else on this, but I explained what I needed better.

http://codingforums.com/showthread.php?t=144295

Thanks.

Fou-Lu
07-15-2008, 11:21 PM
This is really up to how you want to do it, but you simply need to use conditional checks on you're price variable.


<?php

if (!isset($_GET['price']))
{
die ('No price has been selected!');
}

switch ($_GET['price'])
{
case 10:
echo "Here is the information about price 10!";
break;
case 20:
echo "Here is the information about price 20!";
break;
case 30:
echo "Here is the information about price 30!";
break;
default:
die('Invalid price specified');
}

?>


I cannot really give you a whole pile of help on this since I am not familiar with you're scripts, requirements, or variable handling. All I can really offer is that you need to logically break you're pages up into what you need them to be, and specify you're calls with conditional checks - ifs or switches. Depending on the implementation, I cannot even guarantee that conditions are what you are looking for, it may be used for conditions as a part of a query.

I'm sorry, but without understanding what you are doing I cannot offer much other help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum