如何使用PHP动态生成HTML页面?

时间:2022-11-23 08:17:21

I have a page which displays info about a property, based on the unique ID from the url, searching the mysql database for that ID, getting all the info from that row etc, fairly standard really.

我有一个页面显示有关属性的信息,基于url中的唯一ID,在mysql数据库中搜索该ID,从该行获取所有信息等,真的很标准。

I was wondering if/how I can 'create' a html page for each database row, as I'm assuming this would be better for SEO? Having multiple pages with keywords on rather than one dynamic page?

我想知道是否/如何为每个数据库行创建一个html页面,因为我认为这对SEO更好?有多个关键字而不是一个动态页面?

My properties are added to the database by a form/ upload system on the site, I was thinking creating the page on upload might be easiest, but am open to suggestions!

我的属性通过网站上的表单/上传系统添加到数据库中,我想在上传时创建页面可能是最简单的,但我愿意接受建议!

8 个解决方案

#1


20  

I was wondering if/how I can 'create' a html page for each database row?

我想知道是否/如何为每个数据库行创建一个html页面?

You just need to create one php file that generate an html template, what changes is the text based content on that page. In that page is where you can get a parameter (eg. row id) via POST or GET and then get the info form the database.

您只需要创建一个生成html模板的php文件,该页面上基于文本的内容会发生什么变化。在该页面中,您可以通过POST或GET获取参数(例如,行ID),然后从数据库中获取信息。

I'm assuming this would be better for SEO?

我认为这对SEO更好吗?

Search Engine as Google interpret that example.php?id=33 and example.php?id=44 are different pages, and yes, this way is better than single listing page from the SEO point of view, so you just need two php files at least (listing.php and single.php), because is better link this pages from the listing.php.

搜索引擎作为谷歌解释说example.php?id = 33和example.php?id = 44是不同的页面,是的,从SEO的角度来看,这种方式比单个列表页面要好,所以你只需要两个php文件至少(listing.php和single.php),因为更好的链接这个页面来自listing.php。

Extra advice:

额外建议:

example.php?id=33 is really ugly and not very seo friendly, maybe you need some url rewriting code. Something like example/properties/property-name is better ;)

example.php?id = 33真的很丑,不太友好,也许你需要一些url重写代码。像example / properties / property-name这样的东西更好;)

#2


15  

Just in case someone wants to generate/create actual HTML file...

以防有人想要生成/创建实际的HTML文件......

$myFile = "filename.html"; // or .php   
$fh = fopen($myFile, 'w'); // or die("error");  
$stringData = "your html code php code goes here";   
fwrite($fh, $stringData);
fclose($fh);

Enjoy!

请享用!

#3


7  

As per your requirement you dont have to generate a html page dynamicaly. It can be done by .htaccess file .

根据您的要求,您不必动态生成html页面。它可以通过.htaccess文件来完成。

Still this is sample code to generate HTML Page

这仍然是生成HTML页面的示例代码

<?php

 $filename = 'test.html';
 header("Cache-Control: public");
 header("Content-Description: File Transfer");
 header("Content-Disposition: attachment; filename=$filename");
 header("Content-Type: application/octet-stream; ");
 header("Content-Transfer-Encoding: binary");
?>

you can create any .html , .php file just change extention in file name

你可以创建任何.html,.php文件只需更改文件名中的扩展名

#4


3  

You dont need to generate any dynamic html page, just use .htaccess file and rewrite the URL.

您不需要生成任何动态html页面,只需使用.htaccess文件并重写URL。

#5


1  

I suggest you to use URL rewrite mod is enough for your problem,I have the same problem but using URL rewrite mod and getting good SEO response. I can give you a small example. Example is that you consider WordPress , here the data is stored in database but using URL rewrite mod many WordPress websites getting good responses from Google and got rank also.

我建议你使用URL重写mod足以解决你的问题,我有同样的问题,但使用URL重写mod和获得良好的SEO响应。我可以举个小例子。例如,你考虑使用WordPress,这里数据存储在数据库中,但使用URL重写mod很多WordPress网站得到了谷歌的良好反应,并获得了排名。

Example: wordpress url with out url rewrite mod -- domain.com/?p=123 after url rewrite mode -- domain.com/{title of article} like domain.com/seo-url-rewrite-mod

示例:wordpress url with out url rewrite mod - domain.com/?p=123 url rewrite mode - domain.com/{title of article} like domain.com/seo-url-rewrite-mod

i think you have understood what i want to say you

我想你已经理解了我想说的话

#6


0  

I'll just update the code to contain the changes, and comment it to so that you can see what's going on clearly...

我只是更新代码以包含更改,并对其进行注释,以便您可以清楚地看到正在发生的事情......

<?php   
include("templates/header.htm");   

// Set the default name 
$action = 'index'; 
// Specify some disallowed paths 
$disallowed_paths = array('header', 'footer'); 
if (!empty($_GET['action'])) { 
    $tmp_action = basename($_GET['action']); 
    // If it's not a disallowed path, and if the file exists, update $action 
    if (!in_array($tmp_action, $disallowed_paths) && file_exists("templates/{$tmp_action}.htm")) 
        $action = $tmp_action; 
} 
// Include $action 
include("templates/$action.htm"); 

