charlie
charlie

Reputation: 1384

PHP Loop with multiple rows

I have this PHP Code:

$number = substr_replace($_POST["number"],"44",0,1);

$sql="SELECT * from channel_did where did LIKE '%".$number."%' AND (client_id = '' OR client_id IS NULL or client_id = '611') AND (extension_id = '' OR extension_id IS NULL) ";
$rs=mysql_query($sql,$pbx01_conn) or die(mysql_error());
while($result=mysql_fetch_array($rs))
{
    $numbers_list = $result["did"].'<br>';
    $email_body = '<font face="Arial">
    Hello, here are some numbers that you have requested.<br><br>
    Please be aware that these numbers are in a public pool and not reservered, therefore they could be assigned to another client at any time.<br><br>
    Please make your choice as soon as possible to guarantee the number you require.<br><br>
    '.$numbers_list.'<br><br>
    Kind Regards,<br><br>
    Customer Services<br>
    Integra Digital<br><br>
    tel: 01702 66 77 27<br>
    email: [email protected]<br>
    web: www.integradigital.co.uk
    </font>';
}

    echo $email_body;

sendemail($_POST["emailto"],"Integra Digital <[email protected]>","VoIP Phone Numbers You Requested",$email_body,"[email protected]");

it selects rows from a table and i need it to email just one email with a list of the rows

when i run the SQL i know there are about 10 rows (did)

when it sends the email, its using the $email_body variable but only putting one row in the email.

i have created a $numbers_list variable that should have a list of all the rows but it only does one row.

Upvotes: 0

Views: 168

Answers (2)

bipen
bipen

Reputation: 36531

create an array(), push the rows data to it.. and use implode() in $email_body;

try this

while($result=mysql_fetch_array($rs))
{
    $numbers_list[] = $result["did"];

}

 $email_body = '<font face="Arial">
    Hello, here are some numbers that you have requested.<br><br>
    Please be aware that these numbers are in a public pool and not reserved, therefore they could be assigned to another client at any time.<br><br>
    Please make your choice as soon as possible to guarantee the number you require.<br><br>
    '.implode('<br>',$numbers_list).'<br><br>
    Kind Regards,<br><br>
    Customer Services<br>
    Integra Digital<br><br>
    tel: 01702 66 77 27<br>
    email: [email protected]<br>
    web: www.integradigital.co.uk
    </font>';

and as always mysql is deprecated so have a look to mysqli or PDO

Upvotes: 6

Garytje
Garytje

Reputation: 874

$number_list = Array();
// no need for unneccesary variables
// no need for unneccesary *, SELECT did
$sql="SELECT did from channel_did 
        WHERE       did LIKE '%".substr_replace(mysql_real_escape_string($_POST["number"]),"44",0,1)."%' 
        AND (   client_id = '' 
            OR  client_id IS NULL 
            OR  client_id = '611'
            ) 
        AND (   extension_id = '' 
            OR  extension_id IS NULL
            ) ";
$rs=mysql_query($sql,$pbx01_conn) or die(mysql_error());
while($result=mysql_fetch_array($rs))
{
    $numbers_list[] = $result["did"].'<br>';

}
 $email_body = '<font face="Arial">
    Hello, here are some numbers that you have requested.<br><br>
    Please be aware that these numbers are in a public pool and not reservered, therefore they could be assigned to another client at any time.<br><br>
    Please make your choice as soon as possible to guarantee the number you require.<br><br>
    '.implode(',',$numbers_list).'<br><br>
    Kind Regards,<br><br>
    Customer Services<br>
    Integra Digital<br><br>
    tel: 01702 66 77 27<br>
    email: [email protected]<br>
    web: www.integradigital.co.uk
    </font>';
echo $email_body;

Upvotes: 0

Related Questions