1

分类.php

require_once(LIB_PATH.DS."database.php");
require_once(LIB_PATH.DS."database-object.php");

class Category extends DatabaseObjects{

static $db_fields;
static $table_name = "categories";

public function categories(){
global $database;

$sql = "SELECT cat.id, cat.name as category, COUNT(i.category_id) as quantity FROM " . static::$table_name;
$sql .= " as cat JOIN items as i ";
$sql .= " WHERE i.category_id = cat.id";
$sql .= " GROUP BY category ";
$sql .= " ORDER BY category ASC";

$result = $database->query($sql);

$result = $database->query($sql);
 $category = array();

while($cat = $database->fetch_object($result)){
$category[] = "<a href='category.php?id=" . $cat->id . "'>" . $cat->category . "</a>" . ' (' . $cat->quantity . ')';
}
return $category;
}
}

$category = new Category();

Inventory.php <- 我当前的代码

$categories_list = $category->categories();

<div id="categories">
    <ul>
      <?php foreach($categories_list as $cl):; ?>
        <li><?php echo $cl; ?></li>
      <?php endforeach; ?>
    </ul>
</div>

无论如何,我可以让我的代码像这样吗?

$categories_list = $category->categories();

<div id="categories">
    <ul>
      <?php foreach($categories_list as $cl):; ?>
        <li><a href="category.php?id=<?php echo $cl->id?>">$cl->category . " " . $cl->quantity</a></li>
      <?php endforeach; ?>
    </ul>
</div>

您可以在我使用的 Category.php > categories() 上看到

while($cat = $database->fetch_object($result)){
    $category[] = "<a href='category.php?id=" . $cat->id . "'>" . $cat->category . "</a>" . ' (' . $cat->quantity . ')';
}

但我不认为这是写它的正确方法。

我希望有人可以帮助我。

4

0 回答 0