Jump to content


Photo

Help with while loop


  • Please log in to reply
3 replies to this topic

#1 dizel247

dizel247
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 20 August 2006 - 05:27 AM

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

#2 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 20 August 2006 - 08:48 AM

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.

<?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;
?>


#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 20 August 2006 - 09:16 AM

or use an array

<?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
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 dizel247

dizel247
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 20 August 2006 - 01:46 PM

Thank you all for a quick reply.

Best Regards,

Roman




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users