简体   繁体   English

在while循环之外访问变量

[英]Access a variable outside of a while loop

I would like to know how to access a variable outside of the following while loop: 我想知道如何在以下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>";

[...] The reason why I do not want to include the mail items in the while loop its because it will send out 10 emails for example for each title, rather than one email with all title, which is why i do not want to include inside the while loop. [...]我不想在while循环中包括邮件项的原因是因为它将为每个标题发送10封电子邮件,例如,而不是一封包含所有标题的电子邮件,这就是为什么我不想要包含在while循环中。

Problem is that I cannot access the items inside the while loop and include it below. 问题是我无法访问while循环内的项目并将其包含在下面。

I agree with @kitttkittybangbang and @Maverick. 我同意@kitttkittybangbang和@Maverick。

If you want to access your variables use foreach loop and at the same time stored that in array variable. 如果要访问变量,请使用foreach循环,并将其同时存储在数组变量中。

For example: 例如:

Declare at the top of while loop while循环的顶部声明

  $arrVar = []; // empty array variable

Inside while loop create an array variable. while循环内部while创建一个数组变量。

   $arrVar[] = ['format' => $format, 'title' => $title, 'costs' => $costs, 'stage' => $stage, 'topic' => $topic, 'link' => $link];
    print_r($arrVar);  //prints result

Then outside your while loop use. 然后在您的while循环之外使用。

       foreach($arrVar as $key => $val) {
        //do what you want here
     }

Hope this helps. 希望这可以帮助。

As stated in the comments, an array would be your best option. 如评论中所述,数组将是您的最佳选择。

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

Simple enough right? 很简单吧? Now comes the logical part. 现在是逻辑部分。 All you have to do here is loop through your above $data array to send each email off. 您要做的就是遍历上述$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>";
}

I assume you're sending it to the clients right? 我认为您是将其发送给客户吗? Cause I don't see where you set $clean_email or $clean_fullName anywhere. 原因我看不到您在何处设置$clean_email$clean_fullName位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM