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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Arrays and Loops

Status
Not open for further replies.

WJProctor

Programmer
Joined
Jun 20, 2003
Messages
151
Location
GB
HI there ive got this code, as shown below and the idea is that it loops through a list of names, some of which may appear more than once, so it finds each individual name and then calls another routine with the name. For some reason my code doesnt seem to work, i think its something to do with the way i load the array. If you can help me out i would be very greatful. The code is as follows.

function EmailResponse($ForumID,$TopicID,$Username){
$db = mysql_connect ("localhost", "User", "Password") or die ('I cannot connect to the database becasue : ' . mysql_error());
mysql_select_db ("EBMISDB");
$sql = "SELECT * FROM Topics WHERE ForumID = '$ForumID' AND TopicID = '$TopicID';";
$query = mysql_query($sql,$db) or die ('Couldnt query');
$Topics = mysql_fetch_array($query);
if ($Topics['EmailResponse'] == "Yes"){
$sql = "SELECT * FROM Users WHERE Username = '$Topics[TopicStarter]';";
$query = mysql_query($sql,$db) or die ('Couldnt Query');
$Users = mysql_fetch_array($query);
$CallFunction = SendEmail("$Topics[Subject]","$Users[Username]","$Users","$Username");
}
$sql = "SELECT * FROM Posts WHERE ForumID = '$ForumID' AND TopicID = '$TopicID' AND EmailResponse = 'Yes';";
$query = mysql_query($sql,$db) or die ('Couldnt query');
$NamesArray = array();
while ($Posts = mysql_fetch_array($query)){
$Error = False;
$NamesArray[] = $Topic[TopicStarter];
for($n = 0; $n < count($NamesArray); $n++){
echo &quot;TopicStarter &quot; . $Topics[TopicStarter] . &quot; &quot;;
echo &quot;Poster &quot; . $Posts[Poster] . &quot; &quot; ;
if (($NamesArray[$n] == $Posts[Poster]) OR ($Posts[Poster] == $Topics[TopicStarter])){
$Error = True;
} else {
$NamesArray[] = $Posts[Poster];
}
}
if ($Error == False){
$sql = &quot;SELECT * FROM Users WHERE Username = '$Posts[Poster]';&quot;;
$query = mysql_query($sql,$db);
$Users = mysql_fetch_array($query);
$CallFunction = SendEmail(&quot;$Topics[Subject]&quot;,&quot;$Users[Username]&quot;,&quot;$Users[Email]&quot;,&quot;$Username&quot;);
}
}
}

function SendEmail($Subject,$Username,$EmailAddress,$Poster){
echo &quot;Sending Email to &quot; . $Username . &quot; / &quot;;
}
 
try replacing this line
$CallFunction = SendEmail(&quot;$Topics[Subject]&quot;,&quot;$Users[Username]&quot;,&quot;$Users&quot;,&quot;$Username&quot;);
- with this -
$CallFunction = SendEmail($Topics[&quot;Subject&quot;],$Users[&quot;Username&quot;],$Users[&quot;Email],$Username);

I think you where sending the string values of
&quot;$Topics[Subject]&quot;
&quot;$Users[Username]&quot;
&quot;$Users[Email]&quot;
&quot;$Username&quot;

and not the values held in them.

[img]http://www.welshtroll.co.uk/avatar/t_avatar.jpg[/img]
'... and then it wouldn't compile?'
 
The
Code:
foreach($myArray as $key=>$value)
is IMO the best way to loop trough associative arrays. There is no need to count etc.

Also:
Let MySQL do the data sorting and filtering.
Use the SQL statement to just retrieve each distinct e-mail, so you need not check for doubles:
Code:
SELECT distinct(email) FROM myTable WHERE myCondition
 
Thankyou very much, that was very helpful to me, i greatly appriciate ur time. JP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top