Use string as part of a file name inside includes - php

I am new to PHP, still learning, but I need help with this:
If I define a constant like this:
<?php
$area = "New York";
?>
Then I will create a file New-York.php.
I would like to use the constant "New York" inside this line:
<?php include $_SERVER["DOCUMENT_ROOT"] . "/includes/New-York.php"; ?>
I will have a lot of different cities and I would like to avoid having to change the includes link for every page, but rather only define the constant $area.
Am I making sense and can that be done?
Thanking you all in advance.

You could use str_replace() to transform the space " " to "-". Then, concatenate into your include path:
<?php
$area = "New York";
?>
<?php include $_SERVER["DOCUMENT_ROOT"] . "/includes/".str_replace(' ','-',$area).".php"; ?>

Related

How to define a class name based on specific page in php?

I am trying to create a body class name based on a specific page in php. What I need to do is first define a variable and then check if the variable exists, and then if it exists display that one, otherwise if it is something else, then do something else, or if nothing then do not show any.
<body<?php if (defined('PAGE_KEY') && var == "homepage") echo " class=\"homepage\"";
elseif (defined('PAGE_KEY') && var == "page3") echo " class=\"page3\"";
elseif (defined('PAGE_KEY') && var == "page12") echo " class=\"page12\""; ?>>
This code will be located in my head.php.
My thought was to first check if variable was defined and if so then check what the variable was defined as and then based on that variable it will display a respective class.
The goal is for example on the page12 for the body tag to look like this:
<body class="page12">
But for example for the body tag on page55 (which I don't want to show a class for) to look like this:
<body>
In doing so I am able to now define css specifically for a page within the header where the body tag happens to be located.
Problem is first I don't know how to define the variable in the page, and second I don't know exactly how to write the php code above properly.
Attempt, for example on page12 I would have this code:
<?php PAGE_KEY = "page12" ?>
This code would be for example located in page12.php.
Also keep in mind, that the variable will come AFTER the body tag.
I also thought of trying to see what the page URL is but I think that's just making things too complicated.
Based on #Jordi's suggestion, how about this:
<body class="<?php echo PAGE_KEY ?>">
on head.php.
And then on page12.php, this:
<?php PAGE_KEY = "page12" ?>
and for example on page5.php this:
<?php PAGE_KEY = "page5" ?>
so that on those respective pages the body tag shows this:
on page5.php:
<body class="page5">
and on page12.php the body tag will show this:
<body class="page12">
Is this right?
#Jose made this suggestion, is this correct?
for example, on page12.php, to define the variable as 'page12', this:
<?php define("PAGE_KEY", "page12"); ?>
Is that what you were suggesting to do?
Ok! This problem is solved. I just needed to add in the code in the individual pages before the head.php include so I could define it. Thanks for your help! :)
You can use and define a constant like this :
<?php
define( "PAGE_KEY","homepage" );
?>
.
.
.
<body<?php echo " class=\"" . constant( "PAGE_KEY" ) . "\""; ?>>
For another page :
<?php
define( "PAGE_KEY","page5" );
?>
.
.
.
<body<?php echo " class=\"" . constant( "PAGE_KEY" ) . "\""; ?>>
You only change the constant, the rest is the same for every page.
Edit #1:
<body
<?php
define( "PAGE_KEY","homepage" );
echo " class=\"" . constant( "PAGE_KEY" ) . "\"";
?>
>
Edit #2:
<?php
define( "PAGE_KEY","homepage" );
?>
.
.
.
<?php
include( "head.php" >
?>
Now, head.php is something like this :
echo "<body class=\"" . constant( "PAGE_KEY" ) . "\">";
Start with something like this.
<?php
//each page and its class. Many pages can share the same class. If a page doesn't
//have a class, don't include it.
$classes = [
'homepage'=>'home_class',
'page1'=>'base_class',
'page2'=>'home_class',
'page3'=>'special_class'
];
//Adjust this from one page to the next
$this_page = 'homepage';
//get the class corresponding to current page, or '' if no class
$this_class = isset($classes[$this_page])? $classes[$this_page] : '';
?>
//insert the class for this page.
<body class="<?=$this_class ?>">
You can improve on it by moving the $classes array to another file (eg a config file) and including it in all your pages. This way you don't have to rewrite the array on every page (a bad idea because difficult to make a change, easy to make a mistake)

useing $page_title within a echo

I want this to work, it doesn't currently, so, if its possible, what do I need to change:
<?php echo $page_title_LEADER; ?>
In the config file I have
define('ENGLAND_LEADER', 'Bob Smith:');
define('SPAIN_LEADER', 'Stan Smith:');
which when I use:
<?php echo ENGLAND_LEADER; ?>
works fine as you would expect, what I'm trying to do is use the page title to auto fill the COUNTRY name part of COUNTRY_LEADER, so I don't have to manually change the name of the country each time.
NB I do have the $page_title set in the page
You can use the constant function for this
<?php echo constant($page_title . '_LEADER') ?>

Can I extract portions of a php include?

Still learning php as I go so this might just be something I haven't gotten to yet but it's the next roadblock in building my personal site. I have a basic understanding of includes such as linking:
<a href="art.php?id=image id&name=This is my title&menu=side-menu-portfolio">
to pull my includes but I've come to a small problem in that my generic art-gallery page needs to switch between a 'portfolio' header and an 'artwork' header. So I figured I could either build "art-gallery.php" AND "port-gallery.php" and go back and relink everything or just make it so that when you call the link like the above code I just specify which header goes with it. Unfortunately this would also require going back and changing every link. But I noticed that I did state:
...&menu=side-menu-portfolio...
and the pages are already calling side-menu-artwork or side-menu-portfolio so if I could just call in menu and cast aside the 'side-menu-" portion then it would just use artwork or portfolio and call the right header. Unfortunately this is where my limited knowledge of php and syntax come in. I have tried to produce the following code based on my php and js understanding:
<?php include("headlines/headline-" . $_GET[menu - "side-menu-" ] . ".php"); ?>
but I don't know if my syntax is just wrong or if what I'm trying to do is impossible to begin with. Note that when I try this I get
Function Include error of "Warning: include(headlines/headline-.php)"
so it looks like everything else is reading correctly, I just don't know if or how I can extract the word I want from the rest of the menu name.
Should be, Assumed your included file name is headline-side-menu-portfolio.php
<?php
$filename = str_replace("side-menu-", "", $_GET['menu']); // headline-portfolio
include("headlines/headline-" . $filename . ".php"); // headline-portfolio.php
?>
Something like this :
<?php include("headlines/headline-" . $_GET["menu"].".php"); ?>
<!--gives include("headlines/headline-side-menu-portfolio.php")-->
where
$_GET["menu"] = 'side-menu-portfolio'
Try this:
<?php include("headlines/headline-" . $_GET['menu'] . ".php"); ?>
Your code is wrong.
Instead of
<?php include("headlines/headline-" . $_GET[menu - "side-menu-" ] . ".php"); ?>
try
<?php include("headlines/headline-" . $_GET['menu'] . ".php"); ?>
You should check if the file exists before you try including it.
if (file_exists($filesrc)) { ... }
Better yet don't let the user change the menu through a $_GET variable. Instead link to a specific page or pass a variable then decide what menu to get. Like
switch ($_GET['menu']) {
case 'side-menu':
include("headlines/headline-side-menu.php");
break;
}
Just use
$_GET['menu']
, the "side-menu-" part is already in the content of your variable passed as param.
You propably want to do an if .... else so to include one header or another based on the $_GET variable menu.
So something like this will do this:
if($_GET['menu'] == 'side-menu-portfolio') {
include 'headliens/side-menu-portfolio.php';
} elseif($_GET['menu'] == 'side-menu-other') {
include 'headliens/side-menu-other.php';
}
okay....your are almost there....just quotes missing from include syntax...it should be
include("headlines/headline-.php"); /* notice the quotes*/
so it should be
<?php include("headlines/headline-" .$_GET['menu'].".php"); ?>
where $_GET['menu'] should be in the url, like:
art.php?id=image id&name=This-is-my-title&menu=side-menu-portfolio
so what's happening her ??
Upon execution of the line :
<?php include("headlines/headline-" .$_GET['menu'].".php"); ?>
$_GET is fetched from the url and replaced in the header tag, so now the header tag becomes :
<?php include("headlines/headline-"."side-menu-portfolio".".php"); ?> => <?php include("headlines/headline-side-menu-portfolio.php"); ?>
Also. may i suggest that for :
<a href="art.php?id=image id&name=This is my title&menu=side-menu-portfolio">
don't use space in the url, either replace it by - or _

How to include php variable url?

Currently, as an example, I have this php code on one of my pages.
<?php include("codes/games/scripts/titles/angrybirds.php"); ?>
How would I change this to make my variable:
$gametitle = "angrybirds";
work like this:
<?php include("codes/games/scripts/titles/$gametitle.php"); ?>
Thanks in advance for your help!
That code should work as-is, since the . won't be interpreted as part of the variable name. You can see the output of it here: http://codepad.org/ZbtiOPgB
However, for readability, I would encourage you to either use clear concatenation:
<?php include("codes/games/scripts/titles/".$gametitle.".php"); ?>
Or wrap your variable name(s) in { and }:
<?php include("codes/games/scripts/titles/{$gametitle}.php"); ?>
<?php include('codes/games/scripts/titles/'.$gametitle.'.php'); ?>
Or:
<?php include('codes/games/scripts/titles/'.$gametitle.'.php'); ?>
<?php
$file = 'codes/games/scripts/titles/' . $gametitle . '.php';
if (file_exists($file))
require_once($file);
?>

PHP Include with a twist

I have an include
include ('myfile.php');
Now, I am using wordpress and to get the template path I use:
<?php echo get_template_directory_uri(); ?>
My question is:
How can I use both together?
Like:
<?php
include('<?php echo get_template_directory_uri(); ?>/myfile.php');
?>
Thanks
include(get_template_directory_uri() . "/myfile.php");
The include() function just takes a string parameter. Nothing special about it.
You can use the result of the function in the include, but not by echo:ing it. Just use it directly.
<?php
include(get_template_directory_uri() . '/myfile.php');

Categories