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
Related
I've been trying to retrieve the contents of a webpage (http://3sk.tv) using file_get_contents. Unfortunately, the resulting output is missing many elements (images, formating, styling, etc...), and just basically looks nothing like the original page I'm trying to retrieve.
This has never happened before with any other URLs I have tried retrieve using this same method, but for some reason, this particular URL (http://3sk.tv) refuses to work properly.
The code I'm using is:
<?php
$homepage = file_get_contents('http://3sk.tv');
echo $homepage;
?>
Am I missing anything? All suggestions on how to get this working properly would be greatly appreciated. Thank you all for your time and consideration.
Thats normal behaviour, as you are only grabbing the file, and not related images, stylesheets etc...
I have one quick workaround to fix relative paths
http://www.w3schools.com/tags/tag_base.asp
Just add to your code <base> tag.
<?php
$homepage = file_get_contents('http://3sk.tv');
echo str_replace(
'<head>',
'<head><base href="http://3sk.tv" target="_blank">',
$homepage
);
?>
It's should help.
This is to be expected. If you look at the source code, you'll notice many places which do not have a full URL (ex lib/dropdown/dropdown.css). This tells the browser to assume http://3sk.tv/lib/dropdown/dropdown.css. However, on your website, it will be YOURURL.COM/lib/dropdown/dropdown.css, which does not exist. This will be the case for much of the content.
So, you can't just print another website's source and expect it to work. It needs to be the same URL.
The best way to embed another website is usually to just use an iframe or some alternative.
The webpage is not completely generated server-side, but it relies heavily on JavaScript after the HTML part loads. If you are looking for rendering the page as it looks in browser, you may need a headless browser instead - see e.g. this binding to PhantomJS: http://jonnnnyw.github.io/php-phantomjs/
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.
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 have a header.php file containing my DOCTYPE and all my links/scripts.
I use
<?php
// HTML DOCTYPE insert
include 'header.php';
?>
at top of all my pages to have only one header for everyone, and it works fine.
Now, I have another page that get from a database a summary of my products information. When someone click on the "read more" link:
<p>
read more...
</p>
another page opens with the full information displayed...
Actually that works...
BUT on my new page (display_product.php/id=[anynumber]) my included file doesn't work. So I have no nav bar, no scripts, no stylesheet. Only the text from my database.
AND the weird thing is that when I copy/paste the HTML of my generated display_product page and launch it on my browser, it works... O-o
So the generated code is good.
AND the second weird thing is that when I get rid of the /?id... my layout works fine (but I have no text anymore, of course)
Does one of you have an idea why this crazy things happens?
"Hi guys, Thanks very much Fred -ii this was it. it works perfectly. Thanks sergiodebcn for your concerne."
Since other answers were given and did not solve the actual problem, am posting my comment to an answer, in order to close the question.
Remove the slash from /?id
The slash is trying to instruct the server to probably find a folder after a filename, which technically looks like is what's happening here.
The ultimate solution for include and require functions with path issues, is to use the absolute filesystem path to the file that you want to include or require.
i.e you may say:
include("C:\\www\\app\\incs\\header.php");
Hint
To learn how to set the absolute path for include dynamically for your project, check the source code of two files of cakephp framework:
index.php
webroot/index.php
I don't even know if this is possible but hopefully someone will be able to point me in the right direction.
Basically I want to know if there is a way of getting the css class of a div and then displaying content based on that class.
So for example if;
<body class="home">
Then a div would display as follows;
<div><p>This is the Home page</p></div>
Like I said, I don't even know if this is possible but any help would be greatly appreciated.
Thanks
What you're trying to do can be done with Javascript, but if you want to use php only, then try to use php before you provide the "class" parameter. For example, if $_GET['class']=="home" then <div class="<? echo $_GET['class']?>">some text</div>
Perhaps, you can use Javascript, with IDs for example:
<div id="home"></div>
<script>document.getElementById('home').InnerHTML = "this is text for home";</script>
Hope it helps!
See you point, but you goes the wrong way. If you want to put all content in one page differs by some query param, there is no need to do so. You just can hide unneeded blocks with css and show them with js. On the other hand, if this is some sort of server-side utilization, there is definitely no reason to do so too. On the server you can totally control the output, so make separate templates.
Is there a reason not to use PHP instead of reading the class of a div?
#index.html
<html>
<?php include /contentDefinitions.php; ?>
...
<?php $content = home; ?>
...
</html>
#contentDefinitions.php
<?php
if($content = home){
<p>This is the homepage. I am a happy paragraph.</p>
}
?>
**this would be a little more efficient with an array or something,
but at the end of the day the easiest thing would just be to include
home.php, page2.php, page3.php etc. as needed instead of going the
route of variables etc... though having an array would let you edit
all the content within one file.
I'm no master of code and have zero familiarity with Joomla, so this may be absolutely useless to you. :)