简体   繁体   English

从数据库php mysql查询显示类别

[英]Query for display catergories from database php mysql

I'm new to coding with php and using MySQL. 我是第一次使用php和MySQL进行编码。 I am having trouble to display a list of categories by their ID so that each category is displayed individually as a heading. 我无法按ID显示类别列表,因此每个类别都作为标题单独显示。 Instead I got it to display a category name but its only echoing out a category name twice that's the same. 相反,我让它显示了一个类别名称,但是它只回显了两次相同的类别名称。 Here is my code... 这是我的代码...

$sql= "SELECT * FROM categories ";
$query = mysql_query($sql); 


while($row = mysql_fetch_array($query))
 {
     $id=$row['id'];
     $cat_name=$row['cat_name'];

 }
 ?>


<ul class="nav nav-list">

 <li class="nav-header"><?php echo $cat_name;?></li>
 <li class="nav-header"><?php echo $cat_name;?></li>
</ul>

You can use this code as-is : 您可以按原样使用此代码:

$sql= "SELECT * FROM categories ";
$query = mysql_query($sql); ?>

<ul class="nav nav-list">

<?php while($row = mysql_fetch_array($query)) : ?>
    <li class="nav-header"><?php echo $row['cat_name'];?></li>
<?php endwhile; ?>

</ul>

This will loop through your records and for each record, it will print an entire li with the required data. 这将遍历您的记录,并且对于每条记录,将打印出带有所需数据的整个li。

Note that separating your PHP code from your HTML code like this has several benefits. 请注意,像这样将您的PHP代码与HTML代码分开具有许多好处。 It will be better colored in your editor and it is also easier to integrate. 在您的编辑器中它将更好地着色,并且也更易于集成。

Your li tag is outside the while loop. 您的li标签在while循环之外。 So the $id and $cat_name is only the last record in the DB, then you echo them twice. 因此,$ id和$ cat_name只是数据库中的最后一条记录,然后将它们回显两次。 That's way you got the same name twice. 这样一来,您两次获得相同的名字。 Try echo the li tag in the loop (but not the ul): 尝试在循环中回显li标签(但不要在ul中):

<ul class="nav nav-list">
<?php 
while($row = mysql_fetch_array($query))
 {
     $id=$row['id'];
     $cat_name=$row['cat_name'];

     echo '<li class="nav-header">' .$cat_name. '</li>';
 }
 ?>

</ul>

The reason you are printing the same value out twice is because $cat_name is a string variable and will only hold one value at a time you may want to save the items an array and loop at a seperate time, like such 之所以要打印两次相同的值,是因为$cat_name是一个字符串变量,一次只能保存一个值,您可能想将项目保存为数组并在另一个时间循环,例如

<?php
$sql= "SELECT * FROM categories ";
$query = mysql_query($sql); 
$category = array();
while($row = mysql_fetch_array($query))
   {
     $array = array(
        id => $row['id'],
        name => $row['cat_name']
     );
     array_push($category,$array);
   }
 ?>
<ul class="nav nav-list">
<?php 
foreach($category as $c)
   {
     echo '<li class="nav-header">'.$c['name'].'</li>';
   }; 
?>
</ul>
    $sql= "SELECT * FROM categories ";
    $query = mysql_query($sql); 


while($row = mysql_fetch_assoc($query))
 {
 ?>

       <ul class="nav nav-list">

                <li class="nav-header"><?php echo $row['id'];  ?></li>
                <li class="nav-header"><?php echo $row['cat_name']; ?></li>
      </ul>
<?php } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM