Jump to content

Problem with LOOPING


robert_gsfame

Recommended Posts

I wish to tell all members about ticket that is available. When registered, members are given an option to choose which destination (country) information they wish to get. If they chose more than one country then all will be stored into my database with xx,yy,zz format eg:

Brazil, United States, Mexico

 

 

<?php require_once('config.php');

 

 

$query1="SELECT * FROM ticket";

$sql=mysql_query("SELECT * FROM passenger LIMIT 2 OFFSET 0 ");

while($array1=mysql_fetch_assoc($sql)){

$subscountry1=$array1['subs_country'];

$subscountry2= explode(",", $subscountry1);

for($i = 0; $i < count($subscountry2); $i++){

$subscountry[$i] = $subscountry2[$i];

$query_action1=mysql_query($query1);

$num1=mysql_num_rows($query_action1);

if(!empty($num1)){

while($array=mysql_fetch_assoc($query_action1)){

$id=$array['id'];

$registration=$array['reg_no'];

$country=$array['country'];

 

$email=$array1['email'];

if($subscountry[$i]==$country){

echo $id."    ".$registration."     ".$country."     ";

}}}}

mail($email,"xxxxxx","xxxxxx","xxxxxx")."<br>";

}

 

?>

This will result : All registration number and country will appeared together with user email which is used for mail()

But the problem is that $email still appear (which means that mail() will be still executed)  although no records was found

 

Can anyone help me arrange this code, they must be wrongly looped

 

thanks

Link to comment
Share on other sites

firstly, please put your code in code tags. and for the love of god, format your code! You have 4 ending curly braces on 1 line!

 

Its very hard to read your code, but i did notice that your mail function is inside the while loop. is this how you intended it to be? besides that I don't see much wrong, but again, I can't really read the code

Link to comment
Share on other sites

I wish to tell all members about ticket that is available. When registered, members are given an option to choose which destination (country) information they wish to get. If they chose more than one country then all will be stored into my database with xx,yy,zz format eg:

Brazil, United States, Mexico

 

 

<?php require_once('config.php');

 

 

$query1="SELECT * FROM ticket";

$sql=mysql_query("SELECT * FROM passenger LIMIT 2 OFFSET 0 ");

while($array1=mysql_fetch_assoc($sql)){

$subscountry1=$array1['subs_country'];

$subscountry2= explode(",", $subscountry1);

for($i = 0; $i < count($subscountry2); $i++){

$subscountry[$i] = $subscountry2[$i];

$query_action1=mysql_query($query1);

$num1=mysql_num_rows($query_action1);

if(!empty($num1)){

while($array=mysql_fetch_assoc($query_action1)){

$id=$array['id'];

$registration=$array['reg_no'];

$country=$array['country'];

 

$email=$array1['email'];

if($subscountry[$i]==$country){

echo $id.$registration.$country;

}}}}

mail($email,"xxxxxx","xxxxxx","xxxxxx")."<br>";

}

 

?>

This will result : All registration number and country will appeared together with user email which is used for mail()

But the problem is that $email still appear (which means that mail() will be still executed)  although no records was found

 

 

I have to loop the email and the only problem is i don't want the email to appear if no records found

Please help me in solving this code!

which part is wrong and where it should be placed??

 

 

Link to comment
Share on other sites

I took the liberty of formatting your code.

<?php
$query1="SELECT * FROM ticket";
$sql=mysql_query("SELECT * FROM passenger LIMIT 2 OFFSET 0 ");
while($array1=mysql_fetch_assoc($sql)){
$subscountry1=$array1['subs_country'];
$subscountry2= explode(",", $subscountry1);
for($i = 0; $i < count($subscountry2); $i++){
	$subscountry[$i] = $subscountry2[$i];
	$query_action1=mysql_query($query1);
	$num1=mysql_num_rows($query_action1);
	if(!empty($num1)){
		while($array=mysql_fetch_assoc($query_action1)){
			$id=$array['id'];
			$registration=$array['reg_no'];
			$country=$array['country'];

			$email=$array1['email'];
			if($subscountry[$i]==$country){
				echo $id.$registration.$country;
			}
		}
	}
}
mail($email,"xxxxxx","xxxxxx","xxxxxx")."<br>";
}
?>

From what I see, it shouldn't mail anything if there were no records found in the database, as the mail function itself is inside the while loop (which won't run if there are no entries returned.

 

but, you should turn error reporting on. there is a syntax error here

mail($email,"xxxxxx","xxxxxx","xxxxxx")."<br>";

 

That doesn't even really make sense

Link to comment
Share on other sites

Oh, I didn't even look at your query. You are selecting everything from the table. As long as there is information in the table, there will be some data taken from the table. if you want to add some limitations on what is retrieved, then you need to use a WHERE clause in your query. Right now, your query is taking the first 2 rows from the table.

 

Your Loop is actually perfectly fine. If the query doesn't return any rows, than no emails will be sent

Link to comment
Share on other sites

... I told you, you need to change your query, because the query is ALWAYS retrieving 2 rows from table (assuming the table has data) If your table has data, then two emails will be sent. If you don't want emails to always be sent, then you need to add a WHERE clause to your query

Link to comment
Share on other sites

If you never try it, then it seems to be fine but once you try it then you will find that there must be something wrong with the loop as

 

second email always appear !!!!!

 

eg:    123-Australia     

        email@yahoo.com

        456-Brazil

        email@gmail.com

 

If there is a record then no problem with it, but when there is no record let say email@gmail.com never choose Brazil during registration n this will show up

 

        123-Australia     

        email@yahoo.com

        email@gmail.com --------------> I WANT TO KICK THIS STUPID OUT

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.