Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用嵌套的do while循环从数据库中获取菜单和子菜单?_Php_Mysql_Nested Loops - Fatal编程技术网

Php 如何使用嵌套的do while循环从数据库中获取菜单和子菜单?

Php 如何使用嵌套的do while循环从数据库中获取菜单和子菜单?,php,mysql,nested-loops,Php,Mysql,Nested Loops,我希望有人能在php代码方面帮助我。我想做的是从两个表中使用嵌套的do while循环从数据库中获取菜单和子菜单,这样它就会显示一个下拉导航栏。导航表包含菜单项,类别表包含子菜单。但它在获取第一个菜单项的所有菜单项和第一个子菜单项后,在循环的第一次迭代中停止,然后它只显示空结果 -- Table structure for table `category` -- CREATE TABLE `category` ( `cat_id` int(11) NOT NULL, `nav_id`

我希望有人能在php代码方面帮助我。我想做的是从两个表中使用嵌套的do while循环从数据库中获取菜单和子菜单,这样它就会显示一个下拉导航栏。导航表包含菜单项,类别表包含子菜单。但它在获取第一个菜单项的所有菜单项和第一个子菜单项后,在循环的第一次迭代中停止,然后它只显示空结果

-- Table structure for table `category`
--

CREATE TABLE `category` (
  `cat_id` int(11) NOT NULL,
  `nav_id` int(11) NOT NULL,
  `cat_eng` varchar(256) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `category`
--

INSERT INTO `category` (`cat_id`, `nav_id`, `cat_eng`) VALUES
(1, 1, 'Technology'),
(2, 1, 'Science'),
(3, 1, 'Mathemathics'),
(4, 1, 'Computer'),
(5, 2, 'Geography'),
(6, 2, 'Environment'),
(7, 2, 'Weather'),
(8, 2, 'World'),
(9, 3, 'Sport'),
(10, 3, 'Food'),
(11, 3, 'Health'),
(12, 4, 'Mens'),
(13, 4, 'Womens');

-- --------------------------------------------------------

--
-- Table structure for table `navigation`
--

CREATE TABLE `navigation` (
  `nav_id` int(11) NOT NULL,
  `nav_eng` varchar(256) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `navigation`
--

INSERT INTO `navigation` (`nav_id`, `nav_eng`) VALUES
(1, 'Educational'),
(2, 'General-Knowlege'),
(3, 'Life-style'),
(4, 'Fashion');
Connection.php

<?php 
  session_start();

  $host = 'localhost';
  $user = 'root';
  $password = '';
  $dbname = 'test';

  $connection = @mysqli_connect($host, $user, $password, $dbname);

  if (!$connection) {
    die("Connection failed: " . mysqli_connect_error());
  }

  mysqli_set_charset($connection, 'utf8');
 ?>

<div class="collapse navbar-collapse" id="navbarSupportedContent">
          <ul class="navbar-nav mr-auto">

            <!-- FETCHING NAVIGATION LIST ALONG WITH DROPDOWN MENU FROM DATABASE  -->
            <?php
                //SETTING GET PAGES
                if (isset($_GET['nav'])) {
                    $pageid = $_GET['nav'];
                }else {
                    $pageid = 1;
                }

                //NAVIGATION TABLE QUERY
                 $nav_sql = "SELECT * FROM navigation";
                 $nav_query = mysqli_query($connection, $nav_sql);
                 $nav_result = mysqli_fetch_assoc($nav_query);

                 //CATEGORY TABLE QUERY
                  $cat_sql = "SELECT category.*, navigation.nav_id AS id FROM category JOIN navigation ON (category.nav_id = navigation.nav_id) WHERE category.nav_id = " .$pageid;
                  $cat_query = mysqli_query($connection, $cat_sql);
                  $cat_result = mysqli_fetch_assoc($cat_query);
                do {
                  ?> 
                  <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" href="?nav=<?php echo $nav_result['nav_id'] ?>"> <?php echo $nav_result['nav_eng']; ?></a>
                    <ul class="dropdown-menu">
                      <?php 
                        do { ?>
                          <li>
                            <a class="dropdown-item" href="?nav=<?php echo $cat_result['cat_id']; ?>"><?php echo $cat_result['cat_eng'] ?></a>
                          </li> <?php 
                        } while ($cat_result = mysqli_fetch_assoc($cat_query));
                       ?>
                    </ul>
                  </li> <?php
                } while ($nav_result = mysqli_fetch_assoc($nav_query));

             ?>
          </ul>
         </div>


使用问题中的表定义和数据,我在让菜单系统在没有嵌套查询的情况下工作,以及使用
DOMDocument
确保正确写入和关闭元素以避免无效HTML方面做了一些尝试

您可以用自己的值替换数据库凭据来测试以下内容—它应该可以正常工作。我很感激这与最初的大不相同,而且还有其他方法来实现你的目标——这只是我的一个游戏,但它可能是有用的

<?php

    ob_start();
    ob_implicit_flush();


    /* modify as appropriate */
    $dbhost =   'localhost';
    $dbuser =   'root'; 
    $dbpwd  =   'xxx'; 
    $dbname =   'xxx';

    /* create the db connection */
    try{
        mysqli_report( MYSQLI_REPORT_STRICT );
        $db = new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );
        $db->set_charset( 'utf8' );
    }catch( Exception $e ){
        exit( $e->getMessage() );
    }


    /* a utility function that greatly speeds adding DOM elements with attributes and values */
    function create( $dom, $type, $attr=array(), $parent=NULL ){
        if( $dom && is_object( $dom ) && $type && !empty( $type ) && is_array( $attr ) ){
            $node = $dom->createElement( $type );
            foreach( $attr as $key => $value ){
                if( strtolower( $key )=='text' or strtolower( $key )=='nodevalue' or strtolower( $key )=='value' && $node->tagName!='input' ){
                    if( !is_object( $value ) )$node->nodeValue=$value;
                } else {
                    if( !is_null( $value ) && !is_object( $value ) ) $node->setAttribute( $key, $value );
                }
            }
            if( isset( $parent ) && is_object( $parent ) ) $parent->appendChild( $node );
            return $node;
        }
    }   

?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title>PHP & mySQL Database driven menu</title>
        <style>
            html *{font-family:verdana,arial;}
            a{text-decoration:none;}
            .active_cat{text-decoration:underline;font-style:italic}
            .active_subcat{color:red;font-style:italic}
        </style>
    </head>
    <body>
        <div class='collapse navbar-collapse' id='navbarSupportedContent'>
            <ul class='navbar-nav mr-auto'>
            <!-- this will be populated later -->
            </ul>
        </div>
    </body>
</html>
<?php

    /* capture & filter querystring variables */
    $nav_id=filter_input( INPUT_GET, 'nav', FILTER_SANITIZE_NUMBER_INT );
    $sub_nav_id=filter_input( INPUT_GET, 'subnav', FILTER_SANITIZE_NUMBER_INT );


    /* Create the basic menu of main headings based upon this simple query */
    $sql='select nav_id, nav_eng from navigation order by nav_eng asc';
    $results=$db->query( $sql );



    if( $results ){

        /* Create a new instance of DOMDocument and DOMXPath */
        libxml_use_internal_errors( true );
        $dom=new DOMDocument;
        $dom->loadHTML( ob_get_contents() );
        $xp=new DOMXPath( $dom );
        $col=$xp->query( '//div[ @id="navbarSupportedContent" ]/ul' );

        /* We have loaded the buffer - now clear the old version */
        ob_clean();

        /* We will write new DOM elements to this fragment */
        $fragment=$dom->createDocumentFragment();

        /* Maintain an array of menu items that become parent nodes for child menus */
        $nav=array();


        while( $rs=$results->fetch_object() ){

            /* create the list elemet for this category and add the link and a child `UL` element */
            $li=create( $dom, 'li', array( 'class'=>'nav-item dropdown' ), $fragment );

            /* set the attributes for the hyperlink */
            $options=array( 'class'=>'nav-link dropdown-toggle ', 'text'=>$rs->nav_eng, 'role'=>'button', 'data-toggle'=>'dropdown', 'aria-haspopup'=>'true', 'aria-expanded'=>'false', 'href'=>sprintf( '?nav=%d', $rs->nav_id ) );
            if( $rs->nav_id==$nav_id )$options['class'] .= 'active_cat';
            /* append the hyperlink to the li element */
            $a=create( $dom, 'a', $options, $li );

            /* create the child UL element and assign a reference in the $nav array for later use */
            $ul=create( $dom, 'ul', array( 'data-category'=>$rs->nav_eng, 'data-navid'=>$rs->nav_id, 'class'=>'dropdown-menu' ), $li );

            $nav[ $rs->nav_id ]=$ul;
        }
        /* Add the fragment to the main UL*/
        $col->item(0)->appendChild( $fragment );





        /* If there is a querystring variable `nav` then we query to find child menu items */
        if( $nav_id ){

            $sql='select
                    c.cat_eng, 
                    c.cat_id,
                    c.nav_id
                from category c
                    join navigation n on c.nav_id = n.nav_id
                where c.nav_id=?
                    order by c.cat_eng';
            /* create a prepared statement and bind the nav_id value as input */
            $stmt=$db->prepare( $sql );
            $stmt->bind_param('i', $nav_id );
            $res=$stmt->execute();

            if( $res ){
                /* The query succeeded, generate child menu items */
                $stmt->store_result();
                $stmt->bind_result( $cat, $cid, $nid ); # arbitrary names to correspond to fields in recordset

                /* Find the correct parent node from the array created earlier */
                $parent=$nav[ $nav_id ];


                /* iterate through the recordset & add menu items */
                while( $stmt->fetch() ) {
                    $options=array( 'class'=>'dropdown-item ', 'href'=>sprintf( '?nav=%d&subnav=%d', $nid, $cid ), 'text'=>$cat );
                    if( $cid==$sub_nav_id )$options['class'] .= 'active_subcat';

                    create( $dom, 'a', $options, create( $dom, 'li', array(), $parent ) );
                }

                $stmt->free_result();
                $stmt->close();
            }
        }

        /* render new DOM - the output buffer will now be flushed */
        exit( $dom->saveHTML() );
    }
?>

PHP&mySQL数据库驱动菜单
html*{字体系列:verdana,arial;}
a{文本装饰:无;}
.active_cat{文本装饰:下划线;字体样式:斜体}
.active_子类别{颜色:红色;字体样式:斜体}


由于在运行任何一个循环之前执行了
$cat_查询
,因此第一次循环内部循环时将读取数据库中的所有记录。第二次,没有可读取的记录。您需要在外部循环内执行此查询—拾取特定项,或者将SQL光标重新定位在开始处并重新读取。嘿,奈杰尔!因为我对编码还不熟悉,所以我被卡住了。你能给我提供正确的查询吗?!不确定应该是什么,您有
category.nav\u id=“$pageid
在SQL的末尾,但是,
$pageid
与您试图显示的每个菜单有何关联。如果这是从
navigation
表中获取的,那么在第一个循环中,获取特定的值并使用每个菜单的正确值执行
category
查询。我设置
category.nav-is=。$pageid
是因为每个菜单项在navigation表中都有它的nav_id,而category表的子菜单也有nav_id,因此,当单击每个菜单时,我希望它根据通过
,$\u GET[]
方法传递的导航id动态获取子菜单。所以我设置了这一行->``//SETTING GET PAGES if(isset($\u GET['nav']){$pageid=$\u GET['nav'];}否则{$pageid=1;}```上面我可以澄清一下:首先,您希望将
nav_eng
表中的
nav_eng
内容显示为菜单项。单击时,相关子菜单将被提取并显示在该类别标题下。这或多或少正确吗?没问题-你试过了吗?这是预期的结果吗?您仍然应该启用mysqli异常模式:并停止捕获异常。