First of all, I know that there are many questions leading towards including HTML.
The thing is, when I include one HTML (1) file into another (2), using <?php include("1.html") ?>, and both files consist of something like this:
<html>
<body>
<div id="specific div">
<span id="span1">1</span>
</div>
</body>
</html>
Having two different spans in the same specific div - once I include one file into the other one, it would look like this:
<html>
<body>
<div id="specific div">
<span id="span1">1</span>
</div>
<div id="specific div">
<span id="span2">2</span>
</div>
</body>
</html>
while I want the contents of the specific div merged into one of them, instead of having to divs with the same id in the end:
<html>
<body>
<div id="specific div">
<span id="span1">1</span>
<span id="span2">2</span>
</div>
</body>
</html>
How do I achieve that?
EDIT: I found a different and less complicated solution for my specific situation. Therefore I can't really select the correct answer now, so I might select one if it gets enough upvotes.
You could use php's DomDocument::loadHTMLFile() function. With this you can load both of your files and merge them the way you like it.
If your file looks like you said, something like this:
<html>
<body>
<div id="specific div1">
<span id="span">bla bla bla</span>
</div>
</body>
</html>
You can use the DomDocument:
$dom1 = new DomDocument();
$dom1->loadHTMLFile("file_1.html", LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$dom2 = new DomDocument();
$dom2->loadHTMLFile("file_2.html", LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$element = $dom2->getElementById("specific div2")->firstChild;
$dom1->getElementById("specific div1")->appendChild($element);
$merged_html = $dom1->saveHTML()
So this would merge the contents of the div[id="specific div2"] to the div[id="specific div1"]
DOMDocument also supports xpath if you like it more than going through the nodes maually or selecting by id.
If you want to include one html file to another then you can either use iframe or can convert the files to php as well to use include.
Like if you want to include index2.html in index1.html then you can use below iframe code in index1.html:
index1.html
<iframe src="index2.html"></iframe>
Or you can convert your files to PHP and then simply include one file to other like below:
index1.php
<?php include('index2.php'); ?>
You can also use Server Side Include like below to include one html to another:
index1.html
<html>
<body>
<!--#include virtual="/index2.html" -->
</body>
</html>
Related
I am using one header file for every page which will show the HTML head(which includes meta tags and other CSS links)
Here I have used everything as dynamic as if I like in the case of canonical tags I have used $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_HOST'] and all other links to CSS are also accessible even if it can be any file from any directory.
Now I want to create a one-time title meta tag & description tags like this will be my main file looks like
<!DOCTYPE html>
<html lang="en">
<head>
<title><?php echo $page_title; ?></title>
<meta name="description" content="<?php echo $page_content; ?>" />
//Other meta tags & linking to css & js files
</head>
<body>
// Content ........
</body>
</html>
Now here is the twist is that many people will say that just use
$page_title = "Here is the title of the specific page";
$page_content = "Here goes the long-form description describing the page of specific page";
Yes, this would have worked out if the pages were different and had the same place to put that all before including the header file.
But my index page looks like this let me explain it too. (Using Bulma as a framework)
<?php
require 'include/db_connect.php';
require 'include/header.php';
?>
<form name="submitform" method="POST">
<div class="columns is-multiline" id="wrapper">
<div class="column is-6-desktop is-12-tablet" id="main_content">
<div class="box">
<?php
if($country)
{
?>
<?php require 'inc/country.php'; ?>
<?php
}
else if($country && $state)
{
?>
<?php require 'inc/country_state.php' ?>
<?php
}
else if($country && $state && $district)
{
?>
<?php require 'inc/country_state_district.php'; ?>
<?php
}
else
{
?>
<?php require 'inc/other_than.php'; ?>
<?php
}
?>
</div>
</div>
</div>
</form>
<?php require 'footer.php'; ?>
</body>
</html>
The main point here is that I am using the dropdown button which gets auto-submitted using js that's not relevant here but from the top, I have just explained what is the structure of my code.
Now as you can see the if-else structure which includes other files that create dynamic pages but their code starts only from the body and not from the head directly so I am not able to add those title tags and descriptions.
Now how to add title & description tags uniquely to each of these pages.
Any solutions, please... Thanks in Advance
Like already written in the comments. You should define the vars ahead of require 'include/header.php'; or use an MVC structure.
If you still want to stay with the existing code ob_get_contents() might help you.
Use ob_start(); at the beginning to tell PHP to not print any output and instead write the output to a buffer.
You can then at a later point use ob_get_contents(); to fetch the output buffer and print it.
You have to search in the output buffer for an identifier like %page_title% then and replace it with the actual value before sending the output.
This may look like following:
echo str_replace(%page_title%, $page_title, ob_get_contents());
Still I'd rather suggest restructuring your code, as the solution with output buffer is slower, uses more memory and is poor to debug.
I make a new website for my company and i want to use smarty (v3.1.29) for it. Now the problem is that we store the code for all pages in our database (home, products, downloads, ...). Some pages contains PHP inline functions like:
<?php include("functions.php"); ?>
<p> Hello <?php echo printWorldInColor(); ?> </p>
In my template (.tpl) file I have a div-section to load the content from our database:
<html>
<body>
<div id="content">
{$content}
</div>
</body>
</html>
So my code looks like this afterwards:
<html>
<body>
<div id="content">
<?php include("functions.php"); ?>
<p> Hello <?php echo printWorldInColor(); ?> </p>
</div>
</body>
</html>
Is there a way that smarty processes the PHP-code before parsing it?
Hint: I dont want to edit my template file. I just want to parse the database content to my content-section of the website.
What i tried:
saved database-content into a string and replaced PHP-tags with smarty-PHP-tags, then assign it to the template
SmartyBC-Class
You can't do that in Smarty. Also running php code stored in the database sounds like a terrible idea. But if for some reason you have to go on with this nonsense (and considering that you can't use eval), you can try this:
Read the php code from the database.
Save to a temporal php file
Turn on output buffering with ob_start()
include the file you have created
assign the output to a variable with ob_get_clean()
assign the variable to the template
But if I was you, I would try to do the project in another way.
I have searched through some other threads here but have not found the perfect solution
if I have the following layout
<html>
<div> <---- This one
<div> text </div>
<div> text </div>
</div>
<p> text </p>
<div> <---- This one
<div> text </div>
<div> text </div>
</div>
<p> text </p>
</html>
How would I go about getting only the divs on the top level. (NOTE: the two divs inside is only an exmaple, there could be just one, or could be 5 or 6.
Note: The rest of the code that this ties into is using the Simple html dom, I need this to work with that.
One possibiltity is to use XPath. It will look something like the following:
<?php
$doc = new DOMDocument();
// one of the few cases where you may use error suppression
#$doc->loadHTML($yourHtml);
$xPath = new XPath($doc);
$nodes = $xPath->query('//html/div');
Disclaimer: I haven't tested this, but it should at least get you close
we are working on a project, in which we need to appendChild and ReplaceChild within the PHP DOM, and then save the file.
PHP Code
<?php
$string = '<u>technical/u>';
?>
<html><head></head>
<body>
<div>Stack Overflow is great website for technical professionals.
<p>checking all of the websites</p>
</div>
<h1>Looking something new for a technical professional</h1>
</body>
</html>
we just want to replace $string with duplicated words like "technical" is used in and .
The final HTML code will be
<html><head></head>
<body>
<div>Stack Overflow is great website for <u>technical</u> professionals.
<p>checking all of the websites</p>
</div>
<h1>Looking something new for a <u>technical</u> professional</h1>
</body>
</html>
Within my HTML, I have a php script that includes a file. At that point, the code is indented 2 tabs. What I would like to do is make the php script add two tabs to each line. Here's an example:
Main page:
<body>
<div>
<?php include("test.inc"); ?>
</div>
</body>
And "test.inc":
<p>This is a test</p>
<div>
<p>This is a nested test</p>
<div>
<p>This is an more nested test</p>
</div>
</div>
What I get:
<body>
<div>
<p>This is a test</p>
<div>
<p>This is a nested test</p>
<div>
<p>This is an more nested test</p>
</div>
</div>
</div>
</body>
What I want:
<body>
<div>
<p>This is a test</p>
<div>
<p>This is a nested test</p>
<div>
<p>This is an more nested test</p>
</div>
</div>
</div>
</body>
I realise I could just add leading tabs to the include file. However, VS keeps removing those when I format the document.
In your test.inc file, you can use output buffering to capture all the output of the PHP script, before it is sent to the browser. You can then post-process it to add the tabs you want, and send it on. At the top on the file, add
<?php
ob_start();
?>
At the end, add
<?php
$result = ob_get_contents();
ob_end_clean();
print str_replace("\t" . $result, "\n", "\n\t");
?>
I don't necessarily subscribe to this solution - it can be memory intensive, depending on your output, and will prevent your include file from sending partial results to the client as it works. You might be better off reformatting the output, or using some form of custom "print" wrapper that tabs things (and use printing of heredocs for constant HTML output).
Edit: Use str_replace, as suggested by comment
I don't think your solution can be done easily. You might consider using HTML Tidy to clean your source code before presenting it to a client. There are good tutorials for it on the internet.
The easiest solution is to add leading tabs to the include file, but instead of using literal tabs, use the \t escape sequence.