Jump to content


Photo

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


  • Please log in to reply
9 replies to this topic

#1 spindrift

spindrift
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 31 May 2006 - 11:55 AM

Hello!

I have this code in template.php...

<?php @ require_once ("$pagename.php"); ?>

...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:

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

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

Thank you very much and hope to hear from you.

Mark

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 31 May 2006 - 12:05 PM

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

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

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 31 May 2006 - 01:12 PM

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.
Legend has it that reading the manual never killed anyone.
My site

#4 spindrift

spindrift
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 31 May 2006 - 04:21 PM

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

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

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 31 May 2006 - 06:46 PM

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 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.[/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:
//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!';
}


#6 spindrift

spindrift
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 01 June 2006 - 12:02 PM

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...
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!';
}

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

#7 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 01 June 2006 - 12:22 PM

It's a typo ... change exisits to exists and it'll work.
Legend has it that reading the manual never killed anyone.
My site

#8 MikoMak

MikoMak
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 01 June 2006 - 12:23 PM

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_exisits doesn't exist. Simple typo - change that to file_exists ()

#9 spindrift

spindrift
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 01 June 2006 - 01:13 PM

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

#10 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 01 June 2006 - 02:51 PM

Can you post the version of code that you are using now, please.
Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users