Jump to content

How can I write this short/simple bit of php so it will work?


spindrift

Recommended Posts

Hello!

I have this code in template.php...

[code]<?php @ require_once ("$pagename.php"); ?>[/code]

...to bring content into the template.

I've got a folder called 'news' for all the news articles so how could I edit the code to say 'look for files in the news folder as well as the current folder'?

I guess I would need something like:

[code]<?php @ require_once ("$pagename.php" or "news/$pagename.php"); ?>[/code]

...but that doesn't work. How would I change it to work?

Thank you very much and hope to hear from you.

Mark
Link to comment
Share on other sites

First question:
[code]<?php
$file=$pagename;
$file.=".php"
@require_once ($file);[/code]

I didnt really understand the second question... The file is either in root folder or in /news/ ? Or is it in both?

Orio.
Link to comment
Share on other sites

This was already answered in another thread you started.

The solution is to use the file_exists() function [see the php manual] to check for the presence of the file you want in (say) the root folder. If it exists, include it; if it doesn't exist check with file_exists() again in the /news folder.
Link to comment
Share on other sites

[!--quoteo(post=378676:date=May 31 2006, 07:05 AM:name=Orio)--][div class=\'quotetop\']QUOTE(Orio @ May 31 2006, 07:05 AM) [snapback]378676[/snapback][/div][div class=\'quotemain\'][!--quotec--]
First question:
[code]<?php
$file=$pagename;
$file.=".php"
@require_once ($file);[/code]

I didnt really understand the second question... The file is either in root folder or in /news/ ? Or is it in both?

Orio.
[/quote]

Hi Orio and Andy,

Many thanks for the replies - much appreciated.

The file will be in either the _root folder or in /news/. Do you know how I could code it?

Many thanks and hope to hear back from you.

All the best,

Mark
Link to comment
Share on other sites

Is it me or did Andy not explain how to! I will quote his post:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]The solution is to use the [b]file_exists()[/b] function [see the php manual] to check for the presence of the file you want in (say) the root folder. If it exists, include it; if it doesn't exist check with file_exists() again in the /news folder.[/quote]
Heres a link to manual that shows you how to use the [a href=\"http://uk2.php.net/manual/en/function.file-exists.php\" target=\"_blank\"]file_exists[/a] function.

But any I will provide you the code:
[code]//check in the root folder first
if(file_exists('./' . $filename . '.php'))
{
    include './' . $filename . 'php';
}
//if it wasn't found in the root folder then check in the news folder
elseif(file_exisits('./news/' . $filename . '.php'))
{
    include './news/' . $filename . '.php';
}
// if it couldn't be found display message
else
{
    echo $filename . '.php could not be found in either the root folder or the news folder!';
}[/code]
Link to comment
Share on other sites

hi wildteen88,

that's great, thank you so much for that!

the code works fine for loading files from the _root directory but when it needs to pull something from the /news/ folder, i get the following error message...

Fatal error: Call to undefined function: file_exisits() in /var/www/html/clients/npr/main.php on line 40

...that line of syntax is... [code]elseif(file_exisits('./news/' . $filename . '.php'))
{
    include './news/' . $pagename . '.php';
}
// if it couldn't be found display message
else
{
    echo $pagename . '.php could not be found in either the root folder or the news folder!';
}[/code]

do you by any chance know why this could be happening? i've tried a few small things like taking out the full-stop infront of ('./news/' etc. but to no avail unfortunately.

thank you very much once again and i'd be grateful if you could get back to me at some point please.

all the best,

mark
Link to comment
Share on other sites

Fatal error: Call to undefined function: file_exisits() in /var/www/html/clients/npr/main.php on line 40

that's telling you a function by the name file_exis[b]i[/b]ts doesn't exist. Simple typo - change that to [b]file_exists[/b] ()
Link to comment
Share on other sites

hi guys,

thanks for that - boy, do i look stupid!

i've corrected the typo but now everytime it tries to call something from the /news/ folder, it always come up with a message saying "060308.php could not be found in either the root folder or the news folder!".

i've checked that 060308.php is definately in /news/ so i don't understand it. Does any of you guys have any suggestions at all?

thanks again for all your help.

mark
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.