NameSilo

How to do a diff query for each value in a loop

Spaceship Spaceship
Watch
Okay I'm having a bit of a problem. (notice it's 5am :o )

I have a query that loops through a max of 5 values. Then within that loop, I need to update 5 different fields with those different values. So essentially I need to run a different query for each value. (I think)

Currently have it as: (which of course doesn't work)
Code:
if($recipient2 =="0" && $recipient1 != $uid){ query2 }elseif($recipient3 =="0"  && $recipient2 != $uid && $recipient1 != $uid ) { query3 }etc etc

Where $uid contains the 5 different values and $recipient1 through $recipient5 need to be filled.

Any ideas? I lack the fundamentals of php but I can't find the answer to how I should structure this.

Many thanks,
-Ryan
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
I dont know if I understood you right.

$uid is an array with 5 different values and you need to assign each value to a different field in the database?
 
0
•••
$uid is an array with 5 different values and you need to assign each value to a different field in the database?

Yeah I can make it an array, its currently a while loop.

(long join query)
while ($row = mysql_fetch_array($find_them)){
$uid = $row['uid'];

I need to insert the first instance/loop of uid into $recipient1 insert query, second instance/loop into $recipient2 etc etc


Thanks,
-Ryan
 
0
•••
$i=1;
foreach($uid as $value){

$sql= "UPDATE table SET recipient$i = $value ..."
$i++;
...
}
 
1
•••
Something more like this, perhaps?

$i = 0;
while ($i < 5 && $row = mysql_fetch_array($find_them)){
$recipients[$i] = $row['uid'];
$i++;
}
//...
//...
//echo $recipients[0] . ", " . $recipients[1] ...
 
0
•••
Thanks guys

Almost there,

View Code

The UPDATE leads query within the foreach loops is running twice as much as it should. The $find_the query is producing 2 $uid's and the querys in the foreach are running 4 times, so the $recipient1 and 2 is set to the first $uid and recipient3 and 4 is set to the second $uid. Which is a big problem.

Any ideas?

Thanks,
-Ryan

---------- Post added at 06:05 PM ---------- Previous post was at 04:42 PM ----------

Okay, for those interested, here is how I got it to work:

for ($val1 = each($uid), $val2 = each($bid); $val1 || $val2;
$val1 = each($uid), $val2 = each($bid)) {

query goes here calling on the values like $val1[1] and $val2[1]

}
 
Last edited:
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back