0

カテゴリを取得する際に混乱があり、各サブカテゴリを一度だけエコーしたい..

<?php
mysql_connect("localhost", "root", "")or die(mysql_error())//connect to mysql;
mysql_select_db("ubcommerce")//select database;
$catagories = "";
$sql = mysql_query("SELECT * FROM inventory ORDER BY subcatagory DESC")//select data from table;
$found = mysql_num_rows($sql);
if ($found > 0) {
    while ($row = mysql_fetch_array($sql)) {
        // Gather all $row values into local variables for easier usage in output 
        $subcatagory = $row['subcatagory'];
        $link = $row['catagory'];
        $link = lcfirst($link);
        $catagories .= "<li>$subcatagory</li>";
    }
} else {
    $catagories = "you have no product in your list yet";
}
?>

<html>
    <body>

    <div class="col-content">
        <ul>
<?php echo $catagories; ?>
        </ul>
    </div>

</body>
</html>
4

2 に答える 2

0

コメントで説明されている問題の最も簡単な解決策は、次のようなクエリを記述することです。

SELECT DISTINCT subcatagory FROM inventory ORDER BY subcatagory DESC

(うまくスケーリングできないかもしれませんが)

于 2012-11-18T12:47:29.103 に答える
0

pdoを使用してください;)

<?php
//connect to your db
$bdd = new PDO('mysql:host=localhost;dbname=test', 'root', 'ubcommerce');
//prepare your request
$bdd->prepare('SELECT * FROM inventory ORDER BY subcatagory DESC');
//exec your request
$bdd->execute();
//put your reponse into an array
$rep = $bdd->fetchAll();
//run your array
foreach($rep as $subCategory){
    echo $subCategory['catagory'];
}
于 2012-11-18T10:06:54.317 に答える