www.webdeveloper.com
Results 1 to 7 of 7

Thread: calling a function from inside HEREDOC

  1. #1
    Join Date
    Jul 2006
    Posts
    373

    calling a function from inside HEREDOC

    Hi,

    i have a code like this

    Code:
    print <<< EOF
    
    <select>
    $countries
    </select>
    
    EOF;
    I want that $countries calls and executes the following function:

    PHP Code:
    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>';
        } } 
    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:

    PHP Code:
    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>';
        } } 
    and finally:


    Code:
    $countries = do_countries();
    print <<< EOF
    
    <select>
    $countries
    </select>
    
    EOF;
    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.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,537
    You need to move the return outside of the while loop, otherwise you will always only get one iteration.
    PHP Code:
    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;

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jul 2006
    Posts
    373
    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.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,537
    It appends the string instead of overwriting it. These are functionally equivalent:
    PHP Code:
    $text .= "new text";
    $text $text "new text"
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jul 2006
    Posts
    373
    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


    PHP Code:
    function do_include() {
    return include(
    'index.php');
    }
    $include do_include();


    print <<< EOF

    <select>
    $include
    </select>

    EOF; 
    thank you again.

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,537
    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. ).

    PHP Code:
    function do_include() {
       
    ob_start();
       include 
    "some_file.php";
       return 
    ob_get_clean();

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Jul 2006
    Posts
    373
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles