我正在尝试创建一个页面,该页面将显示所有主要类别的名称,这些类别不是第一类别ID。
因此会打印出来:
0
我无法弄明白该怎么做。我正在考虑将与我们所在的主要类别相关的每一个都投射到一个数组中然后反向打印出来,但我不确定这是否是最有效/正确的方法。非常感谢任何帮助!!!
dataQuery():
3D Models > Animals > Farm
SQL表:
function dataQuery($query, $params) {
$queryType = explode(' ', $query);
// establish database connection
try {
$dbh = new PDO('mysql:host='.DB_HOSTNAME.';dbname='.DB_DATABASE, DB_USERNAME, DB_PASSWORD);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo $e->getMessage();
$errorCode = $e->getCode();
}
// run query
try {
$queryResults = $dbh->prepare($query);
$queryResults->execute($params);
if($queryResults != null && 'SELECT' == $queryType[0]) {
$results = $queryResults->fetchAll(PDO::FETCH_ASSOC);
return $results;
}
$queryResults = null; // first of the two steps to properly close
$dbh = null; // second step to close the connection
}
catch(PDOException $e) {
$errorMsg = $e->getMessage();
echo $errorMsg;
}
}
控制器:
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(255) NOT NULL,
`main` int(255) NOT NULL,
`name` varchar(255) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
INSERT INTO `categories` (`id`, `main`, `name`) VALUES
(2, 0, '3D Models'),
(1, 0, 'Sound FX'),
(5, 0, 'Textures'),
(3, 2, 'Animals'),
(4, 2, 'Comic Book Characters'),
(6, 3, 'Farm');
型号:
<?php
// Load Language File
include_once(DIR_CATALOG.DIR_LANGUAGE.LANGUAGE."/".DIR."/".FILE.".php");
// Load Model File
include_once(DIR_CATALOG.DIR_MODEL.DIR."/".FILE.".php");
$site_title = $text['site_title'];
$page_title = $text['page_title'];
$breadcrumbs[] = '<a href="?page=categories/list"> '.$page_title.'</a>';
$categories = array();
$main_cats = array();
$results = getCategories();
foreach ($results as $result) {
$categories[] = array(
"id" => $result['id'],
"main" => $result['main'],
"main_name" => array(getMainCategories($result['id'])),
"name" => $result['name']
);
}
?>
查看:
<?php
function getCategories() {
$query = 'SELECT * FROM `categories` ORDER BY `main`,`name` ASC';
$params = array();
$results = dataQuery($query,$params);
return $results;
}
function getMainCategories($id) {
$query = 'SELECT * FROM `categories` WHERE `id` = ?';
$params = array($id);
$results = dataQuery($query,$params);
if ($results[0]['main'] != 0) {
$main_cats[] = getMainCategories($results[0]['main']);
}else{
$main_cats[] = $results[0]['name'];
}
return $main_cats;
}
?>
此代码不会为附加的主要类别打印任何内容。
<?php foreach ($categories as $cat) { ?>
<tr>
<td class="text-center"><?php echo $cat['id']; ?></td>
<td>
<?php
if ($cat['main'] != 0) {
foreach ($cat['main_name'] as $cn) {
echo $cn;
}
}
echo $cat['name'];
?>
</td>
<td class="text-center">
<a href="?page=categories/edit&id=<?php echo $cat['id']; ?>">Edit</a> /
<a href="?page=categories/delete&id=<?php echo $cat['id']; ?>">Delete</a>
</td>
</tr>
<?php } ?>
= var_dump($cat['main_name'])
答案 0 :(得分:1)
你的dataQuery()有点开销。
function dataQuery(){
global $dbh;
try {
$args = func_get_args();
$query = array_shift($args);
$stmt = $dbh->prepare($query);
$stmt->execute($args);
return $stmt->fetchAll();
} catch (Exception $e) {
return $e->getMessage();
}
}
如果我理解你的问题,这会给你一个更好的设计:
CREATE TABLE IF NOT EXISTS `maincategories` (
`id` int(255) NOT NULL,
`name` varchar(255) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `subcategories` (
`id` int(255) NOT NULL,
`main_id` int(255) NOT NULL,
`subname` varchar(255) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `deepsubcategories` (
`id` int(255) NOT NULL,
`main_id` int(11) NOT NULL,
`subid` int(11) NOT NULL,
`subsubname` varchar(255) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1;
INSERT INTO `maincategories` (`id`, `name`) VALUES
(1, '3D Models'),
(2, 'Sound FX'),
(3, 'Textures');
INSERT INTO `subcategories` (`id`, `main_id`, `subname`) VALUES
(1, 1, 'Animals'),
(2, 1, 'Comic Book Characters'),
(3, 1, 'Farm'),
(4, 2, 'cateunderSoundfx'),
(5, 2, 'cateunderSoundfx2'),
(6, 3, 'catunderTexture'),
(4, 3, 'catunderTexture2');
INSERT INTO `deepsubcategories` (`id`, `main_id`, `subid`, `subsubname`) VALUES
(1, 1, 1, 'Farm'),
(2, 1, 2, 'Spiderman Book');
你可以正常加入和显示(见demo):
SELECT m.name as maincat, s.subname as subcat, d.subsubname as deepcat
FROM deepsubcategories d
INNER JOIN maincategories m ON d.main_id=m.id
INNER JOIN subcategories s ON d.subid=s.id
然后在php中,您通常可以这样输出:
function getCategories() {
$query = 'SELECT m.name as maincat, s.subname as subcat, d.subsubname as deepcat
FROM deepsubcategories d
INNER JOIN maincategories m ON d.main_id=m.id
INNER JOIN subcategories s ON d.subid=s.id';
$params = array();
$results = dataQuery($query,$params);
return $results;
}
foreach (getCategories() as category){
echo category['maincat'].' > '.category['subcat'].' > '.category['deepcat'].';
}