I have a lot of webpages that all have the same title and the client keeps asking me to change the name of the title, is it possible to use php to update all the titles in the html document at once?
Thanks
edit: I don't have any knowledge of PHP beyond using wordpress, I was thinking it would probably look similar to the get_header link, but when searching PHP get I don't get anything I can make use of.
All you need to do is create a separate file named something like title.php
The only thing you need in this file is something like this:
<?php
$title='titlename';
?>
Then in all of the php files that need this (note, if you've been saving them as .html they may not work; most of the time they will need to be .php) add this at the top of your page
<?php
include_once('title.php');
?>
Below, all you need to do is inside the head tag is this
<title><?php echo $title; ?></title>
Any time you need to change the title name, just change it in the title.php file.
Related
I absolutely don't post a question here in SO unless I really can't find a way to solve my problem myself. I did a lot of googling and was not able to find a solution for this one problem I am about to describe.
Here is the problem. I am creating a templated php website. With templated I mean something like below:
<?php include("header.php");?>
<div id="content">
<div id="main">
<h2><?php echo($page_title);?></h2>
<?php
echo ($page_content);
?>
</div>
<?php include("sidebar.php");?>
</div>
<?php include("footer.php");?>
As you can see here page template ECHOES the content of the $page_content variable between header and footer sections to build the page.
To keep the code clean and separated (in my own way) I have been placing the html content in .txt files (let's say page1_content.txt) and assigning the txt content to this variable ($page_content) as below:
$page_content = file_get_contents("page1_content.txt");
My problem starts when I place some php code in page1_content.txt, lets' call this file page2_content.php (yes, I change the file from .txt to .php). Then I assign the content of this file to $page_content variable as below as usual:
$page_content = file_get_contents("page2_content.php");
Now, when the page template ECHOES page2_content.php contents the php code in it is also echoed as string and not executed, but I am trying to query a database and do some stuff in this file with some php code. I mean, I want the php code inside page2_content.php to be executed and the cumulative html code to be echoed by the "echo" line inside the template file.
How can I achieve this?
Please ask me any questions if you need more info/clarification.
Thanks
EDİT:
As many people here suggested the solution was including the file. Actually, I tried including the file before but it didn't look like it was working, it broke my template, so I though I was on the wrong track and quit the "include" way of doing this. Since everybody here is advising to use include I tried that again. I replaced the php code in "page2_content.php" with a basic 1-line code just to see if it gets executed before adding generated html code without breaking the template and it worked. Apparently my php code had a problem at first place and hence broke my template execution.
Now I have changed the template structure slightly and pages using the template, and it seems to work nicely. Thanks a lot everybody. I have up-voted every answer suggesting that I use include :)
As #Ali suggested, you could include the files. The other option which I highly suggest you do not use is the eval() function.
I think what you want to do is to include your content PHP file, not echo it (as you are doing with header.php and footer.php).
echo($page_content);
Would become as below:
include("page2_content.php");
You've already done this in your footer and sidebar, just use include()
I'm certain I can use PHP to accomplish this task, but I'm not sure how.
What I currently have is a faux news (ha ha ho ho) site for practise here.
http://puu.sh/402Rl.png
For Browse News, I would like all html documents within a specified folder to be shown in the format I have
SAMPLE
<p class="content centeralign">
8.12.13 <!-- ARTICLE NAME -->
</p>
<hr noshade></hr>
Although it's not much, setting up a way to do this automatically would save some time.
Here is how I would imagine the logistics behind this would function ----
All HTML files will be listed in a folder
They will all have a consecutive order based on the date they were created (e.g. 1.html, 2.html, 3.html etc.
PHP would find each document and add it in the right order
A bit inside the file would define the title (meta tags?)
That seems a really bad way of doing it, but anyway.. You want to be you want to be using the directory functions. Specifically http://www.php.net/manual/en/function.readdir.php
A good idea would be to set up a MySQL system for this, but it is achieveable with PHP only.
You could get all .html files in the folder with glob, and then include them with this code:
foreach (glob("/htmlfiles/*.{htm, html}") as $filename) {
include "$filename";
}
The nice thing about this, is that it's sorted alphabetically and numerically too.
Edit: You would then use this system twice, with two folders, one for the meta tags/title, and one for the page itself. Again, not the best way to do it. You should really check out a CMS.
If you used MYSQL, you can still get all html files from the folders as you are now, but just use MYSQL to make simple basic references, such as the filename, date, category, etc. Then you don't need to use complex (and likely failing) file and directory code to determine when a file was created and which order to serve them in. The DATE in your MYSQL would be when to serve them.
To answer your meta questions, I would have a header.php file with all the meta data in for the site (doc declaration, titles, css links, etc) and then each individual file could have a variable to pass to the header when it's included.
eg
File: about.php
$PageTitle = 'About';
include_once('header.php');
<some more code>
<h1>$PageTitle</h1>
File: 1.php
$PageTitle = 'News about something';
include_once('header.php');
<some more code>
<h1>$PageTitle</h1>
File: header.php
usual code, doc declaration, head, etc
<title>$PageTitle</title>
So at the start of each file you declare what the title will be then include header.php. The title is used on the meta title (so your browser and tab etc) and the var in the file can also be used on headers (ie h1, h2) and links if needed.
you could do all this without MYSQL still, but using a database to even just reference things like date_of_creation - last_update_date - author, etc, could save you headaches, but is up to you how you want to do it and what skills you have etc.
This is a really simple one but it's driving me crazy.
I want to import regular HTML code with the help of PHP.
<?php get_thefile ?>
Is that correct? And what shall the thefile.php contain to get it right except my HTML content that I want to import of course!
More information
In a category part of a website i want to put a sidebar that will show some information, this can be anything from html to flash. This content will change time to time so insteed of editing 100 pages i want to just edit this file, My php skill is 0,1% and i can't relly find what i shall exactly do, So what i need is
1: The code to place in the page
2: What i shall put in the file before ? and after ? my content that i want to import
The file will be in the same folder on server as the main page, the page is php and it's wordpress
I'm not sure what you're trying to accomplish, but if it's external HTML, you could use
<?php echo file_get_contents('http://EXTERNAL_URL'); ?>
again, not fully sure what you're trying to do. i hope this helps.
If you just want to directly output the file, use readfile.
You certainly want to use include as described here.
Use $html = file_get_contents('link_to_html.html');
<?php include 'thefile.php'; ?>
include Documentation
I have 2 pages of html: a.html & b.html.
a.html contains text "hello" and a link to b.html.
When I click the link to b.html, I want the "hello" text to also come on specific place in the b.html page.
How can i do this? Kindly let me know.
To bring it along, you'll need to pass it somehow. It could be something you keep in an input element on the screen, then when you submit it (as a form) it will be available as a $_REQUEST element. You could also do this by putting your desired welcome message in a hidden element and submitting it in the same manner.
Alternatively, you could put the value you want in a URL string manually. For example, the link in a.html to go to b.html could be called by:
<a href="b.html?welcome=welcome%20to%20the%20show">
Doing things on the URL string is more PHP than html, and if your server's not setup correctly you may not be able to get values from the URL string in an HTML document. So, in that case it's probably easier just to make it a .php file. Again, not that it can't be done, but it might not be setup to work that way by default.
.html pages are static, so you'd have to copy the desired text manually into the second page.
If you want to do it via PHP, then the simplest way is to put the common text into a file, like:
my_html.html:
<p>This is the common text which should be included in both pages</p>
and
a.php:
... lots of stuff ...
<?php include('my_html.html'); ?>
... more stuff ...
and
b.php:
... other stuff ...
<?php include('my_html.html'); ?>
... more other stuff ...
It sounds like you want to "include" some common text between multiple pages. If you're limited to client-side functionality, it's possible to do this by writing some Javascript to populate an HTML element with a string via DOM, and then include that javascript on both pages. Both HTML pages would have to define the target element.
If you have server-side technology flexibility, I would suggest you look at PHP. Implement both pages as .php pages, and use include() to include a common file (e.g. common.php) in both pages.
You could use php sessions and store the text there so e.g.
a.html
<?php
session_start();
$_SESSION['session_name']="Hello";
echo $_SESSION['session_name'];
?>
b.html
<?php
session_start();
echo $session['session_name'];
?>
A session is more or less a cookie and what you are doing here is storing the string "Hello" in a cookie name "session_name". You can then display the cookies content by using the php echo command :)
I want to use php to easily maintain my website, but I simply can't figure out the language - I've found some tuts online, and some other questions here, but none help me.
I've divided my site into some .php files, header/footer and such - And using
works fine..
Now I want the content of my site, to update according to which menu I click on at my site.
http://dawtano.com/pp/
If I click on "about" I want the "Hello World" to open inside my content div, but I can't get the right php code to do it.
I think you should do this---
Note: This will only work if the CSS styling are on the current directory! ()
<div>
<?php
$html_page = implode('', file('http://dawtano.com/pp/'));
echo $html;
?>
</div>
Hope this helps!
well currently your links are taking you to a separate page entirely. So why not just code it so that your include file is specific to the page. i.e, on about.php, use something like
include 'about_content.php
in your contetnt div.
If you're looking for your content to load dynamically into the content div you'll need to look into using ajax to fetch the content pages.
One popular way to construct the site is to have a single php script which displays content based upon a $_GET variable like 'page' or 'content', and then make the link as:
'http://dawtano.com/pp/index.php?page=helloworldcontent'
Using this method, you would need to check if the variable ($_GET['page']) is set using isset(), and then make sure the string is safe... as anybody with a browser could just type in some mumbo-magic script and hijack your site:
'http://dawtano.com/pp/index.php?page=somecleaverlycraftedhax'
Once it exists and is safe, add the '.php' to the file name and include that file... if it exists! If it doesn't exist, then you will need some code to handle that, probably by displaying a 'File not Found' message, or redirecting home, or something.
I prefer not to do this because it is a pain to make safe, and I feel like it is pretty ugly. What I do instead is put all the header/footer/navbar/title bar scripts into seperate 'display' functions, and put them in another file.
Then include this file with the function definitions, and call all the 'display' functions to set up the page. So every php script in your site might look like:
<?php
include 'html_display_functions.php';
/* put lines here to parse $_GET and $_POST, session_start()/$_SESSION, etc... */
print_html_pre_content();
print '<p>Hello, world!</p>';
print_html_post_content();
?>
Since every script will have this structure, you can just create a template file once. When you want to create a new page for your site, copy the template, rename the copy to the php filename you want, and add content between the two print functions.
You also keep the ability to modify the header/footer/navbar/title bar for the whole site in a central location, namely the included file with the functions.
You might be looking for some sort of Template Engine which allows you to create your pages out of variable parts. You could have a look at TBS, which is more or less what is suggested by the name. But there is a whole lot more engines out there which could do the job.
If that's already too much over the top, maybe Apache SSI (Server Side Includes) are a try for you.
A little suggestion from my side, I am often using Apaches mod_rewrite in connection with a single controller.php file. Apaches mod_rewrite will then send all request to the controller.php which will fetch the appropriate page parts for the requested page using TBS and return the respective page. So you have the controll of the page in one location only.
To your original question about.php could look like:
<?php
include('header.php');
?>
// original page content as html for about.php
// assuming header ends with the starting div <div> where you like the content to appear
// and footer starts with the closing div </div>
// if you need variable content here, simply use <?php echo $your_variable ?>
<?php
include('footer.php');
?>
The best way would be to use a switch statement:
http://php.net/manual/en/control-structures.switch.php
Something like this:
<?php
include("header.php");
$page = $_GET['page'];
switch($page)
{
case "about":
include "about.php";
break;
case "faq":
include "faq.php";
break;
case "help":
include "help.php";
break;
default:
include "home.php";
}
include("footer.php);
?>
Then just make all of your links look like this:
http://www.example.com/index.php?page=home
Just replace home with the correct page.