Results 1 to 3 of 3

Thread: How to $_GET this ?

  1. #1
    Join Date
    Aug 2011
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to $_GET this ?

    Hi.
    I am trying to $_GET the values from an url.
    My problem is i don't have the full variable.

    IE: &name=john
    $_GET['name']// outputs john. // is normal example

    but this variable is different. The variable is first built with javascript then passed to the url. The variable then has a numeric value added to the variable.

    here is snippet of url:

    &item_name_1=test_item&item_number_1=1&quantity_1=14&amount_1=99

    The variable for &item_name_1 is actually $item_name_ there is no numeric value. the value will come from the &item_number_1 BUT!! &item_number_1 the variable is $item_number_ without the numeric value??

    Here is the source source of how the variables are built:

    itemsString = itemsString +
    "&item_name_" + counter + "=" + item.name +
    "&item_number_" + counter + "=" + counter +
    "&quantity_" + counter + "=" + item.quantity +
    "&amount_" + counter + "=" + item.price +
    "&on0_" + counter + "=" + "Options" +
    "&os0_" + counter + "=" + optionsString;

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,926
    Thanks
    43
    Thanked 3,192 Times in 3,155 Posts
    Blog Entries
    12

    Default

    First off this is a bad idea from the beginning, at least it looks like it. By using a get and a form, regardless if it's generated by javascript or not, you are inviting your users to set their own price. Unless, or perhaps even if this is all on a ssl, there could be problems of that nature. You might want to set the price on the receiving page or even further along the chain.

    That said, I'm not sure what you want to do with this information on the receiving page. But this sort of thing works and can probably be adapted to your purposes:

    PHP Code:
    $pattern '/^(item_name_|item_number_|quanity_|amount_|on0_|os0_)\d+$/';
    foreach (
    $_GET as $key => $value){
        if(
    preg_match ($pattern$key$matches)){
            echo 
    "$matches[0] is: $value<br>";
        }

    It's here:

    PHP Code:
    echo "$matches[0] is: $value<br>"
    that you could adapt it. $matches[0] will be item_name_1 or 2, etc. or quantity_1 or 2, etc, or whatever, and $value will be its value. You could create a new array before hand and add to it in that section, or do other things, even multuple things with the data using as many lines as is required.

    Getting back to the above code as is though - say this receiving page is get_stuff.php and you navigate to it with:

    Code:
    get_stuff.php?item_name_1=bob&item_number_1=1&quantity_1=3&amount_1=$3.25&on0_1=whatever&os0_1=something&item_name_3=sally&item_number_3=3&quantity_3=2&amount_3=$4.75&on0_3=what&os0_3=somethingelse
    The output would be:

    item_name_1 is: bob
    item_number_1 is: 1
    amount_1 is: $3.25
    on0_1 is: whatever
    os0_1 is: something
    item_name_3 is: sally
    item_number_3 is: 3
    amount_3 is: $4.75
    on0_3 is: what
    os0_3 is: somethingelse
    Or maybe all you need is the number (#) in item_name_# and could go from there using if( isset($_GET['item_number_' . $x]) . . . type filters to get the other info if available. The code for that would be a little different and probably more cumbersome though.
    Last edited by jscheuer1; 12-13-2011 at 04:24 PM. Reason: add - Or maybe all . . .
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    Whatever logic you are using on the page that generates the URL should also be used on the page receiving that information.
    If you have variables that end in numbers, then I think a simple for loop will work for you-- loop through looking for whatever information was set at "variable+#" where # is the number.
    But as John said, there may be a better way to do this in general.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

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