Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.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-pdo中的category-subcategory循环_Php_Html_Arrays - Fatal编程技术网

php-pdo中的category-subcategory循环

php-pdo中的category-subcategory循环,php,html,arrays,Php,Html,Arrays,我有两个表,结构和数据如下: 表1 CREATE TABLE IF NOT EXISTS `category` ( `category_id` int(5) NOT NULL AUTO_INCREMENT, `category_name` varchar(50) COLLATE latin1_general_cs NOT NULL, `status` varchar(8) COLLATE latin1_general_cs NOT NULL DEFAU

我有两个表,结构和数据如下:

表1

    CREATE TABLE IF NOT EXISTS `category` (
      `category_id` int(5) NOT NULL AUTO_INCREMENT,
      `category_name` varchar(50) COLLATE latin1_general_cs NOT NULL,
      `status` varchar(8) COLLATE latin1_general_cs NOT NULL DEFAULT 'Disabled',
      PRIMARY KEY (`category_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs AUTO_INCREMENT=5 ;
关于数据:

    INSERT INTO `category` (`category_id`, `category_name`, `status`) VALUES
    (1, 'Electronics', 'Enabled'),
    (2, 'Computers', 'Enabled'),
    (3, 'Cell Phones', 'Enabled'),
    (4, 'Video Games', 'Enabled');
    INSERT INTO `subcategory` (`subcategory_id`, `subcategory_name`, `category_id`, `status`) VALUES
    (1, 'LED', 1, 'Enabled'),
    (2, 'DVD Players', 1, 'Enabled'),
    (3, 'Tablet', 2, 'Enabled');
表2

    CREATE TABLE IF NOT EXISTS `subcategory` (
      `subcategory_id` int(5) NOT NULL AUTO_INCREMENT,
      `subcategory_name` varchar(25) COLLATE latin1_general_cs NOT NULL,
      `category_id` int(5) NOT NULL,
      `status` varchar(8) COLLATE latin1_general_cs NOT NULL DEFAULT 'Disabled',
      PRIMARY KEY (`subcategory_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs AUTO_INCREMENT=4 ;
关于数据:

    INSERT INTO `category` (`category_id`, `category_name`, `status`) VALUES
    (1, 'Electronics', 'Enabled'),
    (2, 'Computers', 'Enabled'),
    (3, 'Cell Phones', 'Enabled'),
    (4, 'Video Games', 'Enabled');
    INSERT INTO `subcategory` (`subcategory_id`, `subcategory_name`, `category_id`, `status`) VALUES
    (1, 'LED', 1, 'Enabled'),
    (2, 'DVD Players', 1, 'Enabled'),
    (3, 'Tablet', 2, 'Enabled');
我有以下php pdo代码:

    <?php
    include_once 'connstring.inc.php';
    $q = $conn->prepare("SELECT DISTINCT *, C.category_id
    FROM category C LEFT OUTER JOIN subcategory S
    ON C.category_id = S.category_id AND S.status = 'Enabled' 
    WHERE C.status = 'Enabled'
    ");
    $q->execute();   
    //$result = $q->fetchAll(PDO::FETCH_ASSOC|PDO::FETCH_GROUP);

    $currentCategory = null;
    while($row = $q->fetch(PDO::FETCH_ASSOC)){
        $categoryID = $row[category_id];
      $categoryName = $row[category_name];
      if ($categoryName != $currentCategory) {
        echo "<h2>".$categoryID.' '.$categoryName."</h2>";
        $currentCategory = $categoryName;
      }
      echo "<p>".$row['subcategory_id'].' '.$row['subcategory_name']."</p>"; 
    }
    ?>
电子和计算机类别有子类别

我需要生成html标记和css选择器,如下所示:

    <li class='submenu border_t'>
    <div class=cat_bg>
    <a title='Electronics' href='#'><span>Electronics</SPAN></a>
    </div>

    <ul class='level2'>
    <li><a href="#">LED</a></li>
    <li><a href="#">DVD Players</a></li>
    </ul>
    </li>      

    <li class='submenu border_t'>
    <div class=cat_bg>
    <a title='Computers' href='#'><span>Computers</SPAN></a>
    </div>

    <ul class='level2'>
    <li><a href="#">Tablet</a></li>
    </ul>
    </li>  

    <li class='submenu border_t'>
    <div class=cat_bg>
    <a title='Computers' href='#'><span>Cell Phones</SPAN></a>
    </div>

    <li class='submenu border_t'>
    <div class=cat_bg>
    <a title='Computers' href='#'><span>Video Games</SPAN></a>
    </div>

    </ul>
    </div>
    </div>  
  • 这是我写的代码

    <?php
                include_once 'connstring.inc.php';
                //Get records from database
                $q = $conn->prepare("SELECT DISTINCT *, C.category_id
                FROM category C LEFT OUTER JOIN subcategory S
                ON C.category_id = S.category_id AND S.status = 'Enabled' 
                WHERE C.status = 'Enabled'
                ");
                $q->execute();   
                $currentCategory = null;
                ?>
                <div class='categoriesmenuFather ' id='nav-cat' style='display:block' >
                <div class='categories_list categoriesmenu '>
                <ul class='level1'>
    
                <?php
                while($row = $q->fetch(PDO::FETCH_ASSOC))
                {
                $categoryID = $row[category_id];
                $categoryName = $row[category_name];
                if ($categoryName != $currentCategory) 
                {
                ?>
                <li class='submenu border_t'>
                <div class=cat_bg>
                <a title='<?php echo $categoryName; ?>' href='products.php?catid=<?php echo $categoryID; ?>'><span><?php echo $categoryName; ?></SPAN></a>
                </div>
                <ul class='level2'>
                <?php
                $currentCategory = $categoryName;
                  }
                ?>
                <!--BOF Sub-Category-->
    
                <li><a href="products.php?catid=<?php echo $categoryID.'&subcatid='.$row['subcategory_id']?>"><?php echo $row['subcategory_name']; ?></a></li>
                </ul>
                </li>  
                <!--EOF Sub-Category-->
                <?php
                }
                ?>
    
                </ul>
                </div>
                </div>  
    
    在后面添加

    然后添加
    echo“
      如果子类别具有

      然后在后面添加
    • 添加
      echo“
    在子类别列表的末尾(如果是)

    然后
    echo“”在类别末尾


    或者简单地在其他文件中使用一些模板,为变量使用一些特殊字符串。

    这有点快速和肮脏。但我认为这是你的基本想法。在循环过程中,在开始下一个类别之前关闭ul和li。然后,在完成循环后,始终可以手动关闭最后一组

    while ($row = $q->fetch(PDO::FETCH_ASSOC)){
        $categoryID = $row['category_id'];
        $categoryName = $row['category_name'];
        if ($categoryName != $currentCategory) {
            $currentCategory = $categoryName;
    
            //-- Make sure there is html first.
            if ($html) {
                //-- Close the UL from previous category
                if ($previous == true) {
                    $html .= '</ul>';
                }
                //-- Close the LI from previous category
                $html .= '</li>';
            }
            //-- Start new category
            $html .= '<li class="submenu border_t">';
            $html .= '  <div class=cat_bg>';
            $html .= '      <a title="'. $categoryName .'" href="#"><span>'. $categoryName .'</span></a>';
            $html .= '  </div>';
            if ($row['subcategory_name']) {
                $html .= '  <ul class="level2">';
                $previous = true;
            }
            else
                $previous = false;
        }
    
        //-- Add sub category
        if ($row['subcategory_name']) {
            $html .= ' <li><a href="#">'. $row['subcategory_name'] .'</a></li>';
        }
    }
    //-- Close the last category UL and LI
    if ($html) {
        $html .= '  </ul>';
        $html .= '</li>';
    }
    echo $html;
    
    while($row=$q->fetch(PDO::fetch\u ASSOC)){
    $categoryID=$row['category_id'];
    $categoryName=$row['category_name'];
    如果($categoryName!=$currentCategory){
    $currentCategory=$categoryName;
    //--首先确保有html。
    如果($html){
    //--从上一类别关闭UL
    如果($previous==true){
    $html.='';
    }
    //--关闭上一类别中的LI
    $html.='';
    }
    //--开始新类别
    $html.='
  • ; $html.=''; $html.=''; $html.=''; if($row['subcategory_name'])){ $html.='
      ; $previous=true; } 其他的 $previous=假; } //--添加子类别 if($row['subcategory_name'])){ $html.='
    • '; } } //--关闭最后一个类别UL和LI 如果($html){ $html.='
    '; $html.='
  • '; } echo$html;
    另一方面,您可能希望对sql查询的结果进行排序,以便将类别放在一起。否则,您可能会拆分同一类别


    编辑:我最后一次更改了代码。我必须设置一个标志$previous,以确定是否从循环的最后一次迭代关闭UL。

    数据显示格式不正确。我的代码中是否添加了任何内容以使循环正常工作?对不起,我没有理解你的意思。我已经提供了我在上面写的代码,与您注意到的代码相同。问题在于循环中的一些标签。在上面的代码中,只有一个子类别显示在“电子产品”类别中。其余的都不见了。似乎循环运行不正常。只有一个子类别显示在这个图片链接中:我用我的代码运行了一个测试,它正确循环。我想说,您的问题可能与sql语句返回的数据有关。可以使用print\r($row)打印数组吗;在while循环中?请确保数据在那里。对不起,让我再检查一件事。好的,我确实错过了添加的第一个子类别。由于第一个子类别与该类别位于同一行数据中,因此必须在其中添加子类别。我编辑了我的代码,这应该给你你想要的。很抱歉造成混淆。@谢谢你……差不多到了,除了那些没有子类别的类别,如手机、视频游戏显示为空白