如何使用stmt语句结果组合标题meniu类别和子类别mysqli并按父级显示

时间:2017-01-04 17:06:15

标签: php loops mysqli while-loop

我如何对类别和子类别进行分组,并显示misqli DB对标题meniu的所有结果?我创建了一个名为category的表格,其中包含3列id, category, parent,然后我使用parent将类别分组为类别的值parent=0和子目录值,其中corepuning parent与主要类别。但问题是代码只显示了第一个主要类别和第一个相应的子类别,我想显示所有。

所以我有3个类别值(fruits id=1 , parent=0cars id=2 parent=0clothes id=3 parent=0parent=0,这意味着它们是类别标题),例如对于汽车我有2个子类别值(moto id=52 parent=2 auto id=34 parent=2)你有了这个想法......

$parentTake = '0';
$categorieDrowdown = "";
$categorieDrowdownLista = "";
$stmt = $con->prepare('SELECT id, category, parent FROM category WHERE parent=?');
    $stmt->bind_param('i', $parentTake);
    $stmt->execute();
    $existCount = $stmt->store_result();
        if($existCount == 0){
    echo "nU ai nici o categorie adaugata";
     exit();
        }
    $stmt->bind_result($idParent, $categorie, $parent);
    while ($stmt->fetch())    {
        $idParent;
        $categorie;
        $categorieDrowdown .= '<li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"> '.$categorie.'<span class="caret"></span></a>       
            ';      

    $stmt = $con->prepare('SELECT id, category, parent FROM category WHERE parent=?');
    $stmt->bind_param('i', $idParent);
    $stmt->execute();
    $existCount = $stmt->store_result();
        if($existCount == 0){
    echo "nU ai nici o subcategorie lista adaugata";
     exit();
        }
    $stmt->bind_result($idLista, $categorieLista, $parentLista);
    while ($stmt->fetch())    {
        $idLista;
        $categorieLista;
        $categorieDrowdownLista .= ' <ul class="dropdown-menu">
        <li><a href="#">'.$categorieLista.'</a></li>
            </ul>
        </li>      
            ';
        }//close while subcategorie
    }//close while  categorie first select

和显示

   <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home </a></li>
        <li><a href="#">Admin CMS</a></li>
        <?php echo $categorieDrowdown;//CATEGORIE PRIMA ex: fructe?>    
        <?php echo $categorieDrowdownLista;//CATEGORIE SECUNDA ex:mere?>    

      </ul>

对不起我的英语和我的问题,但我是新手

1 个答案:

答案 0 :(得分:0)

您使用第二个$stmt变量覆盖第一个$stmt,这就是为什么您只获得第一个菜单项和子类别的原因。我已经对您的代码进行了一些调整,并添加了另一种方法,它没有像循环查询那样的性能影响。另外值得一提的是,如果你没有子分类,你就不应该退出脚本。只需创建一个空占位符,即可继续加载页面。

$stmt->store_result();不会返回行数。您可以调用它来访问$stmt->num_rows属性,该属性将为您提供找到的行的结果。请注意,我还没有将结果存储在第二个示例(mysqli::store_result())中,我只是填充$menu数组,甚至不需要知道我是否有结果。

$start = microtime( true );
$output = '';

