SteveU
SteveU

Reputation: 111

PHP Combine results from query into one string

I have a app that searches a database and retrieves 3 fields Name, Email Address, Year group.

I put these into a table and would like to have a mailto: link that has all the email addresses retrieved in it but seperated by commas..

something like this for example.

<a href=mailto:" .$row['Email'].  ">"  .$row['Email']. "</a>

and the link would actually look like this : mailto:[email protected],[email protected],[email protected]

any ideas?

Upvotes: 0

Views: 368

Answers (5)

Elle Mundy
Elle Mundy

Reputation: 2217

Building on Mike Purcell's answer, you can do this:

<?php
    while ($row = mysql_fetch_row) {
        $emails[] = $row['Email'];
    }
    $emailsString = implode(',', $emails);
?>
<a href="mailto:<?php echo $emailsString ?>"><?php echo $emailsString ?></a>

Upvotes: 0

Jay Slupesky
Jay Slupesky

Reputation: 1913

I think you want the join() function. For example:

$mailtolist = join(",",$queryresult);

Upvotes: 0

Roger
Roger

Reputation: 7612

Just concat the email like this:

   // loop over the rows
   $rows = //  resultset
    for ($row in $rows) {
     $email = $row['email'] . ","
    }

   // trim last comma

    // create href
    $desc = "this is the link description"
    href = "<a href=\"mailto:$email\">$desc</a>"

   // or
   <a href="mailto:<?= $email ?>">link description</a>

Upvotes: 0

0x46616c6b
0x46616c6b

Reputation: 1475

try this way

<a href="mailto:<?php echo $row['Email'] ?>"><?php echo $row['Email'] ?></a>

this syntax is clearer and echoes only the variable on the place you want instead the whole line where echoed.

Upvotes: 0

Mike Purcell
Mike Purcell

Reputation: 19989

You could build an array of emails then just implode the array:

while ($row = mysql_fetch_row()) {
    $emails[] = $row['Email'];
}

var_dump(implode(',', $emails));

Upvotes: 1

Related Questions