2013-02-14 107 views
10

我有一个页面,显示有关属性的信息,基于来自url的唯一ID,在该数据库中搜索该ID,获取该行的所有信息等等,真的很标准。如何使用PHP动态生成HTML页面?

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

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

+0

听起来像你需要一个CMS而不是 – 2013-02-14 08:57:48

+3

IM尝试(在某种程度上)创建我自己的,更比您正在使用的网页编程语言是什么东西 – rpsep2 2013-02-14 08:58:49

+0

学习? – Pete 2013-02-14 08:59:26

回答

17

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

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

我假设这会更好的搜索引擎优化?

搜索引擎如谷歌解释说example.php?id=33example.php?id=44是不同的页面,并,这种方式比从视SEO角度单一上市页更好,所以你只需要至少两个PHP文件(listing.phpsingle.php),因为更好地链接listing.php这个页面。

额外建议:

example.php?id=33实在是太丑了,不是很搜索引擎友好的,也许你需要一些URL重写代码。类似example/properties/property-name更好;)

+1

接受这个,因为它给出了一些额外的信息,这是有用的,谢谢你,虐待一些这样做,当我回家 – rpsep2 2013-02-14 13:26:20

7

根据您的要求,你不必生成一个html页面dynamicaly。它可以通过 .htaccess文件完成。

不过这是示例代码来生成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"); 
?> 

您可以创建任意的.html,PHP文件只需更改扩展时,文件名

3

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

13

以防万一有人想生成/创建实际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); 

享受!

+0

我有一个文件在哪里它会打印一个'html'文档。与'html'文档一起,它来自主'php'文档中的变量。我怎样才能从主要的PHP文档打印变量到动态创建的'html'文档? – 2016-07-08 17:49:30

1

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

例如: wordpress url带出url重写mod - domain.com/?p=123 url重写模式 - 域名。像domain.com/seo-url-rewrite-mod

COM/{文章标题}我想你已经明白了我想说你

0

我就更新代码包含的变化,并留言,这样你可以看到发生了什么清楚怎么回事...

<?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"); 
0

我一直有种类似这样的,我有一些代码,可以帮助你。现场示例为here及以下,是我用来作为参考的代码。

创建-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'); 

?> 

希望这个文件可以帮助工作作为参考启动和您的项目。

0

它看起来很有趣,但它的工作原理。

<?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); 
?>