我的数据集类似于:
---------------------------------------------------------------------
id | category | sub_category | category_href | sub_category_href |
01 | cat_1 | sub_cat 1 | cat/cat1.php | cat/cat1/sub_cat1.php|
02 | cat_1 | sub_cat 2 | cat/cat1.php | cat/cat1/sub_cat2.php|
03 | cat_2 | sub_cat 1 | cat/cat2.php | cat/cat2/sub_cat1.php|
04 | cat_2 | sub_cat 2 | cat/cat2.php | cat/cat2/sub_cat2.php|
---------------------------------------------------------------------
我想对数据做的是这样的布局:
<div>
<h2 class="title">Cat1</h2>
<p>
<ul class="links">
<li><a href="cat/sub_cat_1.php" target="_top">sub_cat_1</a></li>
<li><a href="cat/sub_cat_2.php" target="_top">sub_cat_2</a></li>
</p>
</div>
<div>
<h2 class="title">Cat2</h2>
<p>
<ul class="links">
<li><a href="cat/sub_cat_1.php" target="_top">sub_cat_1</a></li>
<li><a href="cat/sub_cat_2.php" target="_top">sub_cat_2</a></li>
</p>
</div>
所以我的问题是如何使用php进行此操作,以便我可以更改数据库中的类别和子类别。以下是我在下面的内容,但我需要为子类别设置第二个循环,或者每个类别只有一个子类别。有人能指出我在sub_cat循环的正确方向。谢谢
编辑:
所以我现在的问题是,我有不同的类别如何回应适当的子类别?
由于
<?php
include('connect.php');
$result = mysql_query("SELECT DISTINCT category FROM categories")
or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo "<div>";
echo "<h2 class='title'>" . $row['category'] . "</h2>";
echo "<p>";
echo "<ul class='links'>";
$result1 = mysql_query("SELECT * FROM categories ")
or die (mysql_error());
while ($row = mysql_fetch_array($result1)) {
echo "<li><a href='" . $row['sub_category_href'] . " target='_top'>" . $row['sub_category'] . "</a></li>";
}
echo "</ul>";
echo "</p>";
echo "</div>";
}
?>
答案 0 :(得分:1)
该代码可能如下所示:
<?php
include('connect.php');
$result = mysql_query("SELECT DISTINCT category FROM categories")
or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo "<div>";
echo "<h2 class='title'>" . $row['category'] . "</h2>";
echo "<p>";
echo "<ul class='links'>";
$result1 = mysql_query("SELECT * FROM categories WHERE category = {$row['category']}")
or die (mysql_error());
while ($row1 = mysql_fetch_array($result1)) {
echo "<li><a href='" . $row1['sub_category_href'] . " target='_top'>" . $row1['sub_category'] . "</a></li>";
}
echo "</ul>";
echo "</p>";
echo "</div>";
}
?>
但我建议你以这种方式进行优化:
<?php
include('connect.php');
$result = mysql_query("SELECT * FROM categories")
or die(mysql_error());
$cats = array();
while ($row = mysql_fetch_array($result)) {
$category = $row['category'];
$sub_category = $row['sub_category'];
$sub_category_href = $row['sub_category_href'];
$cat_hrefs[$category][$sub_category] = $sub_category_href;
}
foreach ($cat_hrefs as $category => $sub_category_hrefs) {
echo "<div>";
echo "<h2 class='title'>" . $category . "</h2>";
echo "<p>";
echo "<ul class='links'>";
foreach ($sub_category_hrefs as $sub_category => $sub_category_href) {
echo "<li><a href='" . $sub_category_href . " target='_top'>" . $sub_category . "</a></li>";
}
echo "</ul>";
echo "</p>";
echo "</div>";
}
?>
您不需要进行三次查询,因为您可以在一个查询中执行此查询,这更为理想。
答案 1 :(得分:0)
数据库中不需要四列sub_category&amp; sub_category需要另外2个category_href,sub_category_hrefare冗余,因为可以从前2个生成。
此版本使用PDO,建议用于新应用程序。
<div>
// connect to database
<?php
$host= "xxx";
$username="xxx";
$password="xxx";
$database="xxx";
// Opens a connection to a MySQL server
$connection=mysql_connect("localhost", $username, $password);
try {
// DBH means "D Handle"
// MySQL with PDO_MYSQL
$DBH = new PDO("mysql:host=$host;dbname=$database", $username, $password);
}
catch(PDOException $e) {
echo $e->getMessage();
}
// creating the statement
$STH = $DBH->query('SELECT * FROM category LIMIT 0 , 30');
// setting the fetch mode
$STH->setFetchMode(PDO::FETCH_ASSOC);
//Set up flag
$test = 0;
while($row = $STH->fetch()) {
if($test == 0){
echo "<h2 class= \"title\">".str_replace('_','',ucfirst($row['category']))."</h2>";
$test = 1;
}
else{
$test = 0;
}
echo "<li><a href=\"cat/".$row['category']."/".$row['sub_category'].".php\" target=\"_top\">".$row['sub_category']."</a></li>";
}
// close the connection
$DBH = null;
?>
</div>