创建动态链接而不创建新的.php页面/文件

时间:2013-12-11 01:40:26

标签: php mysql dynamic hyperlink

我是PHP的新手,我正在寻找创建动态链接的可能方式,而无需创建新的.php页面/文件

所以,我正在编写一个博客,对这些帖子进行分类。 A,B,C等类别。

我的桌子

id int(11)
title varchar(255)
post_category varchar(50)
post_content text
num_comments int(11)
date_posted date

这就是帖子的类别集

foreach ($category as $cat=>$value) {
    $post_category = "";
    $post_category .= $value." ";
}

带有复选框

<input type="checkbox" name="category[]" value="CategoryA"> CategoryA 
<input type="checkbox" name="category[]" value="CategoryB"> CategoryB
<input type="checkbox" name="category[]" value="CategoryC"> CategoryC

在index.php中,我需要知道如何创建类别A,类别B等动态链接。

这些链接包含根据其类别的帖子而不创建文件CategoryA.php CategoryB.php等等

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

创建一个名为category.php的php文件。然后使用get变量传入您感兴趣的类别。所以例如category.php?cat = A会得到你的类别A.你可以像这样阅读这个变量echo $_GET["cat"]如果你想了解更多信息,请看这里:http://php.net/manual/en/reserved.variables.get.php

在您的SQL中,您需要选择类似的类别:

SELECT * FROM `your_table_name` WHERE `post_category` like '%'".$cat."'%'  

根据您的PHP版本,您应该在用作mysql查询的任何输入周围使用mysql_real_escape_string()。 http://php.net/manual/en/function.mysql-real-escape-string.php

答案 1 :(得分:0)

在您的博客页面上创建一个包含以下内容的链接:

 while ($row = mysqli_fetch_array($posts)) {
     // your code to display your content here
     // this ill create a link with a GET request and is passed with the post ID
     echo '<a href="category.php?id='.$row['category']'"><button class="read-more">Read More
     </button>  </a>'; 
 }

在您的分类页面上:

// get post category from GET
$cat = $_GET['category'];
$dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME) or die('Error connecting to server');
$query = "SELECT * FROM posts WHERE category = '$cat'";
$posts = mysqli_query($dbc,$query);
while ($row = mysqli_fetch_array($posts)) {
    // your code to display your single post content here                   
}

缺点是您没有永久链接到您的内容。它总是看起来像这样 category.php?ID = SomeRandomNumber。但是可能有办法解决这个问题。