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
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.

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.