0

There are maximum 8 <select name="a"> and I need to store these values in an array

$a = $_POST['a'];
$b = $_POST['b'];

if (isset($a, $b)) {
    foreach ($products as $thisProduct) {
        if ($thisProduct->getId() == $a) {
        //do something & store in array
            switch($b){
                case "one":
                //do something
                break;

                case "two":
                //do something
                break;

                case "three":
                //do something
                break;
            }
        }
    }
}
//call array and make array_sum()...

How do I store these in an array so I could use this array for making calculations?

UPDATE

There are also 8 input fields with an amount. This input multiply with the value of $a.

foreach ($products as $thisProduct) {

    foreach ($a as $value) {
        if ($thisProduct->getId() == $value) {
            $multiply = ($thisProduct->getMultiply($amount));
            array_push($array, $multiply);  
        }
    }
}

How can I link each input to the select name="[a]"?

4
  • 3
    use <select name="a[]">. it will convert post variable into array Commented Jul 17, 2015 at 9:19
  • 2
    Of topic: Instead of multiple if statements, can't you use switch statement ? A Commented Jul 17, 2015 at 9:22
  • @krishna and how do I store other variables in the array? Commented Jul 17, 2015 at 9:28
  • @TismonVarghese That would be better, thanks Commented Jul 17, 2015 at 9:29

1 Answer 1

1

Use <select name=a[]> and use this code

$a = $_POST['a'];
foreach($a as $value){
   // do something
}
Sign up to request clarification or add additional context in comments.

2 Comments

and how do you use it with another for each?
$a is an array your <select> variables

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.