Home > Archive > PHP Language > March 2006 > Multidimensional Array Problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Multidimensional Array Problem
|
|
| info@oceanstudio.co.uk 2006-03-14, 6:56 pm |
| I am attempting to create a form that allows me to change details on
certain products such as price etc.
In the form I have 4 textfields that are repeated for however many
products are in a particular category. The textfields contain:
cap_id - the index value field
price - the price of the product
percentage_discount - the percentage discount to put into the product
record
monetary discount - the amount of money to put into the product record
set_price - the price to override the percentage and monetary amount to
display on the website.
Each of the textfields is defined using the [] to create an array to
access from the post variables.
On the page that the form submits to I've managed to get the 4 values
for each record into seperate arrays using implode and explode:
<?php
$cap_id_to_update = implode(' ', $_POST['cap_id_']);
$cap_id_array = explode(' ', $cap_id_to_update);
$set_price_to_update = implode(' ', $_POST['set_price']);
$set_price_array = explode(' ', $set_price_to_update);
$percentage_discount_to_update = implode(' ',
$_POST['percentage_discount_']);
$percentage_array = explode(' ', $percentage_discount_to_update);
$monetary_discount_to_update = implode(' ',
$_POST['monetary_discount_']);
$monetary_array = explode(' ', $monetary_discount_to_update);
?>
I want to get the arrays into one multidimensional array so that I can
loop through the array and output suitable mysql update statements that
can be executed to update the table.
I basically need it to be of the format:
<?php
$updatestatements = array(
"Field[n]" => array ("$cap_id", "$percentage_discount",
"$monetary_discount", "$set_price"),
)
// output the mysql commands
foreach (---- => -----){
echo($updatestatement);
// create the mysql update sql and execute
@mysql_query($updatestatement)
}
?>
Where n is next empty array value and
<?php
$cap_id
$percentage_discount
$monetary_discount
$set_price
?>
are the appropriate values from the arrays created.
I would (hopefully) assume that it's a case of looping using foreach
through the array 4 times and inserting the correct value into the
correct array section.
But after much hair pulling out I'm still no closer to a solution.
Anyone got any ideas?
| |
| ZeldorBlat 2006-03-14, 6:56 pm |
|
info@oceanstudio.co.uk wrote:
> I am attempting to create a form that allows me to change details on
> certain products such as price etc.
>
> In the form I have 4 textfields that are repeated for however many
> products are in a particular category. The textfields contain:
>
> cap_id - the index value field
> price - the price of the product
> percentage_discount - the percentage discount to put into the product
> record
> monetary discount - the amount of money to put into the product record
> set_price - the price to override the percentage and monetary amount to
> display on the website.
>
> Each of the textfields is defined using the [] to create an array to
> access from the post variables.
>
> On the page that the form submits to I've managed to get the 4 values
> for each record into seperate arrays using implode and explode:
>
> <?php
>
> $cap_id_to_update = implode(' ', $_POST['cap_id_']);
> $cap_id_array = explode(' ', $cap_id_to_update);
>
> $set_price_to_update = implode(' ', $_POST['set_price']);
> $set_price_array = explode(' ', $set_price_to_update);
>
> $percentage_discount_to_update = implode(' ',
> $_POST['percentage_discount_']);
> $percentage_array = explode(' ', $percentage_discount_to_update);
>
> $monetary_discount_to_update = implode(' ',
> $_POST['monetary_discount_']);
> $monetary_array = explode(' ', $monetary_discount_to_update);
>
> ?>
>
> I want to get the arrays into one multidimensional array so that I can
> loop through the array and output suitable mysql update statements that
> can be executed to update the table.
>
> I basically need it to be of the format:
> <?php
> $updatestatements = array(
> "Field[n]" => array ("$cap_id", "$percentage_discount",
> "$monetary_discount", "$set_price"),
> )
> // output the mysql commands
> foreach (---- => -----){
> echo($updatestatement);
> // create the mysql update sql and execute
> @mysql_query($updatestatement)
> }
>
> ?>
> Where n is next empty array value and
> <?php
> $cap_id
> $percentage_discount
> $monetary_discount
> $set_price
> ?>
> are the appropriate values from the arrays created.
>
> I would (hopefully) assume that it's a case of looping using foreach
> through the array 4 times and inserting the correct value into the
> correct array section.
>
> But after much hair pulling out I'm still no closer to a solution.
>
> Anyone got any ideas?
Why not try something like this:
$bigArray = array();
foreach($_POST['cap_id_'] as $i => $cap_id)
$bigArray[] = array($_POST['cap_id_'][$i], $_POST['set_price'][$i],
$_POST['percentage_discount_'][$i], $_POST['monetary_discount_'][$i]);
| |
| info@oceanstudio.co.uk 2006-03-14, 9:55 pm |
| That's great - how do I loop through the array to create the mysql
statements?
I thought it'd be something like:
<?
foreach($bigArray as $i => $cap_id){
foreach($cap_id as $record){
$updatestatement =
"UPDATE CapDer SET
percentage_discount = ".$record[2]. "
monetary_discount = ".$record[3]. "
setprice = ".$record[1]. "
WHERE cap_id = ".$record[0];
echo($updatestatement."<br>");
}
}
?>
But this just seems to write out the first second third letter of the
first array entry.
| |
| ZeldorBlat 2006-03-14, 9:55 pm |
|
info@oceanstudio.co.uk wrote:
> That's great - how do I loop through the array to create the mysql
> statements?
>
> I thought it'd be something like:
> <?
> foreach($bigArray as $i => $cap_id){
>
> foreach($cap_id as $record){
> $updatestatement =
> "UPDATE CapDer SET
> percentage_discount = ".$record[2]. "
> monetary_discount = ".$record[3]. "
> setprice = ".$record[1]. "
> WHERE cap_id = ".$record[0];
>
> echo($updatestatement."<br>");
> }
> }
> ?>
> But this just seems to write out the first second third letter of the
> first array entry.
Close. You only need one for loop. Get rid of the inner foreach and
change the outer one to this:
foreach($bigArray as $record) {
$updatestatement = "..."; //same as you had it
}
| |
| info@oceanstudio.co.uk 2006-03-14, 9:55 pm |
| Dude - your a life saver - thank you very much.
|
|
|
|
|