2013-07-31 26 views
0

我有這樣的PHP代碼:PHP循環具有多個行

$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]"); 

它從表中選擇行,我需要它與行的列表中的電子郵件只是一個電子郵件

當我運行SQL我知道有大約10行(做)

當它發送電子郵件時,它使用$email_body變量,但只在電子郵件中放一行。

我已經創建了一個$numbers_list變量,該變量應該包含所有行的列表,但它只有一行。

+2

'mysql_ *'現在** **不推薦使用。請使用[mysqli](http://php.net/manual/en/book.mysqli.php)或[PDO](http://php.net/manual/en/book.pdo.php)。 – christopher

回答

6

創建一個數組(),將行數據推送給它並在$ email_body中使用implode();

試試這個

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

和往常一樣MySQL是過時所以看看到的mysqli或PDO

+0

真棒方法。我會像其他人所建議的那樣通過串聯來完成它,但這應該是優雅的+1。 – BLaZuRE

+0

謝謝@BLaZuRE ... :)).... – bipen

0
$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;