我想从数据库和子类别中获取类别,我想在类别列表的位置显示这些子类别

时间:2015-12-19 11:46:43

标签: javascript php mysql

我想从数据库和子类别中获取类别,我想在类别列表的位置显示此子类别。 bur我有问题。

代码:

            <h2 id="cat_price" style="color:#f5c658">Price Category</h2>
             <ul class="ul1" id="all_rate">
            <?php
            $sql="select * from category";
            $execute=mysql_query($sql);
            while($row=mysql_fetch_assoc($execute)){ ?> 
            <li><a href="index.php?did=<?php echo $row['id']; ?>" id="wax"><?php echo $row['category'] ; ?></a></li>
            <?php } ?>
            </ul>

            <ul class="ul1" id="Waxing">
            <?php 
            if(isset($_GET['did']))
            {
                $id=$_GET['did'];
                $sql="select * from midcat where cid=$id";
                $execute=mysql_query($sql);
                while($row=mysql_fetch_assoc($execute)){ ?>
                <li><a href="#" id="wax_normal" ><?php echo $row['midcat']; ?> </a></li>
                <?php }
            }  ?>
                <li><a href="#" class="back">Back</a></li>
            </ul>

1 个答案:

答案 0 :(得分:0)

<?php 

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testphp"; 

// Create connection 
//table having 3 fileds category id, parent cat id, and cat name.
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM category";
$result = $conn->query($sql);
?>
<h2 id="cat_price" style="color:#f5c658">Price Category</h2>
<ul class="ul1" id="all_rate">
    <?php
        if ($result->num_rows > 0) {
        // output data of each row
            while($row = $result->fetch_assoc()) { ?>
            <li><a href="index.php?did=<?php echo $row['id']; ?>" id="wax"><?php echo $row["categoryname"] ; ?></a></li>
    <?php } ?>
</ul>
<?php
    } else {
        echo "<li>0 results</li>";
    }
?>
<?php
    if(isset($_GET['did'])){
        $id=$_GET['did'];
        $sql="select * from category where parentcatid=$id";
        $result = $conn->query($sql);
        if ($result->num_rows > 0) { 
            echo '<ul class="ul1" id="Waxing">';

            while($row = $result->fetch_assoc()){ ?>
                <li><a href="#" id="wax_normal" ><?php echo $row['categoryname']; ?> </a></li>
            <?php }
            echo "</ul>";
        }
        else {
            echo "<li>0 results</li>";
        }
    }  
?>

你可以试试上面的代码。 根据您的要求更改表名称和字段名称。