Guest User

Untitled

a guest
Sep 16th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. How to access variable in a while loop from a function in PHP?
  2. <?php
  3. while ($row = mysql_fetch_assoc($result))
  4. {
  5. print_r($row); echo "<br><br>";
  6. $name = $row['Name'];
  7. $date = $row['sDate'];
  8. $time = $row['sTime'];
  9. $phone = $row['Phone'];
  10.  
  11. $email = $row['Email'];
  12. sendMail($row['Email']);
  13.  
  14. $company = $row['Company'];
  15. $course = $row['Course'];
  16. $ref = $row['Reference'];
  17. $optout = $row['optout'];
  18.  
  19. echo "<tr bgcolor=#ABB5F6>
  20. <td>$name</td>
  21. <td>$date</td>
  22. <td>$time</td>
  23. <td>$phone</td>
  24. <td>$email</td>
  25. <td>$company</td>
  26. <td>$course</td>
  27. <td>$ref</td>
  28. <td>$optout</td>
  29. </tr>";
  30. }
  31.  
  32. // Mail to $to and $emailarray recipients
  33. function sendMail($to)
  34. {
  35. $subject = 'Test mail';
  36. $message = 'Hello'.$name; // I want $name from the while loop
  37. $headers = array();
  38. $headers[] = "From:" . "myemail@email.com";
  39. $headerz = implode("rn", $headers);
  40.  
  41. mail($to, $subject, $message, $headerz);
  42. }
  43.  
  44. ?>
  45.  
  46. // Mail to $to and $emailarray recipients
  47. function sendMail($to, $name)
  48. {
  49. $subject = 'Test mail';
  50. $message = 'Hello'.$name; // I want $name from the while loop
  51. $headers = array();
  52. $headers[] = "From:" . "myemail@email.com";
  53. $headerz = implode("rn", $headers);
  54.  
  55. mail($to, $subject, $message, $headerz);
  56. }
  57.  
  58. function sendMail($to, $name)
  59.  
  60. sendMail($row['Email'], $row['Name']);
  61.  
  62. function sendMail($to, $name);
  63.  
  64. sendMail($row['Email'], $row['name']);
  65.  
  66. <?php
  67. while ($row = mysql_fetch_assoc($result))
  68. {
  69. print_r($row); echo "<br><br>";
  70. $name = $row['Name'];
  71. $date = $row['sDate'];
  72. $time = $row['sTime'];
  73. $phone = $row['Phone'];
  74.  
  75. $email = $row['Email'];
  76.  
  77. // i added name as a parameter
  78. sendMail($row['Email'],$name);
  79.  
  80. $company = $row['Company'];
  81. $course = $row['Course'];
  82. $ref = $row['Reference'];
  83. $optout = $row['optout'];
  84.  
  85. echo "<tr bgcolor=#ABB5F6>
  86. <td>$name</td>
  87. <td>$date</td>
  88. <td>$time</td>
  89. <td>$phone</td>
  90. <td>$email</td>
  91. <td>$company</td>
  92. <td>$course</td>
  93. <td>$ref</td>
  94. <td>$optout</td>
  95. </tr>";
  96.  
  97. }
  98.  
  99. // Mail to $to and $emailarray recipients
  100. // i added name as a parameter
  101. function sendMail($to,$name)
  102. {
  103. $subject = 'Test mail';
  104. $message = 'Hello'.$name; // I want $name from the while loop
  105. $headers = array();
  106. $headers[] = "From:" . "myemail@email.com";
  107. $headerz = implode("rn", $headers);
  108.  
  109. mail($to, $subject, $message, $headerz);
  110. }
  111.  
  112. ?>
Add Comment
Please, Sign In to add comment