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