My problem is that the webpage that I do is in complete PHP files. Everywhere there is tutorial how to generate, but there is no instructions what to do when it is a PHP file.
The main file is also index.php. When I insert the code that is generated in HTML.
Comes with a lot of errors, so I convert it to PHP.
it will start like
echo "..bla bla" "\n" ;
echo "<div property=\"gr:legalName\" content=\"G & Sziladi\"></div>\n";
which is good already, google recognize it, BUT this will show on the top of my page.How can i insert this markup into index.php without showing,but also google has to recognize it properly.
If you prefer to copy and paste HTML, the paste it into a PHP file but outside of the PHP tags:
<?php
/*
* May brand new PHP Website
*/
$title_prefix = '- Escaping the Tags';
?>
<html>
<head>
<title>Supersite Deluxe <?php echo $title_prefix; ?></title>
</head>
<body>
....
</body>
</html>
That is especially useful if you have to a lot of HTML, because you can just paste it in.
See Escaping from HTML
Related
I am building a small personal website. To keep things small and to avoid writing things over and over again, I have a single header.php file that every sub-directory index.php file include's. So, every index.php file has these lines:
$title = someTitleVariableOrMethod;
include('/var/testsite/docroot/header.php');
And in my header.php file, I have these lines (I know I could probably improve the formatting, but first I want to get the title working).
<html>
<head>
<?php
if (isset($title)) {
echo '<title>' . $title . '</title>';
} else {
echo '<title>Sampletext</title>';
}
?>
<style>
//a bunch of irrelevant css
</style>
</head>
<body>
//this is the end of the header file, the rest is dealt with in the index.php file
But for some reason, the contents of the title and all my CSS show up at the start of <body> (I see this when I press F12 in browser) and NOTHING at all shows up in <head>. I just want the title contents to be put in the title tag. How could I fix this issue?
Thanks in advance.
Make sure that you are accessing the pages from a web server (e.g., XAMPP - then access via http://localhost/site/index.php). If you try to open them directly from your file system the PHP will simply be shown as you described. Also make sure the index.php has the closing body and html tags.
Sorry for the trouble, I found the issue! I was using passthru to get to content for the variable, which I didn't realize prints to the screen. My mistake!
EDIT: The site says I can't accept this answer for two days, but this is accepted
I decided to build a booking calendar and embed it to my website. But I don't know how to include it in the html file. I tried the <?php ?> tag but it doesn't display at all. I looked around and found this htaccess file thing. How do I generate it? Is it a way to use my php code without this?
Here is what included in my big html file (I did not include other html here)
<!DOCTYPE html>
<hmtl>
<head>
</head>
<body>
<?php
include 'calendar.php';
$calendar = new Calendar();
echo $calendar->show();
?>
</body>
</html>
I used sublime text edit and there is no "color" on the key words meaning it is not recognizing the php tag.
Place the php code of the calendar in a file and save it as say, calendar.php and then add the following line in the pages where you want the calendar to appear in:
<?php include("calendar.php"); ?>
N.B. Make sure to replace the file extension of all your html files with .php extension instead of the .html extension.
For example: index.html should be index.php
I am looking for a simple and effective way to create a pure html file based off a php file. For instance, in template.php below the php would be inserting various portions of the page. I want to save the page then as html removing all php code and leaving what was inserted by it... hopefully that makes sense... the output of template.php would be a better way to say it I guess.
First, I do not know if something like this is possible. Second, is this the best way to go about something like this?
Before anyone starts screaming about security there will be ZERO user submitted / form submitted variables in this page. My goal is to create a report from database values with the template which the user can then view/print/save off the server as pure html. There will be no images only inline css.
EDIT :
This html only output of template.php needs to be saved on the server as its own file. The reason for the php 'template' is because I will be creating the vast majority of the page with php... but I only want to save its resulting output.
template.php :
<!DOCTYPE html>
<html lang="en">
<!-- BEGIN HEAD -->
<head>
<meta charset="utf-8" />
<title><?php echo $title; ?></title>
<meta content="<?php echo $desc; ?>" name="description" />
</head>
<!-- END HEAD -->
<!-- BEGIN BODY -->
<body>
... further html with php mixed in
</body>
<!-- END BODY -->
</html>
Current solution :
I did some further research and this is acting exactly how I want it to. Comments/suggestions welcome for it.
<?php
ob_start();
require_once('/home/test/public_html/template.php');
if ( ob_get_length() > 0 )
{
$ssReport = ob_get_contents();
file_put_contents('/home/test/public_html/test.html', $ssReport);
}
ob_end_clean();
?>
You can use REGEX (Regular Expressions) to strip out all php-code.
The Expression <\?php.*?\?> can do that for you...
PHP-Example:
$tempateFile = file_get_contents('template.php');
$htmlPlain = preg_replace('/<\?php.*?\?>/si', '', $tempateFile);
If you allow "Shorthand Open Syntax" for php-files <? instead of <?php you should use the pattern <\?(?:php)?.*?\?> which will become preg_replace('/<\?(?:php)?.*?\?>/si', '', $tempateFile);
If you want to take a html-snapshot of your site(s) try this options:
use wget to grab a copy of your website (see wget manual or wget tutorial)
use curl with a script to grab a copy of your website (see curl manual or
curl tutorial
make a php-script which uses file_get_contents($url) to grab outputs of you specific public-available URL and store it with file_put_contents(...) to a html-file.
Let's try to explain the title of my question to be the more concise I can: I'm basically designing a static HTML website from scratch. Nothing to worry about here.
The point is that I'm trying to include some links that will retrieve some items (a product inventory) from a database (and therefore the site won't be so 'static' anymore), as there're > 300 products and creating an html for each one is not feasible.
After googling and reading several sites for days, the "easiest" solution I came up with is to use PHP and MySQL. Again, nothing to worry about. Just took my time for reading documentation and move along.
My question is more related about the correct workflow for integrating both worlds. Let's see my idea in code:
This is one schematic example of the page where you can browse some products (e.g: product.html):
<html>
<head>
<title>My Site - These are our products</title>
</head>
<body>
<!--Site goes here-->
Search by name
Search by color
<!--rest of site goes here-->
</body>
</html>
Where the links
product_search_by_name.php
product_search_by_color.php
are actually a modified clone of the same page (product.html). This is, keeping same html code, plus the .php code embedded into it, as I want to have the DB results displayed into a div on that same page, keeping exactly same layout.
So, am I doing this right if I want to maintain the appearance of the whole website? I'm absolutely wrong from the base and should start again? Should I give up and work selling frappuchinos on a Star*ucks?
As a sample of the idea I want to achieve is the following: http://www.w3schools.com/tags/default.asp (when you click on the left menu bar, the center zone updates with the content). By the way, are they using AJAX on that website to update just the center zone, or I'm misunderstanding what is AJAX for?
I'm sure I'm missing something but I'm too confused to separate the sheep from the goats, so I'd thank a lot any tips you can give to me (and additional documentation on the internets to read as well).
There are two main ways to merge or migrate from static HTML to dynamic HTML (PHP, PERL, whatever).
(1) One is to have most of the contest as HTML, and the stuff like inventory as dynamic.
<html>
<head>
<title>My Site - These are our products</title>
</head>
<body>
<h1>My Site - These are our products</h1>
<?php
// php code to retrieve links
?>
</body>
</html>
(2) To have a full PHP site.
<?php
echo "<html>" . "\n";
echo "<head>";
echo "My Site - These are our products"
echo "</head>";
echo "<body>" . "\n";
// php code to retrieve links
echo "</body>" . "\n";
echo "</html>" . "\n";
?>
Many developers start by merging both HTML & PHP.
I suggest to learn how to do a very simple but full php site, connect to a database, retrieve some records with a S.Q.L. query, display them as read-only text or links, and later you may change to the other HTML plus PHP way of doing things.
There are several editors and tools to help develop in PHP, specially by looking for a PHP function, or just highlight HTML tags. Scintilla (Linux) or Notepad++ in windowze, its a very simple yet useful tool.
Cheers.
Well, if you want it to make it using Ajax ...
You might want to do the following.
Create the two files as you said, those are not to be included in the HTML file.
Create a JavaScript function to call the files, You can use jQuery api, to link to php. http://api.jquery.com/jQuery.post/ or http://api.jquery.com/jQuery.get/
Link the click of the button inquired to call the JavaScript function, and add html you get from php to the tag you want.
You can create a index.php page and put down your html code in it. It looks like this
<html>
<body>
<!--- links goes here -->
Search Products
</body>
</html>
If you are retrieving products from a database and if you want to create multiple links for the products create a function in your function.php which pulls all the product name from the database. Now add this function into your index.php
index.php
<?php include ('functions.php')?>
<html>
<body>
<h3>Product List</h3>
product name
</body>
</html>
Always embed html in php file. Do not embed php in html file. File should have .php extension.
How can I replace the text on the active document body on a PHP file on code execution?
Do I have to import a file, replace text on it and then echo it or can I just manipulate the document I run the PHP script on?
I am trying to use templates for easier HTML editing like :usernamecomeshere: and then replacing that :usernamecomeshere: with the actual value. I am wondering If I can do it on one file only instead of loading a file and then displaying it.
If I'm getting your question correctly, you don't need to important a file and echo the document. You can directly manipulate the document itself. For example, in the below sample, you can directly echo the contents of $username in a way that's interspersed with HTML code.
index.php
<?php
// handle code to login
$username = "David";
?>
<html>
<body>
<p>Hello, your username is <?php echo $username ?></p>
</body>
</html>
Worth pointing out is that PHP itself is a templating engine. If you want to replace text, you can do it using PHP such as:
<?php
$user = 'Ugur';
?>
<html><head></head>
<body>
<h1>Hello <?php echo $user; ?></h1>
</body>
</html>
Beyond this sort of simple usage, you may want to look at various template engines, which allow you to do much more elegant things, but are more complex. Take a look at mustache, perhaps?
If you're trying to make these modifications after the page has loaded, remember that PHP runs on the server-side, not the user-side. For that, you need Javascript.
you'll want to look up str_replace() on google. You can search an entire string and replace specified keywords simply.