Jump to content

why does this not work :(


poolhustler86

Recommended Posts

<?php

$id_array = array("1", "2", "3");

$qty_array = array("200","300","250");

 

foreach (($id_array as $id) && ($qty_array as $qty))

{

  echo "ID: " . $id . " QTY: " . $qty . "<br />";

}

?>

 

i get the following error...

 

Parse error: parse error, unexpected T_AS in C:\www\webroot\statrolls-pty-ltd\index.php on line 20

 

line 20 = the line with the foreach statement on it

Link to comment
https://forums.phpfreaks.com/topic/93593-why-does-this-not-work/
Share on other sites

thanks that works a treat... but say i have a form could have 1 row or 20 rows depending on the number of sizes... and i need to collect the id for that product, aswell as the qty..

 

html would be like this i guess?

 

<input name='id[]' type='hidden' value='1'><input name='qty[]' type='text' value=''>

<input name='id[]' type='hidden' value='2'><input name='qty[]' type='text' value=''>

<input name='id[]' type='hidden' value='3'><input name='qty[]' type='text' value=''>

 

or there could be

 

<input name='id[]' type='hidden' value='1'><input name='qty[]' type='text' value=''>

<input name='id[]' type='hidden' value='2'><input name='qty[]' type='text' value=''>

<input name='id[]' type='hidden' value='3'><input name='qty[]' type='text' value=''>

<input name='id[]' type='hidden' value='4'><input name='qty[]' type='text' value=''>

<input name='id[]' type='hidden' value='5'><input name='qty[]' type='text' value=''>

<input name='id[]' type='hidden' value='6'><input name='qty[]' type='text' value=''>

 

cause i will be submitting the data to a shopping cart if their is a qty.

Your two codes should look like this:

 

<?php
$ids = array();
foreach($_POST['id'] as $id){
     $ids[] = $id;
}
$qtys = array();
foreach($_POST['qty'] as $qty){
     $qtys[] = $qty;
}
$id_array = array_combine($ids,$qtys);
//$id_array = array("1"=>"200", "2"=>"300", "3"=>"250");

foreach ($id_array as $id => $qty)
{
  echo "ID: " . $id . " QTY: " . $qty . "
";
}
?>

Well, actually looks like you might want to use a method like:
[code]
<input type='hidden' name='id[0]' value='0' />
<input type='text' name='qty[0]' value='' />

<input type='hidden' name='id[1]' value='0' />
<input type='text' name='qty[1]' value='' />

<input type='hidden' name='id[2]' value='0' />
<input type='text' name='qty[2]' value='' />

 

Then once submitted you've got two nicely organized arrays of id's and quantities to work with:)

 

could do a foreach like...(assuming you're using POST as the method of transmission)

foreach ($_POST['qty']  as  $key => $value)
    $id_array[$_POST['id'][$key]] = $_POST['qty'][$key];

 

[/code]

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.