1

我有一个类似于以下内容的数据集:

---------------------------------------------------------------------
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>";
}
?>
4

2 回答 2

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>";
}
?>

您不需要进行三个查询,因为您可以在一个查询中完成,这是更优化的。

于 2012-09-30T21:16:51.897 回答
0

数据库中不需要四列,只需要 sub_category 和 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>
于 2012-09-30T22:19:59.947 回答