Не выводятся дочерние категории

Discussion in 'PHP' started by antras2007, 24 Nov 2014.

  1. antras2007

    antras2007 New Member

    Joined:
    4 Jul 2013
    Messages:
    33
    Likes Received:
    0
    Reputations:
    0
    Доброго всем времени суток. Подскажите кто знает. Есть php дерево выводящее на экран из базы список меню
    PHP:
    <?php
    include_once "connectdb.php";
    class 
    menu
    {
        private 
    $mysqli;

        function 
    left_menu($parent_category$lvl)
        {


            
    $this->mysqli connectdb::getInstance();
            
    $this->mysqli->query("set names 'utf8'") or die(mysqli_error($this->mysqli));
            
    $query "select * from categories where parent_category = " $parent_category " order by category";
            
    $result $this->mysqli->query($query) or die(mysqli_error($this->mysqli));
            if (
    mysqli_num_rows($result) > 0) {
                echo 
    "<ul style='list-style-type: none;' \n>";
                while (
    $row mysqli_fetch_array($result)) {
                    
    $id $row["id"];
                    echo 
    "<li\n>";
                    echo(
    "<a href='" "?id=" $id "'>" $row["category"] . "</a>" "  \n");
                    echo 
    "</li\n>";
                    
    $this->left_menu($id$lvl);
                    
    $lvl--;
                }

                echo 
    "</ul\n>";
            }
        }
    }

    $menu = new menu();
    $menu->left_menu(00);
    При нажатии на дочернюю категорию все выводится на экран без проблем, а вот при нажатии на родительскую категорию экран пустой

    вот файл выводящий товары на экран

    content.php
    PHP:

    <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="../bootstrap/css/bootstrap-responsive.css" rel="stylesheet">
    <script src="../js/jquery.js"></script>
    <script src="../bootstrap/js/bootstrap.min.js"></script>
    <?php
    include_once "connectdb.php";
    header("content-type: text/html;charset=utf-8");
    class 
    content{
        private 
    $mysqli;
        private 
    $id_parent;

        function 
    maincontent(){
            
    $this->mysqli connectdb::getInstance();

            if (isset(
    $_GET['id'])) {
                
    $this->id_parent htmlspecialchars($_GET['id']);

            }else{
                
    $query "select count(*) from goods";
                
    $result $this->mysqli->query($query);
                
    $row mysqli_fetch_row($result);
                
    $total_rows $row[0];
                
    $per_page 10;
                
    $num_pages ceil($total_rows/$per_page);
                if (isset(
    $_GET['page'])) {
                    
    $page = ($_GET['page'] - 1);
                }else {
                    
    $page 0;
                }
                
    $start abs($page $per_page);

                
    $query "select * from goods ORDER by id DESC limit $start,$per_page ";
                
    $result $this->mysqli->query($query);
                
    $all_goods = array();
                
    $i 0;
                while (
    $row mysqli_fetch_assoc($result)) {
                        
    $all_goods[$i]['id'] = $row['id'];
                        
    $all_goods[$i]['name'] = $row['name'];
                        
    $all_goods[$i]['filename'] = $row['filename'];
                        
    $all_goods[$i]['info'] = $row['info'];
                        
    $all_goods[$i]['price'] = $row['price'];
                        
    $all_goods[$i]['parent_category'] = $row['parent_category'];
                        
    $i++;
                }

                    foreach (
    $all_goods as $goods) {
                        
    $id_goods $goods['id'];
                        
    $id $goods['parent_category'];
                        echo 
    "<div class='container' style='width: 150px;
                                                            height: 220px;
                                                            display: inline-block;
                                                            margin: 10px;
                                                            margin-top: 0;
                                                            padding: 10px;
                                                            padding-top: 0px;
                                                            vertical-align: top;'>"
    ;
                        echo 
    "<div class='span12' style='text-align: center;list-style-type: none;>";
                        echo 
    "<ul'>";
                        echo 
    "<li><a href='" "?id=" $id "&id_good=" $id_goods "'><img src=../images/" $goods['filename'] . "></a></li>";
                        echo 
    "<li>" ucwords($goods['name']) . "</li>";
                        echo 
    "<li><p class='text-info'><strong>" $goods['price'] . "$</strong></p></li>";
                        echo 
    "</ul>";
                        echo 
    "</div>";
                        echo 
    "</div>";
                }

                echo 
    "<p align='center'>";

                for(
    $i=1;$i<=$num_pages;$i++) {
                    if (
    $i-== $page) {
                        echo 
    $i." ";
                    } else {
                        echo 
    '<a href="'.$_SERVER['PHP_SELF'].'?page='.$i.'">'.$i."</a> ";
                    }
                }
                echo 
    "</p>";
            }

            if(isset(
    $this->id_parent)){
                
    $query "select * from goods where parent_category = ".$this->id_parent."";
                
    $result $this->mysqli->query($query);
                
    $goods = array();
                
    $i 0;
                while(
    $row mysqli_fetch_assoc($result)){
                        
    $goods[$i]['id'] = $row['id'];
                        
    $goods[$i]['name'] = $row['name'];
                        
    $goods[$i]['filename'] = $row['filename'];
                        
    $goods[$i]['info'] = $row['info'];
                        
    $goods[$i]['price'] = $row['price'];
                        
    $i++;
                }

                foreach(
    $goods as $good){
                    
    $id_good $good['id'];

                    echo 
    "<div class='container' style='width: 150px;
                                                            height: 220px;
                                                            display: inline-block;
                                                            margin: 10px;
                                                            margin-top: 0;
                                                            padding: 10px;
                                                            padding-top: 0px;
                                                            vertical-align: top;'>"
    ;
                    echo 
    "<div class='span12'>";
                    echo 
    "<ul style='text-align: center;list-style-type: none;'>";
                    echo 
    "<li><a href='" "?id=" $this->id_parent "&id_good=".$id_good."'><img src=../images/".$good['filename']."></a></li>";
                    echo 
    "<li>" ucwords($good['name']) . "</li>";
                    echo 
    "<li><p class='text-info'><strong>" $good['price'] . "$</strong></p></li>";
                    echo 
    "</ul>";
                    echo 
    "</div>";
                    echo 
    "</div>";
                }

            }
        }

    }
    $content = new content();
    $content->maincontent();


    вот index.php начальный файл

    PHP:
    <?php
    session_start
    ();
    if(!isset(
    $_SESSION['cart'])){
        
    $_SESSION['cart'] = array();
    }
    if(!isset(
    $_SESSION['admin'])){
        
    $_SESSION['admin'] = false;
    }
    ?>

    <!DOCTYPE html>
    <html>
    <head lang="ru">
        <meta charset="UTF-8">
        <meta name="keywords" content="Интернет магазин">
        <meta name="description" content="Тестовый интернет магазин">
        <meta name="robots" content="index, follow"/>
    <!--    <meta name="viewport" content="width=device-width, initial-scale=1.0">-->
        <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
        <link href="bootstrap/css/bootstrap-responsive.css" rel="stylesheet">
        <link href="css/style_index.css" rel="stylesheet">
        <title>Интернет магазин</title>
    </head>
    <body>
    <script src="js/jquery.js"></script>
    <script src="bootstrap/js/bootstrap.min.js"></script>
    <div class="container">
        <div class="span4 offset4" id="head">
            <h1>Интернет магазин</h1>
        </div>
    </div>
    <?php
    if(isset($_SESSION['admin']) and $_SESSION['admin'] != true) {
        
    ?>
        <div id="form">
            <form action="classes/admin.php" method="POST">
                <input type="text" class="span2" placeholder="login" name="login" id="login"><br>
                <input type="password" class="span2" placeholder="password" name="password" id="password"><br>
                <button type="submit" class="btn btn-primary" name="submit">Войти</button>
            </form>
        </div>
    <?php
    }
    ?>
    <div class="navbar"  id="basket">
        <nav class="navbar-inner">
            <a href='index.php?cart=basket' style="float: right; margin-top: 9px;">Ваша корзина:(<?php
                
    if(isset($_GET['add_to_basket'])){
                    include 
    $_SERVER['DOCUMENT_ROOT'] . '/classes/add2basket.php';
                }
                if(isset(
    $_SESSION['total_items'])){
                    echo 
    $_SESSION['total_items'];
                }else{
                    echo 
    "0";
                }
    ?>) - <?php
                
    if(isset($_SESSION['total_price'])) {
                    echo 
    $_SESSION['total_price'];
                }else{
                    echo 
    "0";
                }
                
    ?>$
            </a>
        </nav>
    </div>
    <?php
    if(isset($_SESSION['admin']) and $_SESSION['admin'] == true) {
        
    ?>
        <div class="navbar navbar-inverse navbar-fixed-top">
            <nav class="navbar-inner">
                <ul class="nav">
                    <li><a href="index.php"><i class="icon-home"></i> home</a></li>
                    <li class="divider-vertical"></li>
                    <li><a href="admin/categories.php">categories</a></li>
                    <li><a href="admin/goods.php">goods</a></li>
                    <li><a href="admin/get_orders.php">orders</a></li>
                </ul>

                <form action="classes/admin.php" method="POST">
                    <span style="float: right;color: #ffffff;">
                        <ul class="nav">
                            <li style="margin-top: 10px;"><?php
                                $time 
    date('H');
                                if(
    $time >= and $time 12){
                                    echo 
    "Доброе утро";
                                }elseif(
    $time >= 12 and $time 18){
                                    echo 
    "Добрый день";
                                }elseif(
    $time >= 18 and $time 24){
                                    echo 
    "Добрый вечер";
                                }else{
                                    echo 
    "Доброй ночи";
                                }
                                
    ?>, админ!</li>
                            <li><button type="submit" class="btn btn-link" name="exit" >Выйти</button></li>
                        </ul>
                    </span>
                </form>
                <div id="search">
                    <ul>
                        <li>
                            <form action="index.php?search" method="POST">
                                <input type="text" name="search" class="span2 search-query" placeholder="Введите запрос">
                            </form>
                        </li>
                    </ul>
                <div>
            </nav>
        </div>
    <?php
    }else {
        
    ?>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <nav class="navbar-inner">
            <ul class="nav">
                <li><a href="index.php"><i class="icon-home"></i> home</a></li>
            </ul>
            <div id="search_non_register">
                <ul>
                    <li>
                        <form action="index.php?search" method="POST">
                            <input type="text" name="search" class="span2 search-query" placeholder="Введите запрос">
                        </form>
                    </li>
                </ul>
            <div>
        </nav>
    </div>
    <?php
    }
    ?>
    <div class="container-fluid">
        <div class="row-fluid navbar">
            <div class="navbar-inner span2" style="margin-left: -10px">
                <?php
                
    include "/classes/menu.php";
                
    ?>
            </div>
            <div class="span10">
                <?php
                
    if (isset($_GET['cart'])) {
                    include 
    $_SERVER['DOCUMENT_ROOT'] . '/classes/basket.php';
                    exit();
                }

                if (isset(
    $_GET['update'])) {
                    include 
    $_SERVER['DOCUMENT_ROOT'] . '/classes/update_basket.php';
                }

                if (isset(
    $_GET['order'])) {
                    include 
    $_SERVER['DOCUMENT_ROOT'] . '/classes/order.php';
                    exit();
                }

                if (isset(
    $_GET['thenks'])) {
                    echo 
    "<p class='lead'>Ваш заказ успешно принят! Спасибо за покупку!</p>";
                    exit();
                }

                if (isset(
    $_GET['id_good'])) {
                    include 
    $_SERVER['DOCUMENT_ROOT'] . '/classes/fool_info_goods.php';
                    exit();
                }

                if(isset(
    $_GET['search'])){
                    include 
    $_SERVER['DOCUMENT_ROOT'] . '/classes/search.php';
                }

                else {
                    include 
    $_SERVER['DOCUMENT_ROOT'] . '/classes/content.php';
                }

                
    ?>
            </div>
        </div>
    </div>
    </body>
    <footer>
        <p align="center" style="color: #ffffff">Суперпупер веб мастеринг <?php echo date("Y");?>&copy;</p>
    </footer>
    </html>

    я так понимаю что нужно просто изменить вот этот запрос в файле content.php
    PHP:
    $query "select * from goods where parent_category = ".$this->id_parent;
    на
    PHP:
     $query "select * from goods where parent_category = ".$this->id_parent." or category =".$this->id_parent;
    но он выдает ошибку

    Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in D:\cod\test\eshop\classes\content.php on line 84

    подскажите кто знает как сделать правильно , а то что то не выходит у меня. СПС !!!!
     
    #1 antras2007, 24 Nov 2014
    Last edited: 24 Nov 2014