SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Member
    Join Date
    Oct 2003
    Location
    Moscow, Russia
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    File inclusion. Please help.

    How would I include a php file, into this one:

    PHP Code:
    <?php
            
    global $ibforums$DB$std$print;
             
    $this->page_title 'Shadow Gamers Community - Downloads';
             
    $this->nav        = array( $ibforums->lang['page_title'] );
             
    $this->output        

    (
    '<center><b>Welcome to the Shadow Gamers Community Download Center! We are currently developing this new section, so please bear with us.<br /><br />Thank you!</b><br /><br />$RPG</center>');

            
    $print->add_output("$this->output");
            
    $print->do_output( array( 'TITLE' => $this->page_title'JS' => 0NAV => $this->nav ) );

    ?>
    See where the $RPG is in the script? How would I include another php file right in there?

  2. #2
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    include('anotherfile.php'
    ?

  3. #3
    SitePoint Member
    Join Date
    Oct 2003
    Location
    Moscow, Russia
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, you can't do an include inside a (' ') its an error.

  4. #4
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about a function instead though aye ?

    PHP Code:
    '...Thank you!</b><br /><br />'.GetFile().'</center>' 
    And...

    PHP Code:
    function GetFile() {
    # do not use INCLUDE but use fread(...) instead
    #

    You'd need to know some about the file system though ?

    If I remember INCLUDEs don't work this way... Hope this helps anyway

  5. #5
    Thinking about Visual Thinking
    Join Date
    May 2003
    Location
    back in South Africa
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Depending on what you want to achieve with the included script you could
    Quote Originally Posted by PHP Manual
    Handling Returns: It is possible to execute a return() statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it's possible to return values from included files. You can take the value of the include call as you would a normal function.
    see include

  6. #6
    SitePoint Member
    Join Date
    Oct 2003
    Location
    Moscow, Russia
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need to include one php file, inside the other... I managed to do an include, but the file got included at the top of the screen, and broke out of the layout. Here is the file I need to include:
    PHP Code:
    <h2>Roleplaying Name Generator</h2>
    <?php
    $submit 
    $_POST['submit'];
    if(
    $submit){
    $firstname $_POST['firstname'];
    $lastname $_POST['lastname'];
    $mothersmaiden $_POST['mothersmaiden'];
    $citybornin $_POST['citybornin'];

    $firstpart1 substr($lastname03);
    $firstpart2 strtolower(substr($firstname02));

    $lastpart1 substr($mothersmaiden02);
    $lastpart2 strtolower(substr($citybornin03));


    $swfirstname $firstpart1.$firstpart2;
    $swlastname $lastpart1.$lastpart2;
    print(
    "Your roleplaying name is:<p>");
    print(
    "$swfirstname $swlastname");
    } else {
    ?>
    Enter your information below and get your roleplaying name.<p>
    <form action="rpgname.php" method="post">
    <input type="hidden" name="submit" value="submit">
        <table>
            <tr>
                <td>Your First Name</td>
                <td><input type="text" name="firstname"></td>
            </tr>
            <tr>
                <td>Your Last name</td>
                <td><input type="text" name="lastname"></td>
            </tr>
            <tr>
                <td>Your Mother's Maiden Name</td>
                <td><input type="text" name="mothersmaiden"></td>
            </tr>
            <tr>
                <td>The name of the city in which you were born</td>
                <td><input type="text" name="citybornin"></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Generate Your Name">
        </table>
    </form>
    <?
    }
    ?>

  7. #7
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A better way than INCLUDEs would proberly be to enclose that given script you've posted inside HEREDOC tags ?

    Then ECHO the HEREDOC tag yes ? Look at PHPs manual for more on HEREDOC

    Worth a shot I'd say ? Good Luck.

  8. #8
    SitePoint Member
    Join Date
    Oct 2003
    Location
    Moscow, Russia
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have no idea on how to do that.

  9. #9
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From the manual...

    PHP Code:
    <?php
    $str 
    = <<<EOD
    Example of string
    spanning multiple lines
    using heredoc syntax.
    EOD;

    /* More complex example, with variables. */
    class foo
    {
        var 
    $foo;
        var 
    $bar;

        function 
    foo()
        {
            
    $this->foo 'Foo';
            
    $this->bar = array('Bar1''Bar2''Bar3');
        }
    }

    $foo = new foo();
    $name 'MyName';

    echo <<<EOT
    My name is "$name". I am printing some $foo->foo.
    Now, I am printing some 
    {$foo->bar[1]}.
    This should print a capital 'A': \x41
    EOT;
    ?>

  10. #10
    SitePoint Member
    Join Date
    Oct 2003
    Location
    Moscow, Russia
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm going to scream... There is no way I can put that to use with my scripts... Noo much ... foo...

    Urgh... This sucks...

  11. #11
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm...

    What, PHP sucks now ?

  12. #12
    SitePoint Enthusiast Deo's Avatar
    Join Date
    Oct 2003
    Location
    Washington, USA
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    PHP Code:
    <?php 
            
    global $ibforums$DB$std$print
            
    $this->page_title 'Shadow Gamers Community - Downloads'
            
    $this->nav        = array( $ibforums->lang['page_title'] ); 

            
    $output '<center><b>Welcome to the Shadow Gamers Community Download Center! We are currently developing this new section, so please bear with us.<br /><br />Thank you!</b><br /><br />';
     
            
    $output .= include(../path/to/file);
            
    $output .= '</center>';
            
    $this->output        $output;

            
    $print->add_output("$this->output); 
            
    $print->do_output( array( 'TITLE' => $this->page_title'JS' => 0NAV => $this->nav ) ); 

    ?>

  13. #13
    SitePoint Addict shad0w's Avatar
    Join Date
    Aug 2003
    Location
    PA
    Posts
    239
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do the definitions mean after the '<<<'. I can't seem to find a list of different different things to put in front of those. I've seen EOT, EOD, HTML and many others, can someone explain?

  14. #14
    SitePoint Enthusiast Deo's Avatar
    Join Date
    Oct 2003
    Location
    Washington, USA
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the '<<<' is just a way of saying echo or print. It's originally just a function from perl.

    Basically to tell php/perl that an output is starting you put <<<NAME

    and to close it, you place NAME; to signal the end of the output. I dont use it in php because there are faster and more efficient methods of accomplishing the same thing.

    ~Deo

  15. #15
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    What about this?
    PHP Code:
    <?php
            
    global $ibforums$DB$std$print;
            
    $this->page_title 'Shadow Gamers Community - Downloads';
            
    $this->nav        = array( $ibforums->lang['page_title'] );
            
    $this->output        =

    (
    '<center><b>Welcome to the Shadow Gamers Community Download Center! We are currently developing this new section, so please bear with us.<br /><br />Thank you!</b><br /><br />'.readfile('/path/to/include').'</center>');

            
    $print->add_output("$this->output);
            
    $print->do_output( array( 'TITLE' => $this->page_title'JS' => 0NAV => $this->nav ) );

    ?>

  16. #16
    SitePoint Enthusiast Deo's Avatar
    Join Date
    Oct 2003
    Location
    Washington, USA
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    readfile() will be able to open the file, but will not parse the php.

    One thing that I did notice about the file to be included is the fact of reading from http_post_vars.

    I think that probably the best solution would be as follows:

    The Include File:
    PHP Code:
    <?php 

     
    //first, we make the include file into a function.

    function returnfile() {

      global 
    $_POST;

    $display '<h2>Roleplaying Name Generator</h2>';

    $submit $_POST['submit']; 
    if(
    $submit){ 
    $firstname $_POST['firstname']; 
    $lastname $_POST['lastname']; 
    $mothersmaiden $_POST['mothersmaiden']; 
    $citybornin $_POST['citybornin']; 

    $firstpart1 substr($lastname03); 
    $firstpart2 strtolower(substr($firstname02)); 

    $lastpart1 substr($mothersmaiden02); 
    $lastpart2 strtolower(substr($citybornin03)); 


    $swfirstname $firstpart1.$firstpart2
    $swlastname $lastpart1.$lastpart2
    $display .= 'Your roleplaying name is:<p>';
    $display .= '$swfirstname $swlastname';
    } else { 

    $display .= '
    Enter your information below and get your roleplaying name.<p> 
    <form action="rpgname.php" method="post"> 
    <input type="hidden" name="submit" value="submit"> 
        <table> 
            <tr> 
                <td>Your First Name</td> 
                <td><input type="text" name="firstname"></td> 
            </tr> 
            <tr> 
                <td>Your Last name</td> 
                <td><input type="text" name="lastname"></td> 
            </tr> 
            <tr> 
                <td>Your Mothers Maiden Name</td> 
                <td><input type="text" name="mothersmaiden"></td> 
            </tr> 
            <tr> 
                <td>The name of the city in which you were born</td> 
                <td><input type="text" name="citybornin"></td> 
            </tr> 
            <tr> 
                <td></td> 
                <td><input type="submit" value="Generate Your Name"> 
        </table> 
    </form>'
    ;
    }

      
    //now we can return everything set in a variable
      
    return $display
    }
    ?>
    And then of course the main file:
    PHP Code:
    <?php

      
    require_once(/path/to/include/file);
     
            global 
    $ibforums$DB$std$print
            
    $this->page_title 'Shadow Gamers Community - Downloads'
            
    $this->nav = array( $ibforums->lang['page_title'] ); 

            
    $display '<center><b>Welcome to the Shadow Gamers ';
            
    $display .= 'Community Download Center! We are currently ';
            
    $display .= 'developing this new section, so please bear ';
            
    $display .= 'with us.<br /><br />Thank you!</b><br /><br />';

            
    //we now call the function which will return the contents of the included file
            
    $display .= returnfile() . '</center>';

            
    $this->output $display;

            
    $print->add_output("$this->output); 
            
    $print->do_output( array( 'TITLE' => $this->page_title'JS' => 0NAV => $this->nav ) ); 

    ?>
    That should work...

    ~Deo

  17. #17
    SitePoint Member
    Join Date
    Oct 2003
    Location
    Moscow, Russia
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I went with the last example. Thank you. It did the inclusion, and works fine. But then, when the information is submitted through a form, the next page is just a white page with the results. Is there no way to make it give the results in the layout as well?


Bookmarks

Posting Permissions

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