SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 42

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Location
    Raleigh, NC
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Variables in an include

    is there a way to use either variables or constatnts inside an included file (php.net says that making them global dosen't work but it dosen't say how to make them work...><)

  2. #2
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use vars in an included file

    e.g.
    include.php
    PHP Code:
    <?php
    $var 
    'Hooray Variable!';
    define('CONST''Hooray Constant!');
    ?>
    index.php
    PHP Code:
    <?php
    include 'include.php';
    echo 
    $var '<br />'//Hooray Variable!
    echo CONST; // Hooray Constant!
    ?>
    Dylan

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Location
    Raleigh, NC
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not working, maybe I have some option turned off that's messing it up?

  4. #4
    SitePoint Zealot
    Join Date
    Oct 2005
    Location
    Hegelsom, The Netherlands
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you please give us some material you're trying to get to work. Cause basically it should work like Dylan B said.

    Thanks.
    Yeap, I'm new here :P

    Game Insight

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Location
    Raleigh, NC
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is what I have

    mysite.php
    PHP Code:
    <?php

    require once(include.php)

    echo(
    "<a href = \"" $SitePath "/index.php\">hi</a>");

    ?>
    include.php
    PHP Code:
    <?php
        
    //Address of site
            
    $SitePath "http://www.intemporelstudios.com/~ecensane/TechMagic";

         echo(
    "Stuff");
    ?>
    I know that the file is being includeded becuase it output's stuff, and it's a require.

  6. #6
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change
    PHP Code:
    require once(include.php
    to
    PHP Code:
    require_once('include.php'); 

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Location
    Raleigh, NC
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dylan B
    Change
    PHP Code:
    require once(include.php
    to
    PHP Code:
    require_once('include.php'); 
    woops, I dind't copy paste that from the code so that wasen't the problem, just a typo on the boards here. Like I said it definaltly includes the file cause I didn't get an error and "Stuff" printed.

  8. #8
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Copy and paste your EXACT code.

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Location
    Raleigh, NC
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    header.php
    PHP Code:
    <?php
        
        
    //Do this
            
    ob_start();
        
        
        
    //Address of site
            
    $SitePath "http://www.intemporelstudios.com/~ecensane/TechMagic";

    ?>
    index.php
    PHP Code:
    <?php

        
    //Header
            
    require_once("http://www.intemporelstudios.com/~ecensane/TechMagic/Template/Header.php");

        
    //Include files
            
    require_once($SitePath "/Functions/ConnecttoDB.php");
        
        
        
        
    //Footer
            
    require_once($SitePath "/Template/Footer.php");    

    ?>

  10. #10
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why do you need to place the variable in the include on the first place?

  11. #11
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Location
    Raleigh, NC
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is more code then just the variable in the header.php file, and I want it there cuase I use the sitepath alot and I don't want to have to have that at the top of every code, also I'm going to add some more variables that I use over and over on header.php but right now I need to get the first one to work before I can get the others.

  12. #12
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why don't you use any classes or functions to automatically process yor request ???

  13. #13
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Location
    Raleigh, NC
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by REMIYA
    Why don't you use any classes or functions to automatically process yor request ???
    what?

  14. #14
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,933
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should be using file paths not http paths.

  15. #15
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Location
    Raleigh, NC
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bokehman
    You should be using file paths not http paths.
    can you give me an example of that?

  16. #16
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Location
    Raleigh, NC
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    could some feature have been turned off or something, becuase now scripts that used to work that included files with functions in them don't and I haden't changed any of those. they used to work and now they don't and I know that I didn't change anything in the scripts.

  17. #17
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Location
    Raleigh, NC
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyone?

  18. #18
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Location
    Raleigh, NC
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    uhmmmm well I chagned

    PHP Code:
        //Include files
            
    require_once($SitePath "/Functions/ConnecttoDB.php"); 
    to

    PHP Code:
        //Include files
            
    require_once("../Functions/ConnecttoDB.php"); 
    and it worked, can anyone tell me why?

  19. #19
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your $SitePath variable has been wrong. Try it with:

    echo $SitePath;

  20. #20
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Location
    Raleigh, NC
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no the variable for sitepath was working, it was including the files i had put output in all the include files, there where also links that used $SitePath. I also printed it out and it was exactly what it was supposed to be.

  21. #21
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What did the echo $SitePath; print?

  22. #22
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Location
    Raleigh, NC
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by REMIYA
    What did the echo $SitePath; print?
    http://www.intemporelstudios.com/~ec...ate/Header.php

  23. #23
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,933
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I will say again: you should be using file paths not http paths.
    http path: http://www.domain.com/
    file path: c:/apache2/htdocs/web/site

    the reason it works with '../' is because that works as a relative file path.

  24. #24
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Location
    Raleigh, NC
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but i can't use c:/ becuase I have the files on the internet...the closest I can get is "../"

  25. #25
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,933
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That was an example of a file path. Your server may be on a remote machine but it still uses file paths to access other files on the same machine. Run the following to find the file path of your root directory:
    print $_SERVER['DOCUMENT_ROOT'];


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
  •