$stmt = $con->prepare( '
    SELECT
        id, category, parent
    FROM category
    WHERE parent = 0
' );
$stmt->execute();
$stmt->store_result();
// YOU DON'T WANT TO DO THIS... o.O it's fine for debuging tho
//~ if($stmt->num_rows == 0){
    //~ echo "nU ai nici o categorie adaugata";
    //~ exit();
//~ }
// INSTEAD
if( $stmt->num_rows > 0 ){
$stmt->bind_result($idParent, $categorie, $parent);
    while ($stmt->fetch()){
        $output .= '
        <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
                ' . $categorie . ' <span class="caret"></span>
            </a>
            <ul class="dropdown-menu">';

        // rename $stmt to $stmt2 so you'll not overwrite the initial statement
        $stmt2 = $con->prepare('SELECT id, category, parent FROM category WHERE parent=?');
        $stmt2->bind_param('i', $idParent);
        $stmt2->execute();
        $stmt2->store_result();
        // NOR THIS... o.O
        //~ if($stmt2->num_rows == 0){
            //~ echo "nU ai nici o subcategorie lista adaugata";
            //~ exit();
        //~ }
        // INSTEAD
        if( $stmt2->num_rows > 0 ){
            $stmt2->bind_result($idLista, $categorieLista, $parentLista);
            while ($stmt2->fetch()){
                $output .= '
                <li>
                    <a href="#">'.$categorieLista.'</a>
                </li>';
            } //close while subcategorie
        }
        $output .= '
            </ul>
        </li>';
    } // close while  categorie first select
}

echo $output, "\n\n\n";
echo 'Duration: ', microtime( true ) - $start, "\n\n\n";

//-----------------------------------------------------------

// I LIKE IT THIS WAY... ONLY FOR 1 SUBLEVEL THO
// for unlimited dept sub categories you need different approce
$start = microtime( true );
unset( $output );
$output = '';
$menu = array();
$sel = $con->prepare( '
    SELECT
        id, category, parent
    FROM category
    ORDER BY parent, category ASC
' );
$sel->execute();
$sel->bind_result( $id, $category, $parent );
while( $sel->fetch() ){
    if( ! $parent ){ // same as $parent == 0
        $menu[ $id ] = array(
            'name' => $category,
            'sub'  => array(),
        );
    } else {
        $menu[ $parent ]['sub'][ $id ] = array(
            'name' => $category,
        );
    }
}
$sel->close();
foreach( $menu as $id => $item ){
    $output .= '
    <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
            ' . $item['name'] . ' <span class="caret"></span>
        </a>
        <ul class="dropdown-menu">';
    foreach( $item['sub'] as $subid => $subitem ){
        $output .= '
            <li>
                <a href="#">' . $subitem['name'] . '</a>
            </li>';
    }
    $output .= '
        </ul>
    </li>';
}
//~ echo '<pre>', var_dump( $menu ), '</pre>';
echo $output, "\n\n\n";
echo 'Duration: ', microtime( true ) - $start, "\n\n\n";

示例输出:

        <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
                fruits <span class="caret"></span>
            </a>
            <ul class="dropdown-menu">
                <li>
                    <a href="#">apple</a>
                </li>
                <li>
                    <a href="#">orange</a>
                </li>
                <li>
                    <a href="#">banana</a>
                </li>
                <li>
                    <a href="#">Pen Pineapple Apple Pen</a>
                </li>
            </ul>
        </li>
        <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
                cars <span class="caret"></span>
            </a>
            <ul class="dropdown-menu">
                <li>
                    <a href="#">moto</a>
                </li>
                <li>
                    <a href="#">auto</a>
                </li>
            </ul>
        </li>
        <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
                clothes <span class="caret"></span>
            </a>
            <ul class="dropdown-menu">
                <li>
                    <a href="#">skirt</a>
                </li>
            </ul>
        </li>


Duration: 0.0020129680633545



    <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
            cars <span class="caret"></span>
        </a>
        <ul class="dropdown-menu">
            <li>
                <a href="#">auto</a>
            </li>
            <li>
                <a href="#">moto</a>
            </li>
        </ul>
    </li>
    <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
            clothes <span class="caret"></span>
        </a>
        <ul class="dropdown-menu">
            <li>
                <a href="#">skirt</a>
            </li>
        </ul>
    </li>
    <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
            fruits <span class="caret"></span>
        </a>
        <ul class="dropdown-menu">
            <li>
                <a href="#">apple</a>
            </li>
            <li>
                <a href="#">banana</a>
            </li>
            <li>
                <a href="#">orange</a>
            </li>
            <li>
                <a href="#">Pen Pineapple Apple Pen</a>
            </li>
        </ul>
    </li>


Duration: 0.00058293342590332

数据库结构和数据

CREATE TABLE `category` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `category` varchar(255) NOT NULL,
  `parent` int(10) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `parent` (`parent`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

INSERT INTO `category` (`id`, `category`, `parent`) VALUES
(1, 'fruits', 0),
(2, 'cars', 0),
(3, 'clothes', 0),
(4, 'moto', 2),
(5, 'auto', 2),
(6, 'apple', 1),
(7, 'orange', 1),
(8, 'banana', 1),
(9, 'skirt', 3),
(10, 'Pen Pineapple Apple Pen', 1);