php从链接动态生成新的网页 [英] php dynamically generate new web page from link
问题描述
这里是我的代码生成一个摘要:
<?php
if(isset($ _ POST ['search'])){
$ term = mysql_real_escape_string($ _ POST ['keyword']);
$ search = mysql_query(select * from article
where match(title,body)against('$ term'));
if(mysql_num_rows($ search)> 0){
echo< div>。搜索结果为:。< h3& U>中。$术语 < / U>< / H3><。 ; / DIV> 中;
while($ result = mysql_fetch_array($ search)){
echo< div class = \article \>。
< div class = \title\>< a href = \$ result ['cat']。。php。\ >中。$结果[ '标题']。 < / A>< / DIV> 中。
< div class = \body\>。substr($ result ['body'],0,250)。< / div>。
< div class = \cat\>< a href = \$ result ['cat']。。php。\>。 Category:$ result ['cat']。< / a>< / div>。
< div class = \author\>。作者:$ result ['author']。< / div>。
< div class = \dateTime\>。Date:$ result ['date']。< / div>。
< / div>;
}
}
else {
echo对不起!搜索。< h3>$ 。< / h3>。没有结果。
}
}
?>
假设每个文章都有其ID。更改链接以转到动态页面,传递该ID:
< div class = \title\ < a href = \dynamic_page.php?id = $ result [id] \> $ result [title]< / a>< / div>
然后创建一个 dynamic_page.php
该ID并生成文章如下:
if(isset($ _ GET ['id'])){
$ id = mysql_real_escape_string($ _ GET ['id']);
$ q =SELECT
*
FROM
`article`
WHERE
`id` ='$ id'
LIMIT 1; ;
$ q = mysql_query($ q);
if(mysql_num_rows($ q)> 0){
$ result = mysql_fetch_assoc($ q);
echo< div class = \article\>。
< div class = \title\>$ result ['title']。< / div>。
< div class = \body \>$ result ['body']。< / div>。
< div class = \cat\>< a href = \$ result ['cat']。。php。\>。 Category:$ result ['cat']。< / a>< / div>。
< div class = \author\>。作者:$ result ['author']。< / div>。
< div class = \dateTime\>。Date:$ result ['date']。< / div>。
< / div>;
}
其他{
/ *文章未找到* /
}
}
请注意,此次 $ result ['body']
已全部显示。另外我建议在你的情况下使用 mysql_fetch_assoc()
。
代码是 here
i need serious help here! i have this site am building: a summary of each article is generated from the database with the title as a link, but i want it to be that if someone clicks on the link, a page of the article is automatically generated without hustling; for i cannot code a page for every article i have in the database! here's my code that generates a summary:
<?php
if(isset($_POST['search'])){
$term = mysql_real_escape_string($_POST['keyword']);
$search = mysql_query("select * from article
where match(title, body) against('$term') ");
if(mysql_num_rows($search)>0){
echo "<div>"."Search Results For: "."<h3><u>".$term."</u></h3>"."</div>";
while($result = mysql_fetch_array($search)){
echo "<div class=\"article\">".
"<div class=\"title\"><a href=\"".$result['cat'].".php"."\">".$result['title']."</a></div>".
"<div class=\"body\">".substr($result['body'], 0, 250)."</div>".
"<div class=\"cat\"><a href=\"".$result['cat'].".php"."\">"."Category: ".$result['cat']."</a></div>".
"<div class=\"author\">"."Author: ".$result['author']."</div>".
"<div class=\"dateTime\">"."Date: ".$result['date']."</div>".
"</div>";
}
}
else{
echo "Sorry! The search for "."<h3>".$term."</h3>"." gave no results.";
}
}
?>
Assuming each of the articles has its ID. Change the link to go to a dynamic page, passing that ID:
"<div class=\"title\"><a href=\"dynamic_page.php?id=$result[id]\">$result[title]</a></div>"
Then create a dynamic_page.php
that accepts that ID and generates the article as follows:
if (isset($_GET['id'])) {
$id = mysql_real_escape_string($_GET['id']);
$q = "SELECT
*
FROM
`article`
WHERE
`id` = '$id'
LIMIT 1;";
$q = mysql_query($q);
if (mysql_num_rows($q) > 0) {
$result = mysql_fetch_assoc($q);
echo "<div class=\"article\">".
"<div class=\"title\">".$result['title']."</div>".
"<div class=\"body\">".$result['body']."</div>".
"<div class=\"cat\"><a href=\"".$result['cat'].".php"."\">"."Category: ".$result['cat']."</a></div>".
"<div class=\"author\">"."Author: ".$result['author']."</div>".
"<div class=\"dateTime\">"."Date: ".$result['date']."</div>".
"</div>";
}
else {
/* Article not found */
}
}
Note that the $result['body']
is shown in full this time. Also I suggest using mysql_fetch_assoc()
in your case.
The code is here
这篇关于php从链接动态生成新的网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!