PHP - 如何创建动态网址? [英] PHP - How to Create Dynamic URLs?

查看:164
本文介绍了PHP - 如何创建动态网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了一个关于这个简单任务的教程的网页,但是无济于事。所以我转向你有用的同志。这是我需要做的:



我有一个带有事件表的MySQL数据库。我需要创建一个包含事件标题列表的PHP网页,每个标题必须是一个链接到事件的全部细节。但是我想避免为每个事件创建一个静态页面,主要是因为我不希望数据输入志愿者必须创建这些新的页面。 (是的,我意识到静态页面更友好SEO,但我需要放弃在这种情况下为了效率。)



我看过PHP网址语法如下:

  pagename.php?id = 20 
pre>

但我不知道如何使其工作。



任何和所有的帮助非常感谢。



谢谢!



Kip

解决方案

以下是从名为calendar的表提取11月份的事件列表的相关代码,每个事件都有一个名为event.php的页面的链接,并将事件的id字段附加到结尾的URL:

  $ result = mysql_query(SELECT * FROM calendar WHERE sort_month = '11'); 
while($ row = mysql_fetch_array($ result))
{echo
< a href ='event.php?id =。$ row ['id']。 >中$行[ 'EVENT_NAME']。 < / A> 中
;}

这里是event.php页面上的相关代码。请注意,括号中的行号取决于表格中的位置,记住第一行(字段)括号内的数字为0:

  $ id = $ _GET ['id']; 
$ sql =select * from calendar where id = $ id;
$ result = mysql_query($ sql,$ con);
if($ result){
$ row = mysql_fetch_row($ result);
$ title = $ row [12];
$ content = $ row [7];
}
?>

< html>
< head>
< title><?php echo $ title?>< / title>
< / head>
< body>
< h1><?php echo $ title?>< / h1>
< p><?php echo $ content?>< / p>
< / body>
< / html>

感谢上述的帮助,这对我来说很有用。


I've scoured the web for a tutorial about this simple task, but to no avail. And so I turn to you helpful comrades. Here's what I need to do:

I have a MySQL database with an Events table. I need to create a PHP web page with a list of the Event titles, and each title must be a link to the full details of the Event. But I want to avoid having to create a static page for each event, primarily because I don't want the data entry volunteer to have to create these new pages. (Yes, I realize that static pages are more SEO friendly, but I need to forego that in this case for the sake of efficiency.)

I've seen PHP url syntax with something like this:

pagename.php?id=20

but I don't know how to make it work.

Any and all help greatly appreciated.

Thanks!

Kip

解决方案

Here is the pertinent code for extracting a list of events in November from a table named calendar, with each event having a link to a page called event.php and with the event's id field appended to the end of the url:

$result = mysql_query("SELECT * FROM calendar WHERE sort_month='11'");  
while($row = mysql_fetch_array($result))  
{echo  
"<a href='event.php?id=".$row['id']."'>".$row['event_name']."</a>"  
;}  

And here is the pertinent code on the event.php page. Note the row numbers in brackets depends on the placement of such in your table, remembering that the first row (field) would have the number 0 inside the brackets:

$id = $_GET['id'];  
$sql = "select * from calendar where id = $id";  
$result = mysql_query($sql, $con);  
if ($result){  
$row = mysql_fetch_row($result);  
$title = $row[12];  
$content = $row[7];  
}  
?>  

<html>  
<head>  
<title><?php echo $title ?></title>  
</head>  
<body>  
<h1><?php echo $title ?></h1>  
<p><?php echo $content ?></p>  
</body>  
</html>  

This works for me, thanks to the help from those above.

这篇关于PHP - 如何创建动态网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