Forum Moderators: open

Message Too Old, No Replies

Convert output of mysql fetch array to a $variable

Convert output of mysql_fetch_array to a $variable

         

drooh

6:07 am on Mar 9, 2007 (gmt 0)

10+ Year Member



I am making a simple system that views email addresses that I have stored for an email newsletter. I am having my php list them out in groups of 10 adding a semicolon at the end of each address. This way I can copy & paste them right into my email composition.

The problem I am having is that I want to remove the very last semicolon becuase what I have is outputting something like this

randomnameemailcom; randomguyemailcom; randomgirlemailcom;

and I need it to output like this

randomnameemailcom; randomguyemailcom; randomgirlemailcom

here is some of my php code, i know that i need to probably use the rtrim() function but Im just not sure how to emplement it...


while(list($val) = mysql_fetch_array($result))
{
$text = "$val; ";
};

$text = rtrim($text, '; ');
echo $text;

how would I make the output result into a single variable in which I could then clip the final semicolon off of?

dreamcatcher

8:22 am on Mar 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$text = substr_replace [uk2.php.net]($text,'',-1);

dc

drooh

9:14 am on Mar 9, 2007 (gmt 0)

10+ Year Member



that seems to work but I still am having trouble understanding how to get what is output by the array into a single variable. meaning in my example, all the emails are listed out with a semicolon after them & I want to just clip off the very last semicolon of the string..?

does this make sense?

in the example I have it only echos one email address, not all of them.

dreamcatcher

9:26 am on Mar 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Firstly, get your data into an array like this:


$text = array();
$string = '';
while(list($val) = mysql_fetch_array($result))
{
$text[] = $val;
};

Then do this:


if (!empty($text))
{
$string = implode(",",$text);
echo $string;
}

Using implode you won`t need to strip the last comma.

dc

drooh

9:46 am on Mar 9, 2007 (gmt 0)

10+ Year Member



awesome, not sure i know how you did it or understand why it works but it does, thank you!

dreamcatcher

4:04 pm on Mar 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You`re welcome. Implode converts data in an array to a delimited string. More info here [uk2.php.net]

dc

drooh

10:58 am on Mar 26, 2007 (gmt 0)

10+ Year Member



I am having a problem now with this script, it seems that the fields in the db that are empty are causing problems. is there a way to populate a list of only fields that contain text? right now Im getting an output similar to this.


, email[at]email[dot]com, , , email[at]email[dot]com, email[at]email[dot]com, ,

dreamcatcher

8:41 pm on Mar 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No problem drooh, change this:

$text[] = $val;

to this:

if ($val)
{
$text[] = $val;
}

dc