Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: including a function from another file.

  1. #1
    Join Date
    Jan 2007
    Location
    Bournemouth, England
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default including a function from another file.

    hey all, I am trying to make a simple script that detects the URL, and changes some CSS attributes accordingly. I have done all the script, but I have encountered a problem. I could call the function that gets the page file name in the include page, but then it only detects the file name of the include page, but for the script to operate correctly it needs to detect the main file name.

    Heres a layout of how it works:

    file.php [

    include to header.php - for cosmetical reasons the div tag im editing needs to be in this file. This is also the file that contains the script that needs the function.

    actual function here.

    ]
    I have tried it and I get this error: Fatal error: Call to undefined function: curpagename() in /home/killerch/public_html/templates/header.php on line 61

    I know what it means, I just cant see any logical way to get past it :S


    Thanks

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    What I usually do is centralize all of my functions in one file (like functions.php), then include that file with whatever other files I need those functions on; if that makes any sense. Why not try that?

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Jan 2007
    Location
    Bournemouth, England
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    EDIT: ok, solved but came into a new problem...

    lol.php (the file name I need to detect). header.php is included. header.php contains the script, and it needs to be here for asthetical reasons, and the function. I need to get the page name of lol.php - is this possible?
    Last edited by killerchutney; 11-11-2007 at 05:17 PM.

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Try this to see if it is something like what you are talking about:

    test.php
    Code:
    <?php
     include('test2.php');
    
     echo '<BR>';
    
     echo 'This is '.getpage();
    
    ?>
    test2.php
    Code:
    <?php
    
    echo 'Testing from test2.php';
    
    function getpage() {
    
      $this_page = $_SERVER['PHP_SELF'];
    
      return $this_page;
    }
    ?>
    Working Demo: http://thetestingsite.net/test.php

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Jan 2007
    Location
    Bournemouth, England
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    yes, thats it pretty much - but I need to use the output from that in footer.php

  6. #6
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    k...check the link again to see if this works for you:

    test.php
    Code:
    <?php
     include('test2.php');
    
      echo '<BR> This is some text from the actual page test.php <BR>';
    
     include('footer.php');
     ?>
    test2.php
    Code:
    <?php
    
    echo 'Testing from test2.php';
    
    function getpage() {
    
      $this_page = $_SERVER['PHP_SELF'];
    
      return $this_page;
    }
    ?>
    footer.php
    Code:
    <?php
    echo 'The current page is: '.getpage();
    ?>
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  7. #7
    Join Date
    Jan 2007
    Location
    Bournemouth, England
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    footer.php gives me the same error.

    but I have made an online example.

    http://www.killerchutney.com/lol.php (aptly named ) heres the page I need to detect through header.php.

    heres the code in header.php
    Code:
    <div <?php
    	function getpage() {
    		$this_page = $_SERVER['PHP_SELF'];
     		return $this_page;}
    	
    	if (getpage() == 'index.php'){
    		echo ('id="content"');}
    	
    	elseif (getpage() == 'lol.php'){
    		echo ('style="background-color:#ff00f6;"');}
    		
    	elseif (getpage() == '/templates/header.php'){
    		echo ('style="background-color:#3cff00;"');}
    	
    	else{ echo ('style="background-color:#000000;"'); }?>>
    if the script is working correctly, the background color of lol.php should be pink. If it is working incorrectly then it should be green (as it is now).

    you can see in the left corner of the green div /templates/header.php - the include file name. That is using the same function as the one you posted - but in the address bar it says http://www.killerch.../lol.php I need it to detect the URL shown in the address bar, instead the URL of the include.

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Check $_SERVER['REQUEST_URI'].
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Jan 2007
    Location
    Bournemouth, England
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I have tried changing

    $this_page = $_SERVER['PHP_SELF'];

    to $_SERVER['REQUEST_URI'];

    but the same result.

    EDIT: after looking up REQUEST_URI on the php documentation, I think PHP_SELF is a better solution for me - i dont want the vairables to be included.
    Last edited by killerchutney; 11-11-2007 at 08:19 PM.

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It sounds to me as if you're including it via HTTP (i.e. include 'http://...';).
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •