2012-07-10 103 views
0

我想要在名爲sendMail的函數中訪問在while循環中定義的$name變量。我是否必須以某種方式將函數併入循環中?如何從PHP中的函數訪問while循環中的變量?

<?php 
while ($row = mysql_fetch_assoc($result)) 
{ 
    print_r($row); echo "<br><br>"; 
    $name = $row['Name']; 
    $date = $row['sDate']; 
    $time = $row['sTime']; 
    $phone = $row['Phone']; 

    $email = $row['Email']; 
    sendMail($row['Email']); 

    $company = $row['Company']; 
    $course = $row['Course']; 
    $ref = $row['Reference']; 
    $optout = $row['optout']; 

    echo "<tr bgcolor=#ABB5F6> 
    <td>$name</td> 
    <td>$date</td> 
    <td>$time</td> 
    <td>$phone</td> 
    <td>$email</td> 
    <td>$company</td> 
    <td>$course</td> 
    <td>$ref</td> 
    <td>$optout</td> 
    </tr>"; 
} 

// Mail to $to and $emailarray recipients 
function sendMail($to) 
{ 
    $subject = 'Test mail'; 
    $message = 'Hello'.$name; // I want $name from the while loop 
    $headers = array(); 
    $headers[] = "From:" . "[email protected]"; 
    $headerz = implode("\r\n", $headers); 

    mail($to, $subject, $message, $headerz); 
} 

?> 
+0

只需在while循環(在定義之後),在sendMail()內添加sendMail($ name),它將在$到var中。或者,您可以傳遞整個$行,以便擁有所有的值。 sendMail($ row) – somedev 2012-07-10 15:34:17

+0

您可以訪問和設置變量,並調用函數,但不能調用變量。 mysql擴展已過時,正在棄用。新代碼應該使用mysqli或PDO,它們都具有重要的優點,例如支持預處理語句。 – outis 2012-07-10 15:44:27

回答

5

爲什麼不通過$名稱功能的Sendmail:

// Mail to $to and $emailarray recipients 
function sendMail($to, $name) 
{ 
    $subject = 'Test mail'; 
    $message = 'Hello'.$name; // I want $name from the while loop 
    $headers = array(); 
    $headers[] = "From:" . "[email protected]"; 
    $headerz = implode("\r\n", $headers); 

    mail($to, $subject, $message, $headerz); 
} 

然後調用名爲變量Sendmail的功能?

5

要麼讓sendMail()接受陣列(即$row), or add another parameter to Sendmail的()which is called $ name`,並傳遞它

function sendMail($to, $name) 

叫這樣的:。

sendMail($row['Email'], $row['Name']); 

參數可能是最簡單的,不要走下去的路線全局。我建議不要使用數組,因爲格式未定義,並且如果您的表結構可以改變。如果你要使用mysql_fetch_object和強烈定義的類,那麼這將是最可接受的解決方案(當然,當然也使用PDO/MySQLi)......特別是如果你打算延長使用$row以上的更多數據未來。

5

爲什麼不能通過它?

function sendMail($to, $name); 

,並呼籲

sendMail($row['Email'], $row['name']); 
0

我做了,纔能有你的代碼工作所需的變革。

<?php 
      while ($row = mysql_fetch_assoc($result)) 
      { 
       print_r($row); echo "<br><br>"; 
       $name = $row['Name']; 
       $date = $row['sDate']; 
       $time = $row['sTime']; 
       $phone = $row['Phone']; 

       $email = $row['Email']; 

       // i added name as a parameter 
       sendMail($row['Email'],$name); 

       $company = $row['Company']; 
       $course = $row['Course']; 
       $ref = $row['Reference']; 
       $optout = $row['optout']; 

       echo "<tr bgcolor=#ABB5F6> 
       <td>$name</td> 
       <td>$date</td> 
       <td>$time</td> 
       <td>$phone</td> 
       <td>$email</td> 
       <td>$company</td> 
       <td>$course</td> 
       <td>$ref</td> 
       <td>$optout</td> 
       </tr>"; 

      } 

      // Mail to $to and $emailarray recipients 
      // i added name as a parameter 
      function sendMail($to,$name) 
      { 
       $subject = 'Test mail'; 
       $message = 'Hello'.$name; // I want $name from the while loop 
       $headers = array(); 
       $headers[] = "From:" . "[email protected]"; 
       $headerz = implode("\r\n", $headers); 

       mail($to, $subject, $message, $headerz); 
      } 

      ?>