NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page How to do a diff query for each value in a loop

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search
5 members in live chat ~  


Reply
 
LinkBack Thread Tools
Old 10-19-2009, 02:15 AM THREAD STARTER               #1 (permalink)
Senior Member
 
DJ-Sound's Avatar
Join Date: May 2004
Posts: 1,812
DJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud of
 



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


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

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.
????: NamePros.com http://www.namepros.com/programming/617511-how-do-diff-query-each-value.html

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 by DJ-Sound; 10-19-2009 at 02:20 AM.
DJ-Sound is offline   Reply With Quote
Old 10-19-2009, 02:27 AM   #2 (permalink)
NamePros Member
 
adamo's Avatar
Join Date: Aug 2008
Location: poland
Posts: 94
adamo is an unknown quantity at this point
 




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?
__________________
adamo is offline   Reply With Quote
Old 10-19-2009, 02:33 AM THREAD STARTER               #3 (permalink)
Senior Member
 
DJ-Sound's Avatar
Join Date: May 2004
Posts: 1,812
DJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud of
 



Originally Posted by adamo View Post
$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
DJ-Sound is offline   Reply With Quote
Old 10-19-2009, 02:54 AM   #4 (permalink)
NamePros Member
 
adamo's Avatar
Join Date: Aug 2008
Location: poland
Posts: 94
adamo is an unknown quantity at this point
 




$i=1;
foreach($uid as $value){

$sql= "UPDATE table SET recipient$i = $value ..."
$i++;
...
}
__________________
adamo is offline   Reply With Quote
Old 10-19-2009, 06:10 AM   #5 (permalink)
NamePros Regular
 
Jim_'s Avatar
Join Date: Aug 2005
Location: NY, USA
Posts: 608
Jim_ is a splendid one to beholdJim_ is a splendid one to beholdJim_ is a splendid one to beholdJim_ is a splendid one to beholdJim_ is a splendid one to beholdJim_ is a splendid one to behold
 


Save The Children
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] ...
__________________
ask me about the internet
Jim_ is offline   Reply With Quote
Old 10-19-2009, 03:05 PM THREAD STARTER               #6 (permalink)
Senior Member
 
DJ-Sound's Avatar
Join Date: May 2004
Posts: 1,812
DJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud ofDJ-Sound has much to be proud of
 



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:
????: NamePros.com http://www.namepros.com/showthread.php?t=617511

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 by DJ-Sound; 10-19-2009 at 01:58 PM.
DJ-Sound is offline   Reply With Quote
Reply

Tags
joins, loops, mysql, php


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 02:31 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger