Trying to add a php include ...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rdkelsey
Forum Newbie
Posts: 5
Joined: Fri Aug 14, 2009 7:23 am

Trying to add a php include ...

Post by rdkelsey »

I have been trying to do this for several months now. I can't seem to make this work, nor can I find the answer online.
(I am not a programmer, for sure, but I can dabble a bit)

This is from a file (archive.php) within a Wordpress blog I run:

<?php single_cat_title(); ?>

If I retype this into the file it will print to screen the name of the category, let's say ... marketing.

What I want to do is have a "php include" that calls a file name that is this "category" (marketing).

<?php
include_once("templates/marketing.php");
?>

like that, where the word "marketing" would come from <?php single_cat_title(); ?>.

I have tried ending the include and inserting the single_cat call ... but of course that doesn't work.

I've tried many different ideas, but like I said, I'm not a programmer.

Can this even be done?

--Robert
robnet
Forum Commoner
Posts: 85
Joined: Mon Aug 10, 2009 8:32 am
Location: South East, UK

Re: Trying to add a php include ...

Post by robnet »

Try this:

Code: Select all

 
include('dir/'.function().'.php');
 
rdkelsey
Forum Newbie
Posts: 5
Joined: Fri Aug 14, 2009 7:23 am

Re: Trying to add a php include ...

Post by rdkelsey »

Here is what I put into the file:

<?php include ('../../'.single_cat_title().'.php'); ?>

Here is what printed to screen:

Marketing
Warning: include(../../.php) [function.include]: failed to open stream: No such file or directory in /home/ddeu/public_html...etc
robnet
Forum Commoner
Posts: 85
Joined: Mon Aug 10, 2009 8:32 am
Location: South East, UK

Re: Trying to add a php include ...

Post by robnet »

Ahaa: I think your function is echoing the string you want. If you use return instead of echo you should be there.

This will output the string to screen before your include statement is processed:

Code: Select all

function getstring(){
 echo "Marketing";
}
While this will return the string after the function has finished, into include(), just as you need:

Code: Select all

function getstring(){
 return "Marketing";
}
rdkelsey
Forum Newbie
Posts: 5
Joined: Fri Aug 14, 2009 7:23 am

Re: Trying to add a php include ...

Post by rdkelsey »

i'm not getting something here

I tried:

<?php return('../../../includes/'.single_cat_title().'.inc'); ?>

which simply prints to screen the cat name (marketing or acne or whatever category i am in)

<?php single_cat_title(); ?>
simply prints the cat name to screen

<?php include('../../../includes/'.single_cat_title().'.php'); ?>

First displays to screen the cat name:
Marketing
Then prints the error to screen:
Warning: include(../../../includes/.php) [function.include]: failed to open stream: No such file or directory in /home/adfss/public_html/blog ...... etc

So, it is printing the cat name to screen but it IS NOT putting the cat name into the include where the error says ../../../includes/.php --- it should be /includes/marketing.php
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Trying to add a php include ...

Post by jackpf »

Do as robnet suggested and use return() then.
robnet
Forum Commoner
Posts: 85
Joined: Mon Aug 10, 2009 8:32 am
Location: South East, UK

Re: Trying to add a php include ...

Post by robnet »

The problem is in your function, single_cat_title(). - replace "echo" with "return" - as the example above...

If I have misdiagnosed the problem post the function and I'll have a look.
rdkelsey
Forum Newbie
Posts: 5
Joined: Fri Aug 14, 2009 7:23 am

Re: Trying to add a php include ...

Post by rdkelsey »

I finally solved this:

<?php $current_category = single_cat_title("", false); ?>
<?php include_once("/includes/$current_category.inc"); ?>

That code outputs whatever I have in a directory full of include files. I have one file for each category the blog has.

One problem though is that the "single_cat_title" provides me with a name with a Capital Letter to start it, like Marketing instead of marketing.

Is there a way to force it to be lowercase?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Trying to add a php include ...

Post by jackpf »

strtolower()
rdkelsey
Forum Newbie
Posts: 5
Joined: Fri Aug 14, 2009 7:23 am

Re: Trying to add a php include ...

Post by rdkelsey »

Yes, this works.

<?php $current_category = single_cat_title("", false); ?>
<?php include_once strtolower("includes/$current_category.php"); ?>

This is very cool ... I know am able to pull out a file to include within any page that is relevant to the topic of the page itself ... well, relatively revelant anyway, since categories are rather broad.

I need to dig deeper into php and learn, learn, learn...

THANK YOU
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Trying to add a php include ...

Post by jackpf »

Cool, no problem.
Post Reply