Jump to content

Archived

This topic is now archived and is closed to further replies.

dizel247

Help with while loop

Recommended Posts

Hello,

Can some one help me with while loop expression.

I am looping from the database email adresse.

This is how I do it.
//Select User Email from Database
$rsUserEmail = mysql_query("SELECT * FROM contacts WHERE carrier_id = '$cid' AND contacts.rates = '1'")
or die(mysql_error()); 
// store the record of the "example" table into $row
while($row = mysql_fetch_array($rsUserEmail)){
// Print out the contents of the entry
$email = $row['contact_email'];

echo "$email; ";
// Here I get result a@aol.com; b@aol.com; c@aol.com;
}

echo "Emails Will be $email";
//Here I get: Emails Will be c@aol.com

Now when I refer to the $emails I only get one email.
My question is.. How do I store loop in the variable. So I can refrence it anytime.
Also I need my result to be
a@aol.com; b@aol.com; c@aol.com <--- no ; at the end.

Thanks,

Roman

Share this post


Link to post
Share on other sites
You will need to use a differant variable name to store it all for later, plus you can use substr() to take off the last character.

[code]
<?php
//Select User Email from Database
$rsUserEmail = mysql_query("SELECT * FROM contacts WHERE carrier_id = '$cid' AND contacts.rates = '1'")
or die(mysql_error()); 
// store the record of the "example" table into $row
$allemails = '';//give it some value other wise php5 gives a notice
while($row = mysql_fetch_array($rsUserEmail)){
// Print out the contents of the entry
$email = $row['contact_email'];
     
echo "$email; ";
// Here I get result a@aol.com; b@aol.com; c@aol.com;
$allemails = $allemails.$email.';';
}
$allemails = $str = substr($allemails,0,strlen($allemails)-1);
echo '<br />';
echo $allemails;
?>
[/code]

Share this post


Link to post
Share on other sites
or use an array

[code]<?php
$rsUserEmail = mysql_query("SELECT contact_email FROM contacts
          WHERE carrier_id = '$cid'
          AND contacts.rates = '1'")
          or die(mysql_error());

$email = array();
while($row = mysql_fetch_array($rsUserEmail)){
    $email[] = $row['contact_email'];  // add to array
}

// when you want to use them
$allemails = join ('; ', $email);  //--> a@aol.com; b@aol.com; c@aol.com
?>[/code]

Share this post


Link to post
Share on other sites

×

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.