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 _
Related
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"; ?>
I want to use my index.php page as my template for all my other pages. So I'm printing it out with the code below.
echo file_get_contents("index.php");
I've added this piece of code into the template (index.php) where i want to display the contents. of whichever page im on.
<?php
echo $index_content;
?>
So when I use
echo file_get_contents("index.php");
to get my page template, on for example users.php. In the users.php file I want to use the code below
$index_content = echo "string";
to then print out my page contents where I added this variable
<?php
echo $index_content;
?>
My problem is when I say $index_contents = echo ("string");
it's not printing anything out. onto my template. or it prints the stuff out but at the end or the beginning of the template. not where i've inserted my variable. Why wont it echo out my stuff where I've inserted my variable.
file_get_contents() give you the source of your file.
If I get you right you want to use include instead. Also don't echo in a variable but assign the value and echo it in the template.
users.php:
$content = 'what ever';
include 'template.php';
other.php:
$content = 'other page';
include 'template.php';
template.php:
echo $content
If you call users.php output will be "what ever". If you call other.php output will be "other page".
You are storing the return value of "echo" in $index_content, which is empty.
Just omit the echo when assigning the string to the variable.
The other problem is, with file_get_contents you don't evaluate the php expression where you echo out the $index_content.
Instead, you should use include('index.php') in users.php, and set the variable $index_contents before that.
I have php file named http://example.com/result.php?site=qxvyp6g6vefmt6tduifcr3swhuzo0uju&num=2
which when executed, given only top 2 results. Here num is variable and can be changed to any value n to get top n results. This works fine when called from browser address bar.
Now i use to get this file content by
<?php
echo file_get_contents("http://example.com/result.php?site=" . $_GET["site"] . "&num="2"");
?>
On a page having url ABC.com/index.php?site=qxvyp6g6vefmt6tduifcr3swhuzo0uju
This was working fine before adding &num="2" to file_get_contents. but after adding this gives a mysql error. Kindly help.
Just change this line
<?php
echo file_get_contents("http://example.com/result.php?site=" . $_GET["site"] . "&num="2"");
?>
like this
<?php
echo file_get_contents("http://example.com/result.php?site=" . $_GET["site"] . "&num=2");
?>
You are using "" inside "", so php thinks this is not a string. Replace the "" around the 2 with single ones (like '2') and it should work.
I'm a little lost here, hoping that someone can help. I'm using the Meta Box plugin for WordPress, and I'm trying to create a process for the user to select an option from a predefined list, and then assign a URL to that option as a link. Im trying to define the URL in a variable, and then call it in a function, but I'm still a little green on PHP syntax. this is my code now:
<?php
$article_url= rwmb_meta('orion_2016_article_url', 'type=URL');
if (rwmb_meta('orion_2016_article_source') != '') {
echo '<a href= ("$article_url") target=blank>';
echo rwmb_meta('orion_2016_article_source');
echo '</a>';} ?> on <?php the_date(); ?>
Since the options are already predefined, it seems like assigning a random URL to one of the options should be pretty simple. Hopefully this makes sense!
You need to to place variables you wish to echo inside double quotes or simply concatenate strings using . as in my example. Note that I didn't check the plugin's specific syntax, only general PHP syntax.
<?php
$article_url= rwmb_meta( 'orion_2016_article_url', 'type=URL' );
if (rwmb_meta('orion_2016_article_source') != '') {
echo '' . rwmb_meta( 'orion_2016_article_source' ); . '';
} ?> on <?php the_date(); ?>
The title says it all: I want the page to determine the title, but the title is being set before the page is being read (I think). Is there a way to accomplish this, or am I doomed to include the header on each individual page?
Here's what I have:
php.ini:
auto_prepend_file = "header.php"
header.php:
<?php
if (isset($title) == false) {
$title = "foobar";
}
$title = "My Site : " . $title;
?>
<title><?php echo($title) ?></title>
index.php
<?php
$title = "Home"; // ideally this would make the title "My Site : Home"
?>
Instead of using auto_prepend_file, I would just use:
include 'header.php';
An important reason why I wouldn't use auto_prepend_file is, if you move to another server, you'll have to remember to edit the php.ini. If you just include the file, you can move your code to any server.
Also, just like Fred-ii- said, I wouldn't use parenthesis. Also, you are missing a semi-colon after the echo.
To take that a step further, I would create a file called something like $config.php or $vars.php. Include that before everything and have it define all your global variables and constants.
I would check this out: http://php.about.com/od/tutorials/ht/template_site.htm
This is not an ideal answer, but I could use CGI variables to get the name of the page, then turn that into a title.
function get_title($page){
$title = str_replace("/", "", $page);
$title = str_replace("_", " ", $title);
$title = str_replace(".php", "", $title);
$title = ucfirst($title);
if($title = "Index"){
$title = "Home";
} elseif ($title == "") {
$title = 'Foobar';
}
return $title;
}
$title = get_title($_SERVER["PHP_SELF"]);
$title = 'My Site: ' . $title;
As a follow-up to my original comment, I'm posting this as an answer because while it doesn't specifically solve the problem, it addresses the underlying cause.
Disclaimer: The code below has many problems, especially security, it's not meant to be copied directly but only explains the concept.
What you need to do is have a container file that includes your headers and whatever else, and each PHP file is included from there. For example, name your container index.php, and have the following in it:
<?php
include 'header.php';
if ($_GET['page'])
include $_GET['page'].'.php';
include 'footer.php';
?>
Then each PHP page you have will be wrapped in the index.php file, and you can add whatever you want in the header file which will be included in all of your files. That way you don't have to include anything in the individual page files.
The client will access your pages with a query string, such as: index.php?page=test
Again, for security reasons you will still want to include basic checks in each individual file, but technically this can be avoided in you plan for this. You definitely won't need to include huge headers in each file, like MySQL connections etc. Also for security you should have stringent checks on your $_GET variables to make sure that only the pages you want can be included.
I'd define a writeTitle (or similar) function in the header.php file which you're auto_prepending:
header.php
<?php
function writeTitle($title = 'foobar') {
$title = "My Site : " . $title;
return '<title>' . $title . </title>';
}
And then you can just call the function from your page scripts instead of setting a variable:
index.php
<?php
echo writeTitle('Home');