nuet maessen
nuet maessen

Reputation: 131

how to bind multiple variables to a variable on an efficient way

I have to bind multiple variables to another variable ( $products_output) Now i do it like this:

$products_output  = $product1_output;
$products_output .= $product2_output;
$products_output .= $product3_output;
$products_output .= $product4_output;
$products_output .= $product5_output;
// and so on till 50...

Is there a way i can do it more efficient then this? I need the variable $products_output in a mail function later and this variable should contain all the variables $product1_output till $product50_output

Update: more info about this question: I am reading the values with $_POST form a form like this:

$product1_output = '';
if(!empty($_POST['product1'])) {
$product1_output =    $product1.' '.$_POST['product1'].' pieces Price € '.number_format($_POST['product1']*$price[$product1], 2);
}
$product2_output = '';
if(!empty($_POST['product2'])) {
$product2_output = $product2.' '.$_POST['product2'].' pieces Price € '.number_format($_POST['product2']*$price[$product2], 2);
}
// and so on...

For using all those variables in a mail function, i thougth: bind them all together to just 1 variable like this:

$products_output  = $product1_output;
$products_output .= $product2_output;
// and so on...

So my question: lets say there a 500 products, each with different prices that i have to read fro the form and multiply with the price of the product, how can i do this in an efficient way without typing long lists of code?

This is piece of the form:

 <tr>
            <td><?php echo $product1 ?></td>
            <td>&euro; <?php echo number_format($prijs[$product1], 2); ?></td>
            <td><input type="text" name="product1" size="3" /></td>

        </tr>
        <tr>
            <td><?php echo $product2 ?></td>
            <td>&euro; <?php echo number_format($prijs[$product2], 2); ?></td>
            <td><input type="text" name="product2" size="3" /></td>
        </tr>

And i use this associative array with the list of products.

$product1 = 'Broodje Ham';
$product2 = 'Broodje Kaas';
$product3 = 'Broodje Gezond';
$product4 = 'Broodje Tonijn';
$product5 = 'Broodje Zalm';
// and so on...

//prices of products
$price = array(
                $product1 => 3.5, // prijs 3.50 
                $product2 => 3.5,
                $product3 => 3.5,
                $product4 => 4.5,
                $product5 => 4.5,
                // and so on...
 );

Upvotes: 1

Views: 84

Answers (6)

Devon Bessemer
Devon Bessemer

Reputation: 35357

You should try to source your variables from an array, so the $products_output should be an array containing all of the values.

One, you can use brackets in the form input names to start off with an array:

<input type="text" name="product[1]" size="3" />

So now you will have an array at $_POST['product'] rather than ~50 different variables.

You can then loop through this array to create an output array using the same key (1...50).

foreach ($_POST['product'] as $key => $product) {
  $products_output[$key] = "...";
}

Now when you need to use the output array, you can loop through that.

foreach ($products_output as $product) {
  echo $product."\n";
}

Upvotes: 0

Konstantin
Konstantin

Reputation: 25359

You can do something like this

$var_prefix = 'products';
$var_suffix = '_output';
$var_name = '';
for($i=1; $i<=50; $i++)
{
    $var_name = $var_prefix.$i.$var_suffix;
    $products_output .= $$var_name;
}

It uses variables variable, but you really should avoid this in your code. Presence of variables $products1_output, $products2_output, ..., $products50_output indicates, that there are some design problems in your code. You might want to use arrays instead of maintining a bunch of variables with similar names.

Demo

Upvotes: 0

Pedro Lobito
Pedro Lobito

Reputation: 99001

Using ${} is a way to create dynamic variables:

for($i=1;$i<=50;$i++){
$products_output .= ${"product".$i."_output"};
}

DEMO

Upvotes: 1

Pravat Kumar Sahoo
Pravat Kumar Sahoo

Reputation: 303

You can write your code as follows.

$products_output = "";

for($i=1;$i<=50;$i++){

$prodVar = "product".$i."_output";

$products_output .= $$prodVar;

}
echo $products_output;

Upvotes: 1

Ari Djemana
Ari Djemana

Reputation: 1249

you are looking for array

Example:

$products_output[1] = $product1_output;
$products_output[2] = $product2_output;

..

if you want to concate all value from array, you can use implode

if you want to parse each variable into array you can use loop for

Upvotes: 0

user1433049
user1433049

Reputation:

Something like:

$products_output = '';
for($i = 1; $i<=50;$i++) {
    $prodx_output = 'product'.$i.'_output';
    $products_output .= $$prodx_output;
 }

Upvotes: 0

Related Questions