1

I would like to know how to access a variable outside of the following while loop:

  $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.

Problem is that I cannot access the items inside the while loop and include it below.

4
  • 2
    Use arrays for each column in the loop, then iterate through your arrays in the email. Commented Aug 27, 2015 at 23:51
  • 2
    You can save the variables into an array Commented Aug 27, 2015 at 23:52
  • which is a better approach? Commented Aug 27, 2015 at 23:59
  • Why access them outside the loop? The @message = could be before the loop, and all the message .= inside the loop. Commented Aug 28, 2015 at 0:19

2 Answers 2

2

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.

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah I have defined clean_email and clean_fullname above. SHould they included within the foreach as well as the mail mail($to,$subject,$message,$headers); thanks
2

I agree with @kitttkittybangbang and @Maverick.

If you want to access your variables use foreach loop and at the same time stored that in array variable.

For example:

Declare at the top of while loop

  $arrVar = []; // empty array variable

Inside while loop create an array variable.

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

Then outside your while loop use.

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

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.