Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: PHP Paramenters

  1. #1
    Regular Coder
    Join Date
    Jul 2008
    Posts
    195
    Thanks
    3
    Thanked 11 Times in 11 Posts

    PHP Paramenters

    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.
    Last edited by macwiz; 07-15-2008 at 08:24 PM.

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Where would you like them to be stored?

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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 Code:
    <?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 Code:
    <?php

    $validPrice 
    = array(102030);
    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?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    Regular Coder
    Join Date
    Jul 2008
    Posts
    195
    Thanks
    3
    Thanked 11 Times in 11 Posts
    Maybe you can take a look here. I have something else on this, but I explained what I needed better.

    New To PHP- Please Help

    Thanks.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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 Code:
    <?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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •