Skip to main content

Forums

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #195167
    bearhead
    Participant

    ok, so I have a bit of an odd situation:

    I have three php documents: names.php, project_data.php, and template.php.

    What I’m trying to do is echo the contents of a div in names.php into a div in project_data.php, and then echo that content again into template.php.

    In order echo the contents of a specific div, I’m using this php:

    <?php
    $content = file_get_contents('FILE LOCATION');
    $first_step = explode( '<div id="DIV ID">' , $content );
    $second_step = explode("</div>" , $first_step[1] );
    echo $second_step[0];
    ?>
    

    The content from names.php is being successfully echoed into project_data.php. The issue is that when I try to echo that content from project_data.php into template.php, the literal php code gets echoed out instead of the content.

    Its like when I’m looking at template.php, the php in project_data.php isn’t running and it’s just being treated like a text string or something? Maybe

    explode()
    

    has something to do with it?

    #195170
    Tom
    Participant

    It’s really hard to diagnose without seeing the rest of the code.

    #195174
    bearhead
    Participant

    This reply has been reported for inappropriate content.

    Is there a way to maybe parse a text string into php?

    #195177
    __
    Participant

    This reply has been reported for inappropriate content.

    the php in project_data.php isn’t running and it’s just being treated like a text string or something? Maybe explode() has something to do with it?

    It’s because you’re using file_get_contents():

    Reads entire file into a string
    —http://php.net/file_get_contents

    You need to do some re-organization, here. The content in question should be in your (a) template file. If it needs to be re-usable, it should be in its own template file. This way, you may simply [require](http://php.net/require) it where you want it to appear.

    div-template.php

    <div>Hello, World!</div>
    

    main-template.php

    <!doctype html>
    <html>
        ...
        <?php require "./path/to/div-template.php" ?>
        ...
    </html>
    

    Further, the scripts that actually do work (logic, data retrieval, etc.) should be completely separate from your templates.

    For example, you might have a script which controls the page itself, and another which prepares the needed data.

    controller.php

    <?php
    // make decisions about what to do,
    //  handle user input,
    //  get info from the database,
    //  and so forth.
    
    // maybe you need other code to do some of the work;
    //  that's fine
    require "./other/script.php";
    
    // when ALL of the above work is done,
    // THEN you can use your templates:
    require "./path/to/main-template.php";
    exit;
    

    Think of it this way: PHP happens first, on your server. HTML happens last, on the user’s browser. So design your scripts in the same way and separate the logic/work your program does from its output.

    • All work goes FIRST
    • All output goes LAST
    #195178
    __
    Participant

    This reply has been reported for inappropriate content.

    Is there a way to maybe parse a text string into php?

    Yes.
    Do not do this.

    #195181
    bearhead
    Participant

    This reply has been reported for inappropriate content.

    ah, ok, thanks for explaining that… I restructure like you suggested, and I got things working like I was hoping!

    :)

    #195186
    __
    Participant

    This reply has been reported for inappropriate content.

    hey, cool! : )

    Let me know if you run into any other issues.

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.