Solved

How to display a functions data inside HEREDOC

Posted on 2016-11-27
2
14 Views
Last Modified: 2016-11-27
I am populating a dropdown with database records without using HEREDOC but I now want to do it with HEREDOC. Is is possible to edit this to display the contents of the function query or not? I assume I should remove the PHP tags from inside the HEREDOC but I just left them there so you can see the actual HTML.

$showData = <<<SHOWDATA

<div class="form-group">
<label>Select Categories</label>
<select multiple data-title="Multiple Select" name="cat_name" class="selectpicker" data-style="btn-info btn-fill btn-block" data-menu-style="dropdown-blue">
<?php list_categories_multiple($link); ?>
</select>
</div>	
SHOWDATA;
echo $showData;

Open in new window

0
Comment
Question by:Black Sulfur
2 Comments
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 350 total points
ID: 41903013
You can't: HEREDOC manages only variables. But you can do this:
$showData = <<<SHOWDATA
<div class="form-group">
<label>Select Categories</label>
<select multiple data-title="Multiple Select" name="cat_name" class="selectpicker" data-style="btn-info btn-fill btn-block" data-menu-style="dropdown-blue">
SHOWDATA;
$showData .= list_categories_multiple($link);
$showData .= <<<SHOWDATA
</select>
</div>	
SHOWDATA;
echo $showData;

Open in new window

1
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 total points
ID: 41903027
A smart design would create all of your variables in a PHP object instance of StdClass or your own preferred object, and then inject the variables into the HTML via the HEREDOC.  If you have a function that creates HTML statements and you need these injected into the output HTML document, you might want to consider redesigning the application using a Separation of Concerns.

A HEREDOC is just another way of writing a string variable as if you had double quotes.  
http://php.net/manual/en/language.types.string.php
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now