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 [email protected]; [email protected]; [email protected];
}

echo "Emails Will be $email";
//Here I get: Emails Will be [email protected]

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
[email protected]; [email protected]; [email protected] <--- no ; at the end.

Thanks,

Roman
Link to comment
https://forums.phpfreaks.com/topic/18075-help-with-while-loop/
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 [email protected]; [email protected]; [email protected];
$allemails = $allemails.$email.';';
}
$allemails = $str = substr($allemails,0,strlen($allemails)-1);
echo '<br />';
echo $allemails;
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/18075-help-with-while-loop/#findComment-77496
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);  //--> [email protected]; [email protected]; [email protected]
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/18075-help-with-while-loop/#findComment-77509
Share on other sites

Archived

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

×
×
  • 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.