Log in

View Full Version : How to $_GET this ?



antonyf
12-13-2011, 12:21 PM
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;

jscheuer1
12-13-2011, 04:03 PM
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:


$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:


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:


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.

djr33
12-13-2011, 06:31 PM
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.