Jump to content

Loop Help please


nathanashton

Recommended Posts

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

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

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.