include("templates/footer.htm");

#7


0  

I've been working kind of similar to this and I have some code that might help you. The live example is here and below, is the code I'm using for you to have it as reference.

我一直在做类似的工作,我有一些可能对你有帮助的代码。现场示例在这里和下面,是我用来为您提供参考的代码。

create-page.php

创建-page.php文件

<?php

// Session is started.
session_start();

// Name of the template file.
$template_file = 'couples-template.php';

// Root folder if working in subdirectory. Name is up to you ut must match with server's folder.
$base_path = '/couple/';

// Path to the directory where you store the "couples-template.php" file.
$template_path = '../template/';

// Path to the directory where php will store the auto-generated couple's pages.
$couples_path = '../couples/';

// Posted data.
$data['groom-name'] = str_replace(' ', '', $_POST['groom-name']);
$data['bride-name'] = str_replace(' ', '', $_POST['bride-name']);
// $data['groom-surname'] = $_POST['groom-surname'];
// $data['bride-surname'] = $_POST['bride-surname'];
$data['wedding-date'] = $_POST['wedding-date'];
$data['email'] = $_POST['email'];
$data['code'] = str_replace(array('/', '-', ' '), '', $_POST['wedding-date']).strtoupper(substr($data['groom-name'], 0, 1)).urlencode('&').strtoupper(substr($data['bride-name'], 0, 1));

// Data array (Should match with data above's order).
$placeholders = array('{groom-name}', '{bride-name}', '{wedding-date}', '{email}', '{code}');

// Get the couples-template.php as a string.
$template = file_get_contents($template_path.$template_file);

// Fills the template.
$new_file = str_replace($placeholders, $data, $template);

// Generates couple's URL and makes it frendly and lowercase.
$couples_url = str_replace(' ', '', strtolower($data['groom-name'].'-'.$data['bride-name'].'.php'));

// Save file into couples directory.
$fp = fopen($couples_path.$couples_url, 'w');
fwrite($fp, $new_file);
fclose($fp);

// Set the variables to pass them to success page.
$_SESSION['couples_url'] = $couples_url;
// If working in root directory.
$_SESSION['couples_path'] = str_replace('.', '', $couples_path);
// If working in a sub directory.
//$_SESSION['couples_path'] = substr_replace($base_path, '', -1).str_replace('.', '',$couples_path);

header('Location: success.php');

?>

Hope this file can help and work as reference to start and boost your project.

希望这个文件可以帮助并作为参考来启动和提升你的项目。

#8


0  

It looks funny but it works.

看起来很有趣,但它确实有效。

<?php 
$file = 'newpage.html';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "<!doctype html><html>
<head><meta charset='utf-8'>
<title>new file</title>
</head><body><h3>New HTML file</h3>
</body></html>
";
// Write the contents back to the file
file_put_contents($file, $current);
?>

#1


20  

I was wondering if/how I can 'create' a html page for each database row?

我想知道是否/如何为每个数据库行创建一个html页面?

You just need to create one php file that generate an html template, what changes is the text based content on that page. In that page is where you can get a parameter (eg. row id) via POST or GET and then get the info form the database.

您只需要创建一个生成html模板的php文件,该页面上基于文本的内容会发生什么变化。在该页面中,您可以通过POST或GET获取参数(例如,行ID),然后从数据库中获取信息。

I'm assuming this would be better for SEO?

我认为这对SEO更好吗?

Search Engine as Google interpret that example.php?id=33 and example.php?id=44 are different pages, and yes, this way is better than single listing page from the SEO point of view, so you just need two php files at least (listing.php and single.php), because is better link this pages from the listing.php.

搜索引擎作为谷歌解释说example.php?id = 33和example.php?id = 44是不同的页面,是的,从SEO的角度来看,这种方式比单个列表页面要好,所以你只需要两个php文件至少(listing.php和single.php),因为更好的链接这个页面来自listing.php。

Extra advice:

额外建议:

example.php?id=33 is really ugly and not very seo friendly, maybe you need some url rewriting code. Something like example/properties/property-name is better ;)

example.php?id = 33真的很丑,不太友好,也许你需要一些url重写代码。像example / properties / property-name这样的东西更好;)

#2


15  

Just in case someone wants to generate/create actual HTML file...

以防有人想要生成/创建实际的HTML文件......

$myFile = "filename.html"; // or .php   
$fh = fopen($myFile, 'w'); // or die("error");  
$stringData = "your html code php code goes here";   
fwrite($fh, $stringData);
fclose($fh);

Enjoy!

请享用!

#3


7  

As per your requirement you dont have to generate a html page dynamicaly. It can be done by .htaccess file .

根据您的要求,您不必动态生成html页面。它可以通过.htaccess文件来完成。

Still this is sample code to generate HTML Page

这仍然是生成HTML页面的示例代码

