Crystal-Pixel Posted October 21, 2009 Share Posted October 21, 2009 I have been going in circles for 3 days now trying to figure out how to pass 2 variable values through a foreach statement that will let me run them through mysql. Its basically to update a quantities in a shoppingcart. The two values are, #1 the quantity #2 the product id. The problem I'm having is I am only getting ONE of the values to go through when I need both of them together so I can input the data correctly into the database. If I run 2 Foreach statements I end up with different qtys and the same productid.. I have no idea what else to do.. please help?? Here is a snippet.. foreach (array_merge($_POST['qty'], $_POST['favids']) as $key => $value) { echo '<br><span class="error"><b>Going to Script Number Two - QTY: '.$value.' FAV ID: '.$favids.'</b></span>'; $query = "UPDATE favorites set". " qty='".$key."'". " WHERE favid ='".$_POST['favids']."'"; echo 'FAVID: '.$key.'<br>'; echo 'QTY:'. $val.'<br>'; $result = mysql_query($query) or die ("Error in query: $query"); if ($result) { echo '<br><span class="error"><b>Product Successfully Updated!</b></span>'; } Link to comment https://forums.phpfreaks.com/topic/178433-for-each-statement-with-2-_posts/ Share on other sites More sharing options...
trq Posted October 21, 2009 Share Posted October 21, 2009 The problem really is more to do with why your arrays are created the way they are in the first place. They are called associative arrays because you can associate keys to values. Using two unrelated arrays is ridiculous. Link to comment https://forums.phpfreaks.com/topic/178433-for-each-statement-with-2-_posts/#findComment-941040 Share on other sites More sharing options...
sasa Posted October 21, 2009 Share Posted October 21, 2009 change foreach (array_merge($_POST['qty'], $_POST['favids']) as $key => $value) { to foreach (array_combine($_POST['favids'] ,$_POST['qty']) as $key => $value) { Link to comment https://forums.phpfreaks.com/topic/178433-for-each-statement-with-2-_posts/#findComment-941064 Share on other sites More sharing options...
Crystal-Pixel Posted October 21, 2009 Author Share Posted October 21, 2009 Okay I tried this to change it to array_combine, but now the script isn't even running Any ideas? - I need both values in order ot update the database properly, I need the Qty and the ProductId number.... this is comin from a shoppingcart where there are multiple qty text input fields that allow the user to modify there qty and press Update. Snippet of What i have so far: $qty = array($_POST['qty']); $favids = array($_POST['favids']); foreach (array_combine($qty, $favids) as $key => $value) { echo '<br><span class="error"><b>Going to Script - QTY: '.$qty.' FAV ID: '.$favids.'</b></span>'; $query = "UPDATE favorites set". " qty='".$qty."'". " WHERE favid ='".$favids."'"; echo 'FAVID: '.$favids.'<br>'; echo 'QTY:'. $qty.'<br>'; $result = mysql_query($query) or die ("Error in query: $query"); if ($result) { echo '<br><span class="error"><b>Product Successfully Updated!</b></span>'; } Link to comment https://forums.phpfreaks.com/topic/178433-for-each-statement-with-2-_posts/#findComment-941210 Share on other sites More sharing options...
sasa Posted October 21, 2009 Share Posted October 21, 2009 can you post your form or print_r($_POST); Link to comment https://forums.phpfreaks.com/topic/178433-for-each-statement-with-2-_posts/#findComment-941218 Share on other sites More sharing options...
Crystal-Pixel Posted October 21, 2009 Author Share Posted October 21, 2009 Here is part of the form. It is within a "While" because it pulls multiple product ids out of the database. while ($row = mysql_fetch_array($result)) { $getproduct = "SELECT * FROM Products WHERE id = '".$row['productid']."'"; $getprod = mysql_query($getproduct) or die("Query Failed: ".mysql_errno()." - ".mysql_error()."<BR>\n$Query<BR>\n"); $pd = mysql_fetch_array($getprod); //Get SizeName $getsizes = "SELECT sizesname FROM Sizes WHERE id = '".$pd['Size']."'"; $getsize = mysql_query($getsizes) or die("Query Failed: ".mysql_errno()." - ".mysql_error()."<BR>\n$Query<BR>\n"); $finalsize = mysql_fetch_array($getsize); // $favid = $row['favid']; echo ' <tr> <td width="4%" bgcolor="#F7FAFD" class="blacktext"> <div align="center">'.$row['favid'].' <input name="favid" id="favid" type="radio" value="'.$row['favid'].'"> </div> </td> <td width="25%" bgcolor="#F7FAFD" class="blacktext">'.$pd['PartNum'].'</td> <td width="25%" bgcolor="#F7FAFD" class="blacktext">'.$pd['Name'].'</td> <td width="25%" bgcolor="#F7FAFD" class="blacktext">'.$finalsize['sizesname'].'</td> <td width="26%" bgcolor="#F7FAFD" class="blacktext"><input name="qty[]" type="text" class="tinyform" value ="'.$row['qty'].'" /><input name="favids[]" type="hidden" value="'.$row['favid'].'" /></td> <td width="4%" bgcolor="#F7FAFD" class="blacktext"> <div align="center"> <input name="checked[]" id="checked" type="checkbox" value="'.$row['favid'].'"> </div> </td> </tr> '; } } Link to comment https://forums.phpfreaks.com/topic/178433-for-each-statement-with-2-_posts/#findComment-941221 Share on other sites More sharing options...
sasa Posted October 21, 2009 Share Posted October 21, 2009 change form to echo ' <tr> <td width="4%" bgcolor="#F7FAFD" class="blacktext"> <div align="center">'.$row['favid'].' <input name="favid" id="favid" type="radio" value="'.$row['favid'].'"> </div> </td> <td width="25%" bgcolor="#F7FAFD" class="blacktext">'.$pd['PartNum'].'</td> <td width="25%" bgcolor="#F7FAFD" class="blacktext">'.$pd['Name'].'</td> <td width="25%" bgcolor="#F7FAFD" class="blacktext">'.$finalsize['sizesname'].'</td> <td width="26%" bgcolor="#F7FAFD" class="blacktext"><input name="qty['.$row['favid'].']" type="text" class="tinyform" value ="'.$row['qty'].'" />'.$row['qty'].'</td> <td width="4%" bgcolor="#F7FAFD" class="blacktext"> <div align="center"> <input name="checked[]" id="checked" type="checkbox" value="'.$row['favid'].'"> </div> </td> and on next page do foreach ($_POST['qty'] as $favid => $qty){ ... Link to comment https://forums.phpfreaks.com/topic/178433-for-each-statement-with-2-_posts/#findComment-941246 Share on other sites More sharing options...
Crystal-Pixel Posted October 21, 2009 Author Share Posted October 21, 2009 change form to echo ' <tr> <td width="4%" bgcolor="#F7FAFD" class="blacktext"> <div align="center">'.$row['favid'].' <input name="favid" id="favid" type="radio" value="'.$row['favid'].'"> </div> </td> <td width="25%" bgcolor="#F7FAFD" class="blacktext">'.$pd['PartNum'].'</td> <td width="25%" bgcolor="#F7FAFD" class="blacktext">'.$pd['Name'].'</td> <td width="25%" bgcolor="#F7FAFD" class="blacktext">'.$finalsize['sizesname'].'</td> <td width="26%" bgcolor="#F7FAFD" class="blacktext"><input name="qty['.$row['favid'].']" type="text" class="tinyform" value ="'.$row['qty'].'" />'.$row['qty'].'</td> <td width="4%" bgcolor="#F7FAFD" class="blacktext"> <div align="center"> <input name="checked[]" id="checked" type="checkbox" value="'.$row['favid'].'"> </div> </td> and on next page do foreach ($_POST['qty'] as $favid => $qty){ ... That Did it!! Thank you SO MUCH!!!! Link to comment https://forums.phpfreaks.com/topic/178433-for-each-statement-with-2-_posts/#findComment-941256 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.