Jump to content

Looping results into a variable string


paddy_fields
Go to solution Solved by Psycho,

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;
               
      
    }

Edited by paddyfields
Link to comment
Share on other sites

  • Solution

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;
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.