Jump to content

Looping results into a variable string


paddy_fields

Recommended Posts

Hi, I'm creating an email alert and need the entire list of results to be stored in $message so I can use it in the mail() function

 

As the script loops I'm trying to append each result to $message but I can't work out how to do this using the <<< method (I've forgotten what it's called so I can't google it!)

 

I've tried the $x = $x."new data" method but this clearly doesn't work.

 

Can someone point me in the right direction?


    //display the job results for that particular alert
    while($row = $jobResult->fetch_assoc()){


        $title = $row['title'];
        $location = $row['location'];
        $subject = $row['subject'];


        //begining of HTML message
        $message = $message.<<<MESSAGE
        <h3>$title</h3>
        <p>$location</p>
        <p>$subject</p>
        <hr>
MESSAGE;
               
      
    }

Link to comment
https://forums.phpfreaks.com/topic/286728-looping-results-into-a-variable-string/
Share on other sites

That format works for me. Are you getting errors? I didn't do a DB query, but I ran the following and got the expected results

 

for($i=0; $i<5; $i++)
{

    $message = $message.<<<MESSAGE
        <h3>title{$i}</h3>
        <p>location{$i}</p>
        <p>subject{$i}</p>
        <hr>
MESSAGE;

}

echo $message;

Haha, right you are. I wasn't expecting it to work for some reason and so when I got a Notice I assumed the email hadn't sent.

 

Do you know why I'd be getting the notice though? It doesn't display when I simply use $message = <<<MESSAGE

Notice: Undefined variable: message in /Users/pat/Sites/recruitment/1_DummySite/email-alert-sandbox.php on line 68

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.