3

I'm using an online shopping cart which takes items ordered and passes their unique ID through the URL to a process.php page. So the URL would look something like

process.php?code_1=231&code_2=532&code_3=342

Another code_x=xxx is generated for each item ordered. On the process.php page, how would I create an array to get the values of the code_x without knowing how many items are ordered?

4 Answers 4

8

This problem is much better solved by changing the names of the elements in your form to code[].

For example, where you now have let's say

<input type="text" name="code_1" ... />
<input type="text" name="code_2" ... />
<input type="text" name="code_3" ... />

You would change that to

<input type="text" name="code[]" ... />
<input type="text" name="code[]" ... />
<input type="text" name="code[]" ... />

After doing this, $_GET['code'] will be an array which contains all the values from the text boxes as its items.

Update:

If you cannot control the names of the incoming parameters, you need to parse manually. Here's how I would do it:

// Sample data
$get = array('code_1' => 'foo', 'code_2' => 'bar', 'code_X' => 'X', 'asdf' => 'X');
$codes = array();

foreach($get as $k => $v) {
    // Reject items not starting with prefix
    if (substr($k, 0, 5) != 'code_') {
        continue;
    }

    // Reject items like code_X where X is not all digits
    $k = substr($k, 5);
    if (!ctype_digit($k)) {
        continue;
    }

    $codes[$k] = $v;
}

print_r($codes);
Sign up to request clarification or add additional context in comments.

3 Comments

I agree, and if I created the 'cart' myself, this is how I would have done it. Rather than $_GET['code'] I would have posted the data. I just need to get something up quickly, and then improve on it from there. Thanks for the help!
@Fleppar: I added an alternative for this situation.
With the update, I'll put you as the correct answer since you better explained what you should do AND how to do it if you have no other option. Thanks for the help.
4

It would be much better to use an array like Jon suggested.

It also would be cleaner to not use get for this. But rather post.

However if you really want to go this route you could do:

foreach($_GET as $name=>$value) {
    if (strpos($name, 'code_') !== 0) continue;

    // here are the names and values of the items
}

However again: I would not recommend it.

2 Comments

I agree, I am using jcart and without heavy modification to the code itself - it seems like the quickest way to get it going. I do not really need anything secure right away as the website is closed only to internal employees. I will take your advice and eventually recode the cart and post data.
Your code match some characters too like "code_test" will match too. So it is better to use regular expression like "^code_[1-9]$" to get the exact filtering.
1
print_r($_GET);

foreach($_GET as $key=>$val)
{
    echo '<br />';
    echo $key.' : '.$val;

}

Comments

1

You have to iterate through $_GET and look for names.

foreach(!empty($_GET as $name=>$value)) {
    // Check for match names for your 
    if (preg_match('/^code_[1-9]$/', $name)) {
      // Do whatever with values
    }
}

1 Comment

ereg has been deprecated as of PHP 5.3.0. Use preg_match instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.