简体   繁体   English

如何在php中显示特定类别的帖子?

[英]How to display posts of a particular category in php?

The problem here is , when i type cat='hotels' manually , it displays the detail of that category . 这里的问题是,当我手动输入cat ='hotels'时,它会显示该类别的详细信息。
But when i give cat=$category , (category will be selected by user) , it says that category is undefined variable . 但是当我给cat = $ category时,(类别将由用户选择),它说类别是undefined variable。

What i want is , when i click on any category , the posts related to that category should get displayed . 我想要的是,当我单击任何类别时,应该显示与该类别相关的帖子。

Do help to solve this . 帮忙解决这个问题。

Below is the code . 下面是代码。

Here's the script . 这是脚本。

<script>
   $(document).ready(function(){
   $(".button").click(function(){
   $("#display").load("cat.php");
   });
   });
</script>  

Here's my html. 这是我的HTML。

<ul>            
<li class="button"><a href="#">Hotels</a></li>
<li class="button"><a href="#">Resorts</a></li>
<li class="button"><a href="#">Hills</a></li>
</ul>

<div id="display"></div>

This is my insert.php file. 这是我的insert.php文件。

$id = isset($_POST['post_id'])?$_POST['post_id']:'';
$category=$_POST['cat'];

mysql_connect('localhost','root','');

mysql_select_db("database") or die("Unable to select database");

$insertquery="INSERT INTO posts VALUES('$id','$category')";

$result=mysql_query($insertquery);

if(! $result )
{
  die('Could not enter data: ' . mysql_error());
}
else {

        echo "Entered data successfully\n";
        header('Location:home.php');
    }   

mysql_close();
?>    

This is the code (cat.php) to display it on my home page. 这是将代码显示在我的主页上的代码(cat.php)。

<?php
$row="";

$link = mysql_connect("localhost","root","");
mysql_select_db("database");

$query = "SELECT * FROM posts WHERE cat=$category";

$result = mysql_query($query) or die("Query to get data failed with error:   ".mysql_error());


while($row = mysql_fetch_array($result)) { 

echo    $row['cat'];

}
mysql_close($link);
?>

Change 更改

$query = "SELECT * FROM posts WHERE cat=$category";

To

$query = "SELECT * FROM posts WHERE cat='$category'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM