nathanashton Posted April 14, 2010 Share Posted April 14, 2010 Hi Guys, really stuck with a loop at the moment. I have a table of items (pallets) with a location attached to them. I am setting up a stocktake system, where all the pallets are displayed. You select the location you are stocktaking, then checkbox the pallets that are actually there. I need a loop to run through all the pallets and update the DB depending on the stocktake location, and where the pallet acutally is at. This is where I'm at: $stocktakelocation = $_POST['lid;'] $sql = "SELECT * FROM pallets ORDER BY palletnumber ASC"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { $palletlocation = $row['location;'] $palletid = $row['palletid;'] // If pallet has been selected if (in_array($palletid, $_POST['stake))'] { // if pallets location doesnt match stocktake location if ($palletlocation != $stocktakelocation) { // MySQL Query to Move pallet to the stocktake location $result = mysql_query("UPDATE pallets SET location='$stocktakelocation' WHERE palletid = '$palletid'"); } elseif ($currentlocation == $locationid){ // do nothing, as pallet is where we expect it to be } } // If pallet has not been selected else { if ($palletlocation != $stocktakelocation) { // do nothing as it is probably in its correct location } elseif ($palletlocation == $stocktakelocation) { // Pallet is supposed to be in stocktake location, but isnt, so MySQL Query to move it to unknown $result = mysql_query("UPDATE pallets SET location='30' WHERE palletid = '$palletid'"); } } } Link to comment https://forums.phpfreaks.com/topic/198466-loop-help-please/ Share on other sites More sharing options...
Ken2k7 Posted April 14, 2010 Share Posted April 14, 2010 Maybe it's me, but is there something wrong here: if (in_array($palletid, $_POST['stake))'] Are the parentheses supposed to be there? Link to comment https://forums.phpfreaks.com/topic/198466-loop-help-please/#findComment-1041419 Share on other sites More sharing options...
nathanashton Posted April 14, 2010 Author Share Posted April 14, 2010 Maybe it's me, but is there something wrong here: if (in_array($palletid, $_POST['stake))'] Are the parentheses supposed to be there? My mistake..looks like a carry over from the copy paste.. Here is the correct code I am using $stocktakelocation = $_POST['lid']; $sql = "SELECT * FROM pallets ORDER BY palletnumber ASC"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { $palletlocation = $row['location']; $palletid = $row['palletid']; // If pallet has been selected if (in_array($palletid, $_POST['stake'])) { // if pallets location doesnt match stocktake location if ($palletlocation != $stocktakelocation) { // MySQL Query to Move pallet to the stocktake location $result = mysql_query("UPDATE pallets SET location='$stocktakelocation' WHERE palletid = '$palletid'"); } elseif ($currentlocation == $locationid){ // do nothing, as pallet is where we expect it to be } } // If pallet has not been selected else { if ($palletlocation != $stocktakelocation) { // do nothing as it is probably in its correct location } elseif ($palletlocation == $stocktakelocation) { // Pallet is supposed to be in stocktake location, but isnt, so MySQL Query to move it to unknown $result = mysql_query("UPDATE pallets SET location='30' WHERE palletid = '$palletid'"); } } } I'll try and edit the post to correct it.... If it helps, seems the above code works for the first pallet in the array, doesnt continue looping through the remainder. Link to comment https://forums.phpfreaks.com/topic/198466-loop-help-please/#findComment-1041421 Share on other sites More sharing options...
Ken2k7 Posted April 14, 2010 Share Posted April 14, 2010 In your UPDATE SQL, you are overwriting the variable $result so that when your while loop tries to fetch the next result, it's empty. Be careful about overwriting variables. It's a really common mistake that happens almost all the time when people ask for help. Link to comment https://forums.phpfreaks.com/topic/198466-loop-help-please/#findComment-1041430 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.