<?php

 $filename = 'test.html';
 header("Cache-Control: public");
 header("Content-Description: File Transfer");
 header("Content-Disposition: attachment; filename=$filename");
 header("Content-Type: application/octet-stream; ");
 header("Content-Transfer-Encoding: binary");
?>

you can create any .html , .php file just change extention in file name

你可以创建任何.html,.php文件只需更改文件名中的扩展名

#4


3  

You dont need to generate any dynamic html page, just use .htaccess file and rewrite the URL.

您不需要生成任何动态html页面,只需使用.htaccess文件并重写URL。

#5


1  

I suggest you to use URL rewrite mod is enough for your problem,I have the same problem but using URL rewrite mod and getting good SEO response. I can give you a small example. Example is that you consider WordPress , here the data is stored in database but using URL rewrite mod many WordPress websites getting good responses from Google and got rank also.

我建议你使用URL重写mod足以解决你的问题,我有同样的问题,但使用URL重写mod和获得良好的SEO响应。我可以举个小例子。例如,你考虑使用WordPress,这里数据存储在数据库中,但使用URL重写mod很多WordPress网站得到了谷歌的良好反应,并获得了排名。

Example: wordpress url with out url rewrite mod -- domain.com/?p=123 after url rewrite mode -- domain.com/{title of article} like domain.com/seo-url-rewrite-mod

示例:wordpress url with out url rewrite mod - domain.com/?p=123 url rewrite mode - domain.com/{title of article} like domain.com/seo-url-rewrite-mod

i think you have understood what i want to say you

我想你已经理解了我想说的话

#6


0  

I'll just update the code to contain the changes, and comment it to so that you can see what's going on clearly...

我只是更新代码以包含更改,并对其进行注释,以便您可以清楚地看到正在发生的事情......

<?php   
include("templates/header.htm");   

// Set the default name 
$action = 'index'; 
// Specify some disallowed paths 
$disallowed_paths = array('header', 'footer'); 
if (!empty($_GET['action'])) { 
    $tmp_action = basename($_GET['action']); 
    // If it's not a disallowed path, and if the file exists, update $action 
    if (!in_array($tmp_action, $disallowed_paths) && file_exists("templates/{$tmp_action}.htm")) 
        $action = $tmp_action; 
} 
// Include $action 
include("templates/$action.htm"); 

include("templates/footer.htm");

#7


0  

I've been working kind of similar to this and I have some code that might help you. The live example is here and below, is the code I'm using for you to have it as reference.

我一直在做类似的工作,我有一些可能对你有帮助的代码。现场示例在这里和下面,是我用来为您提供参考的代码。

create-page.php

创建-page.php文件

<?php

// Session is started.
session_start();

// Name of the template file.
$template_file = 'couples-template.php';

// Root folder if working in subdirectory. Name is up to you ut must match with server's folder.
$base_path = '/couple/';

// Path to the directory where you store the "couples-template.php" file.
$template_path = '../template/';

// Path to the directory where php will store the auto-generated couple's pages.
$couples_path = '../couples/';

// Posted data.
$data['groom-name'] = str_replace(' ', '', $_POST['groom-name']);
$data['bride-name'] = str_replace(' ', '', $_POST['bride-name']);
// $data['groom-surname'] = $_POST['groom-surname'];
// $data['bride-surname'] = $_POST['bride-surname'];
$data['wedding-date'] = $_POST['wedding-date'];
$data['email'] = $_POST['email'];
$data['code'] = str_replace(array('/', '-', ' '), '', $_POST['wedding-date']).strtoupper(substr($data['groom-name'], 0, 1)).urlencode('&').strtoupper(substr($data['bride-name'], 0, 1));

// Data array (Should match with data above's order).
$placeholders = array('{groom-name}', '{bride-name}', '{wedding-date}', '{email}', '{code}');

// Get the couples-template.php as a string.
$template = file_get_contents($template_path.$template_file);

// Fills the template.
$new_file = str_replace($placeholders, $data, $template);

// Generates couple's URL and makes it frendly and lowercase.
$couples_url = str_replace(' ', '', strtolower($data['groom-name'].'-'.$data['bride-name'].'.php'));

// Save file into couples directory.
$fp = fopen($couples_path.$couples_url, 'w');
fwrite($fp, $new_file);
fclose($fp);

// Set the variables to pass them to success page.
$_SESSION['couples_url'] = $couples_url;
// If working in root directory.
$_SESSION['couples_path'] = str_replace('.', '', $couples_path);
// If working in a sub directory.
//$_SESSION['couples_path'] = substr_replace($base_path, '', -1).str_replace('.', '',$couples_path);

header('Location: success.php');

?>

Hope this file can help and work as reference to start and boost your project.

希望这个文件可以帮助并作为参考来启动和提升你的项目。

#8


0  

It looks funny but it works.

看起来很有趣,但它确实有效。

<?php 
$file = 'newpage.html';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "<!doctype html><html>
<head><meta charset='utf-8'>
<title>new file</title>
</head><body><h3>New HTML file</h3>
</body></html>
";
// Write the contents back to the file
file_put_contents($file, $current);
?>