Jump to content

Array and Looping question?


Solarpitch

Recommended Posts

Hey,

 

I have the following array that will run the function for each value of val[]. This part is fine and works grand. It will call the function inserting both values into it. $k being the id and $v being the value of the textfield.

 

The only problem is I have another array that I need to include ... $_POST['total']... and pass the values into the function also.

 

<?php

//First function and array

  foreach ($_POST['val'] as $k => $v)
	 {
 	if ($v != '' && $k != 'submit') {

	echo confirm_order($k, $v);

	}
 }

//Second array

  foreach ($_POST['total'] as $k => $v)
	 {
 	if ($v != '' && $k != 'submit') {


	}
 }


//Here's what I was trying but its wrong... but you can see the logic of what I'm trying to do

foreach (($_POST['val'] as $k => $v) && foreach ($_POST['total'] as $a => $b))
{
     if ($v != '' && $k != 'submit') {

         echo confirm_order($k, $v, $a, $b);

     }

}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/119811-array-and-looping-question/
Share on other sites

Do the arrays $_POST['val'] and $_POST['total'] have the same length?

 

Maybe it would help to rearrange your form data (assuming that 'val' and 'total' make up one row), structuring it into rows instead of columns.

 

An example would help.

 

Ok

 

<?php

//This is a snippet of where the textfields are being first called. Their dynamic based on the results being returned from the databse. Just in the image below
....

<td><input type='text' name='val[".$row[0]."]' id='quantity' /></td>
<td><input type='text' name='total[".$row[0]."]' id='total'/></td>


?>

 

This is what the form looks like..

 

example50869.gif

 

Confirm_Order() takes 2 parameters at the min, but I need it to take 4 just like in my efforts on the first post. So basically as in the image above I want to get all the values of the quantity and total fields and pop them into the function like I have already done with just the quantity.

Yes they have the same amount of values. As you can see I will store all the quantities of each product and all the totals of each products in the array I have...

 

$_POST['val'] and $_POST['total']

 

I then want to loop through them like I'm doing for $_POST['val'] and insert the values into the function. But now, I need to also get the values from $_POST['total'] and include them into the function. Hope this makes sence

Good. Try to make the form appear like this:

 

<input name="rows[1][value]" id='value_1' type="text"/>
<input name="rows[1][total]" id='total_1' type="text"/>

 

Your $_POST will then look like this:

 

Array (
  [rows] => Array (
    [1] => Array (
      [value] => 10.00
      [total] => 55.00
    )
    [2] => Array (
      [value] => ...
      ...
    )
  )
)

 

Then it would be easier to loop through all elements:

 

foreach ($_POST['rows'] as $row) {
  echo confirm_order($row[value],$row[total],...);
}

 

How can I pass the id of the array in also with the values... as the id of the array will be the product id

 

<input name="rows[1][value]" id='value_1' type="text"/>
<input name="rows[1][total]" id='total_1' type="text"/>

...

name='rows[$id][value]'
name='rows[$id][total]'

so needs to be something like

foreach ($_POST['rows'] as $row) {
echo confirm_order($row[id_here][value],$row[id_here][total],...);
}

is that possible?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.