Jump to content

Help with while loop


dizel247

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
Link to comment
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]
Link to comment
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]
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.