/    Sign up×
Community /Pin to ProfileBookmark

calling a function from inside HEREDOC

Hi,

i have a code like this

[CODE]print <<< EOF

<select>
$countries
</select>

EOF;
[/CODE]

I want that $countries calls and executes the following function:

[code=php]function do_countries() {

$query = “SELECT * from ip_country group by country” ;
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
echo ‘<option value=”‘ . $row[‘country’] . ‘”>’ . $row[‘country’] . ‘</option>’;
} }
[/code]

That is the original function but i had to change the ECHO to RETURN like this to it wont executes the code when trying to declare the variable:

[code=php]function do_countries() {

$query = “SELECT * from ip_country group by country” ;
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
return ‘<option value=”‘ . $row[‘country’] . ‘”>’ . $row[‘country’] . ‘</option>’;
} }
[/code]

and finally:

[CODE]
$countries = do_countries();
print <<< EOF

<select>
$countries
</select>

EOF;
[/CODE]

it wont work quite well because its supposed to generate a full list of countries and i only get the first one of the list. I guess it has to do with changing the ECHO for the RETURN but if i dont change it then it wont work either. Can you please help me out?

Thank you.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 01.2010 — You need to move the return outside of the while loop, otherwise you will always only get one iteration.
[code=php]
function do_countries() {
$text = "";
$query = "SELECT * from ip_country group by country" ;
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
$text .= '<option value="' . $row['country'] . '">' . $row['country'] . "</option>n";
}
return $text;
}
[/code]
Copy linkTweet thisAlerts:
@supercainauthorNov 01.2010 — Thank you. That fixed the problem. One more question. I would like to know why is $text .= instead of just $text =

Whats the purpose of the period there?

Thank you.
Copy linkTweet thisAlerts:
@NogDogNov 01.2010 — It appends the string instead of overwriting it. These are functionally equivalent:
[code=php]
$text .= "new text";
$text = $text . "new text";
[/code]
Copy linkTweet thisAlerts:
@supercainauthorNov 01.2010 — I see, thank you again. One final question please. Does the above work for include as well? i want a variable to execute an include when embedded into a heredoc but cant get it to work


[code=php]function do_include() {
return include('index.php');
}
$include = do_include();


print <<< EOF

<select>
$include
</select>

EOF;[/code]


thank you again.
Copy linkTweet thisAlerts:
@NogDogNov 02.2010 — No, an include will execute and output at the point where you call it via include(). If no PHP code exists in the include file, you could just use file_get_contents() instead. If it does need to execute some PHP code, then you could use output buffering (though things would start getting kind of ugly at that point, IMO. ? ).

[code=php]
function do_include() {
ob_start();
include "some_file.php";
return ob_get_clean();
}
[/code]
Copy linkTweet thisAlerts:
@supercainauthorNov 02.2010 — Thank you again. That was very useful. I was using file_get_contents but i needed it to execute before loading along with the rest of the code so include was a must and thanks to your help now i know how to use it.

Thank you.
×

Success!

Help @supercain spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...