gpt4 book ai didi

php - 在 while 循环之外访问变量

转载 作者:太空宇宙 更新时间:2023-11-03 10:45:35 25 4
gpt4 key购买 nike

我想知道如何访问以下 while 循环之外的变量:

  $resources = "select * from resources where stage LIKE '%".$stage."%'";

$run_query = mysqli_query($con, $resources);

while($row = mysqli_fetch_array($run_query)) {

$format = $row['format'];
$title = $row['title'];
$costs = $row['cost'];
$stage = $row['stage'];
$topic = $row['topic'];
$link = $row['link'];
}




// Email subject
$subject = "subject!";
$subject_us = "New custom resource delivered to: $clean_email";

$message = '<html><body>';
$message .= "<p>";
$message .= "Hi $clean_fullName, <br><br>";
$message .= " Based on your responses, we have created a custom resource pack tailored to your needs. <br><br>";
$message .= "<b>$title</b><br>";
$message .= "$format <br>";
$message .= "$costs <br>";
$message .= "$link <br><br>";

[...]我不想在 while 循环中包含邮件项目的原因是它会发送 10 封电子邮件,例如每个标题,而不是一封包含所有标题的电子邮件,这就是为什么我不想在 while 中包含循环。

问题是我无法访问 while 循环内的项目并将其包含在下面。

最佳答案

如评论中所述,数组将是您的最佳选择。

$data = array();
while($row = mysqli_fetch_array($run_query)) {
// add to data
$data[] = array(
'format' => $row['format'],
'title' => $row['title'],
'costs' => $row['cost'],
'stage' => $row['stage'],
'topic' => $row['topic'],
'link' => $row['link']
);
}

很简单吧?现在是逻辑部分。您在这里所要做的就是遍历上面的 $data 数组以发送每封电子邮件。

foreach($data as $item) {
// Email subject
$subject = "subject!";
$subject_us = "New custom resource delivered to: $clean_email";

$message = '<html><body>';
$message .= "<p>";
$message .= "Hi $clean_fullName, <br><br>";
$message .= " Based on your responses, we have created a custom resource pack tailored to your needs. <br><br>";
$message .= "<b>{$item['title]}</b><br>";
$message .= "{$item['format']} <br>";
$message .= "{$item['costs']} <br>";
$message .= "{$item['link']} <br><br>";
}

我假设您是将其发送给客户,对吗?因为我看不到您在哪里设置 $clean_email$clean_fullName

关于php - 在 while 循环之外访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32261194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com