我需要使用PHP和MySQL / MariaDB创建一个类别引擎
示例:
SQL(表):
CREATE TABLE `categories` (
`id` int(11) NOT NULL auto_increment,
`parent` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`icon` varchar(255) NOT NULL,
`poster` varchar(255) NOT NULL,
`description` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) engine="MyISAM" DEFAULT auto_increment="1" ;
PHP(使用mysqli,pdo或mysql):
class categories
{
/**
* Fetch all subs( recursive )
*/
public function getSubsById ($id)
{
$rows = array();
$list = array();
$query = mysql_query("SELECT id, name, parent, icon, poster, description FROM categories WHERE parent = '$id' ORDER BY id");
while ($row = mysql_fetch_array($query)) {
$row["subs"] = $this->getSubsById($row["id"]);
$rows[] = $row;
}
return $rows;
}
/**
* Fetch all parents categories
*/
public function fetchParents ()
{
$rows = array();
$query = mysql_query("SELECT id, name, parent, icon, poster, description FROM categories WHERE parent = 0 ORDER BY id");
while ($row = mysql_fetch_array($query)) {
$rows[] = $row;
}
return $rows;
}
}
如果你知道一些更有效的方式,请告诉我它是什么。
也许我应该考虑更改数据库引擎?
...部署APC,Memcache,Cache,Json等是另一个问题......
P.S:我想创建一个像Ebay这样的在线商店系统类别:)