检索类别& PHP中的子类别列表

时间:2016-11-23 06:25:46

标签: php mysql

我是HTML,PHP和Linux的新手。 MySql,为noob问题道歉,但是,我需要从数据库中检索类别,这些类别在2个表中,但在点击初始链接后很难调用子类别。

//Category code as...

<?php

require 'dbcon.php';

$sql = "SELECT * FROM `category`";

$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));

echo "<table>
  < tr >
  <th>Category < /th>
  < /tr>";

while ($row = $result ->fetch_assoc()) {
  $id = $row['category_ID'];

  echo "<td><a href=\"subcategory.php?id=$id\">" .$row['description']. "</a></td>";
  echo "</tr>";
}

echo "</table>";

?>


//Subcategory code as...

<?php
require 'database_conn.php';
$sql = "SELECT category.ID, subcategory.title, subcategory.ID FROM category, subcategory WHERE subcategory.ID=category.ID";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));

echo "<table>
  < tr >
  <th>Title < /th>
  < /tr>";
while ($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td>" .$row['title']. "</td>";
  echo "</tr>";
}
echo "</table>";
mysqli_close($conn);

?>

理想情况下,我希望选择一个类别,可以在同一页面或单独的页面中显示整个子类别列表。我已经挣扎了好几天了,我真的很难理解我做错了什么。

1 个答案:

答案 0 :(得分:0)

只需从网址中获取ID并将其放入您的查询

即可
$category_id = $_GET['id'];
SELECT * FROM subcategory WHERE category_id = $id;