|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
||||
|
||||
|
Foreach and associative arrays
I have two sets of query data, a list of active users and a list of unpublished news. I am looping through the users as follows:
Code:
$query_usr = 'select * from email_test where bactive = \'t\'';
$res_usr = pg_query($db_conn, $query_usr);
$query_news = 'select * from newsclips where bsent = \'f\'';
$res_news = pg_query($db_conn, $query_news);
$arr_news = pg_fetch_array($res_news, NULL, PGSQL_ASSOC);
while($arr_usr = pg_fetch_array($res_usr, NULL,PGSQL_ASSOC))
{
if(preg_match('/@/', $arr_usr['suser']))
$user = $arr_usr['suser'];
else
$user = $arr_usr['suser'].'@dri.edu';
$pmail = popen('/usr/ucb/Mail -s DRI_News_Clips_Mailing '.$user.'', 'w');
if($pmail)
// need to loop through arr_news results here and build up body for email (fed back to popen via fprintf)
}
Would foreach be a possible route to go here? I problem I have w/ foreach and associative arrays is that foreach steps through each column when I am only interested in the values of specific columns for each row. How can I extract this data in this fashion inside the loop above? pclose($pmail); |
|
#2
|
||||
|
||||
|
I don't think you can use the foreach() for what you want since a definition of the foreach is:
"The PHP control statement, foreach{}, is a handy way to cycle through the elements of an array and perform the same operations on each." So it will cycle through everything that the array contains. Thats how i understand it. Can I ask, whats wrong with the while?(I assume the above is working, since you didn't say)Why do you want to use foreach? |
|
#3
|
||||
|
||||
|
I was trying to find a cleaner way than nesting while statements but you are right I have that working fine here is the code:
Code:
while($arr_usr = pg_fetch_array($res_usr, NULL,PGSQL_ASSOC))
{
if(preg_match('/@/', $arr_usr['suser']))
$user = $arr_usr['suser'];
else
$user = $arr_usr['suser'].'@dri.edu';
$pmail = popen('/usr/ucb/Mail -s News_Clips_Mailing '.$user.'', 'w');
while($arr_news = pg_fetch_array($res_news, NULL, PGSQL_ASSOC))
{
if($pmail)
fprintf($pmail, $arr_news['soverview']."\n\n\n"); }
pclose($pmail);
pg_result_seek($res_news, 0);
}
- Nick |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > PHP Development > Foreach and associative arrays |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|