poolhustler86 Posted February 28, 2008 Share Posted February 28, 2008 <?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 More sharing options...
The Little Guy Posted February 28, 2008 Share Posted February 28, 2008 make them one array: <?php $id_array = array("1"=>"200", "2"=>"300", "3"=>"250"); foreach ($id_array as $id => $qty) { echo "ID: " . $id . " QTY: " . $qty . " "; } ?> Link to comment https://forums.phpfreaks.com/topic/93593-why-does-this-not-work/#findComment-479608 Share on other sites More sharing options...
poolhustler86 Posted February 28, 2008 Author Share Posted February 28, 2008 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. Link to comment https://forums.phpfreaks.com/topic/93593-why-does-this-not-work/#findComment-479615 Share on other sites More sharing options...
The Little Guy Posted February 28, 2008 Share Posted February 28, 2008 then you do something 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); ?> Link to comment https://forums.phpfreaks.com/topic/93593-why-does-this-not-work/#findComment-479619 Share on other sites More sharing options...
poolhustler86 Posted February 28, 2008 Author Share Posted February 28, 2008 once i do that... how would i pull out the id value and the qty value.. so i can insert it into a database if qty does not equal 0 or empty! Link to comment https://forums.phpfreaks.com/topic/93593-why-does-this-not-work/#findComment-479621 Share on other sites More sharing options...
The Little Guy Posted February 29, 2008 Share Posted February 29, 2008 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 . " "; } ?> Link to comment https://forums.phpfreaks.com/topic/93593-why-does-this-not-work/#findComment-479655 Share on other sites More sharing options...
fireMind Posted February 29, 2008 Share Posted February 29, 2008 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] Link to comment https://forums.phpfreaks.com/topic/93593-why-does-this-not-work/#findComment-479656 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.