need help debugging php, please.

Discussion in 'Web Design and Development (archive)' started by brianellisrules, Oct 22, 2003.

  1. macrumors regular

    Joined:
    Oct 17, 2003
    #1
    PHP:
    <?php
    $full_path 
    getenv("REQUEST_URI");
    $file basename ($full_path,".php"); 
    include(
    'head.inc');
    include(
    $file.inc);
    ?>
    OK, here's what I'm trying to do. Let's say I have a file named "pics.php". There's a corresponding "pics.inc" file that would have all the html formatting. "head.inc" contains my header/banner/links/etc.

    I'd like for the php to grab the name of the file "pics.php" and automatically know to use "pics.inc". I'm thinking this make my life easier because I'd have files like "ohio.php", "towelshirt.php", "complaint.php". Rather than going into each one of those files and changing the corresdponding *.inc name, I'd like to have it update automatically.

    Is this making any sense?

    To test this:
    www.brianellisrules.com/test/ -> click on Pictorials. It's the only like I have setup to work like this. And it doesn't even work right.
     
  2. macrumors 6502a

    sonofslim

    Joined:
    Jun 6, 2003
    #2
    first off, basename might not be working the way you want it to--this is no fault of your own.

    basename(dir/whatever.ext) will return the filename, with extension, as expected; but to return a filename without extension using the suffix parameter, as you have with basename($full_path,".php") your server needs to be running php version 4.1.0. my server is running 4.0.6 and this wouldn't work for me either. so you need to strip off the extension in some other manner.

    try this:
    PHP:
    $filename current(explode('.'basename($SCRIPT_NAME)));
    here's what happens, from the inside out:
    PHP:
    basename($SCRIPT_NAME)
    does what it looks like, taking the basename (with extension, remember) from whatever page the command is on.

    next, we use the explode command to break that result into an array, using the '.' as our break point.

    finally, we use the current command to retrieve the current element of this array. since we haven't done anything to this array yet, the pointer is still on the first element -- just what we want, the first bit of that filename.

    so: you've arrived at a good basename. now you need to append the correct .inc suffix. your command,
    PHP:
    include($file.inc)
    won't work because of that period. in php, the period acts as a concatenator; meaning that it appends one string to another.
    PHP:
    print('hello'.'world')
    prints: "hello world".

    likewise, if your basename is "thisfile,"
    PHP:
    include($basename.inc)
    asks the php engine to include "thisfileinc" because it just runs the two together. and without quotes, that inc isn't going to do any good, either, because php won't know it's a string. instead, use the concatenator like this:
    PHP:
    include($file.'.inc')
    this will result in an include of filename.inc. see the difference?

    so your complete code looks like this:
    PHP:
    <?php
    $filename 
    current(explode('.'basename($SCRIPT_NAME)));
    include(
    $filename.'.inc');
    ?>
    placing this in anyfile.php will result in an include of anyfile.inc

    hope this helps!!
     
  3. macrumors 6502a

    sonofslim

    Joined:
    Jun 6, 2003
    #3
    one more thing i should definitely mention: a great, GREAT resource for php is php.net. to get more information on all things php, just go to php.net/[whatever] and it'll bring up a full reference for you.

    try it out: www.php.net/basename
     
  4. thread starter macrumors regular

    Joined:
    Oct 17, 2003
    #4
    Awesome!! Thanks for the help!!

    I should've mentioned that at some point during my debugging process I added the command "print $file" as an intermediate step just to make sure itwas extracting the file name correctly (which it was).... so I guess that means my server has 4.1.0, eh? And since php is all server-side, I should be good to go, regardless of who visits the site, right?

    And now that you mention it, I do remember reading yesterday in one of the tutorials that the "." is used to concactenate two strings... I should've remembered. Oh well. I had a sneaking suspicion it would be a *really* simple fix.

    Thanks again!
     
  5. macrumors 6502a

    sonofslim

    Joined:
    Jun 6, 2003
    #5
    phpinfo() will list the version as well as every other parameter--useful at times, but too verbose if you just want the version. echo phpversion(); will do the trick.

    and yes, php is server-side; so if your code works, it'll work for everyone.

    good luck!
     
  6. thread starter macrumors regular

    Joined:
    Oct 17, 2003
    #6
    I've got another problem. I want to include a file that's not in the same directory... according to www.php.net/include/ this should work.

    PHP:
    <?php
    include('http://www.brianellisrules.com/test/head.inc');
    include(
    'stories.inc');
    ?>
    Unfortunately, I get this error:

    Warning: main(): stream does not support seeking in /home2/brian/public_html/test/stories/index.php on line 2

    Does the error mean that the php isn't configured to allow that function? Or that it doesn't allow including of files from different directories?


    EDIT: I forgot to mention that this php file would be stored in: http://www.brianellisrules.com/test/stories/
     
  7. macrumors 6502a

    sonofslim

    Joined:
    Jun 6, 2003
    #7
    i don't know if you can include an absolute URL like that. (sorry, end of day, synapses clogged with cold coffee.) try pointing to the include file from the parent directory or your root. ie:
    PHP:
    include('../whatever/whatever/file.inc')
    this is always one of those areas where i trip up, and have to spend some time just trying out various dots and slashes to see what points where. don't ask me why i can never keep it straight...
     
  8. thread starter macrumors regular

    Joined:
    Oct 17, 2003
    #8
    another simple fix. thanks again.
     
  9. Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #9
    Looks like I missed the fun earlier. ;)

    Good stuff, sonofslim and glad you're moving along Mr. ber.
     
  10. thread starter macrumors regular

    Joined:
    Oct 17, 2003
    #10
    Haha...

    I'm actually getting started with the big 'switch'.... so, I'm sure there will be plenty more 'fun' in the future.... :)

    -brian
     
  11. macrumors G3

    jelloshotsrule

    Joined:
    Feb 7, 2002
    Location:
    serendipity
    #11
    so the question that comes up, given where we are.... when will you make the ultimate "switch"??????
     
  12. Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #12
    Aha...so Mr. Brian's a Windows user?

    tsk, tsk, tsk...;)
     
  13. thread starter macrumors regular

    Joined:
    Oct 17, 2003
    #13
    Uh.... well.... you see....

    OH MY GOD, LOOK OUT BEHIND YOU!!!!




    *runs away*
     
  14. macrumors G3

    jelloshotsrule

    Joined:
    Feb 7, 2002
    Location:
    serendipity
    #14
    just answer the question

    you can lick my g5 next time you're here if that will help sell it... hah

    or better yet... when we go to night of the panther in nyc friday.... hot!
     
  15. thread starter macrumors regular

    Joined:
    Oct 17, 2003
    #15
    speaking of a website switch, this is getting UGLY.... a mix of php and straight up, all-html pages (because of things like the guestbook, etc). If I want to change my header at all, it's going to be a pain in the butt.
     
  16. Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #16
    Yeah, sometimes it's forcing it to do everything when you don't have complete control over all the elements.

    You just have to decide if it's worth the saved time and effort to have some PHP and then keep the few HTML-only pages, or if the mix will throw you off too much.
     

Share This Page