Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 Arrays?? 1

Status
Not open for further replies.

RENO9

Technical User
Dec 8, 2005
27
GB
I have 2 pages one displaying a form from a database, which allows a user to change the status of a job via a drop down box, see Page1.php code below
Page1.php
Code:
  	while($row = mysql_fetch_array($result))
	{
		echo "<tr align='center'>\n";
		extract($row);
		echo '<td>
				<select name="AwaitingStatus[]" class="body">
				<option value="">Change Status...</option>
				<option value="P">Awaiting Spare Parts</option>
				<option value="U">Awaiting Tools/Equipment</option>
				<option value="K">Awaiting Mod Kits</option>
				<option value="I">Awaiting Inspection</option>
				<option value="V">Awaiting Tech. Services</option>
				<option value="B">Awaiting Access</option>
				<option value="Q">Awaiting Other Staff/Jobs</option>
				<option value="F">Change of Priority</option>
				<option value="E">Card Completion</option>
				<option value="X">System Fault</option>
				<option value="Z">Awaiting Planning</option>
			  </select></td>';
		echo "<td>".$row['jobTitle']."</td>\n";
		echo "<input type='hidden' name='hidvar[]' value='$jobCode'";
		echo "</tr>\n";
	}
This code now posts the variables to Page2.php and processes it i.e.
Page2.php
Code:
	foreach ($AwaitingStatus as $JobStatus)   //Checks for Status Change
	{				
		foreach ($jobCode as $Code)
		{
			$sql3  = "UPDATE joblist SET LostTime = '$JobStatus' WHERE jobCode = '$Code'";
			$Code = mysql_query($sql3);
			echo $sql3.'<br>';
		}
		echo "<br />";
	}
The problem with this code is that it outputs,
Code:
UPDATE joblist SET LostTime = 'U' WHERE jobCode = 'CG162'
UPDATE joblist SET LostTime = 'U' WHERE jobCode = 'RS976'

UPDATE joblist SET LostTime = 'I' WHERE jobCode = 'CG162'
UPDATE joblist SET LostTime = 'I' WHERE jobCode = 'RS976'
where as i just want it to update
Code:
UPDATE joblist SET LostTime = 'I' WHERE jobCode = 'RS976'
UPDATE joblist SET LostTime = 'U' WHERE jobCode = 'CG162'
if i output the array structure and values i.e.
Code:
	print_r ($AwaitingStatus);
	print_r ($jobCode);
it returns
Code:
Array ( [0] => U [1] => I ) Array ( [0] => CG162 [1] => RS976 )
Im pretty sure the problem is within the foreach loop,
Code:
foreach ($AwaitingStatus as $JobStatus)
{
   foreach ($jobCode as $Code)
   {
   }
}
Any suggestions on how i can achieve this, without it looping again?

Thanks in Advance and i hope this makes sense.
 
instead of giving the select box a name "awaitingstatus[]" give it the name awaitingstatus[jobcode]

insert the job code programmatically in php.

then you just loop the awaiting status array
Code:
foreach ($awaitingstatus as $key =>$val):
 mysql_query = ("UPDATE joblist SET LostTime = '." $_POST['awaitingstatus'][$key] ."' WHERE jobCode = '$key'";
endforeach;
{haven't tested the code to make sure it's free of parse errors but you should ge the gist}
 
If I have understood your code right, you could use a 'for' loop instead of a foreach:
Code:
$count = count($AwaitingStatus);
for($i=0; $i<$count; $i++)
{
    $sql3 = "UPDATE joblist 
             SET LostTime = '$AwaitingStatus[$i]' 
             WHERE jobCode = '$jobCode[$i]'";
             
    $Code = mysql_query($sql3);
    echo $sql3.'<br /><br />';
}

HTH
 
Thanks for the quick responses,

Itshim, i have tried using it your way and it works ok if u change the status for every row but if you just change one row it resets the rows that were not set back to nothing? as the query returns
Code:
UPDATE joblist SET LostTime = '' WHERE jobCode = 'RS976'

Any suggestions?
Cheers
 
Sorry, you would need to add a check for that situation...
Code:
$count = count($AwaitingStatus);
for($i=0; $i<$count; $i++)
{
    //check that 'status' has some value
    if (!empty($AwaitingStatus[$i])) {
        $sql3 = "UPDATE joblist
                 SET LostTime = '$AwaitingStatus[$i]'
                 WHERE jobCode = '$jobCode[$i]'";
             
        $Code = mysql_query($sql3);
        echo $sql3.'<br /><br />';
    }
}
You may want to look at empty() in the PHP Manual to see what PHP considers empty.
 
Thanks Itshim that done the trick.
Have a star!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top