简体   繁体   English

MySQL查询结果

[英]MySQL query results

I'm working on coding a design for a project and have spent the past few days trying to get this to work but cannot wrap my head around how to do it. 我正在为项目设计编码,过去几天一直在努力使它起作用,但无法全神贯注于如何做到这一点。

I'm trying to get the first 4 results from a database, and print them to the header. 我正在尝试从数据库中获取前4个结果,并将它们打印到标题中。 For each result, the nav list number needs to increase by one. 对于每个结果,导航列表号需要增加一个。 For example: 例如:

<li class="nav_list" id="nav_list1">RESULT 1
<li class="nav_list" id="nav_list2">RESULT 2

Heres what I have so far, but have no idea how I could get it to display correctly. 这是我到目前为止的内容,但不知道如何使它正确显示。 Any help is appreciated! 任何帮助表示赞赏!

$query = "SELECT id, title, description, groupid FROM category LIMIT 0, 4";
            $result = mysqli_query($dbc, $query)
            or die('Error querying database');
            while ($row = $result->fetch_row()){
                $catID = $row[0];
                $catTITLE = $row[1];

            }
            ?> 
            <ul id="nav_line1">
            <?php
            for ($i = 1; $i <= 4; $i++) {
                print('<li class="nav_list" id="nav_list'.$i.'"><a href="catIDhere">catTITLEhere</a>|</li>');
            }
$i = 1;
while ($row = $result->fetch_row()) {
   echo <<<EOL
<li id="nav_list{$i}"><a href="{$row[0]}"> etc...

EOL;
    $i++;
}

Can you try this, 你可以试试这个吗

    $query = "SELECT id, title, description, groupid FROM category ORDER BY id DESC LIMIT 0, 4";
    $result = mysqli_query($dbc, $query)
    or die('Error querying database');

      echo '<ul id="nav_line1">';
      while ($row = $result->fetch_row()){
        $catID = $row[0];
        $catTITLE = $row[1];           
            print('<li class="nav_list" id="nav_list'.$catID.'"><a href="'.$catID.'">'.$catTITLE.'</a>|</li>');
      }

     echo "</ul>";

Try somethin like this: 试试这样的东西:

query = "SELECT id, title, description, groupid FROM category LIMIT 0, 4";
        $result = mysqli_query($dbc, $query)
        or die('Error querying database');
        $i = 1
        while ($row = $result->fetch_row()){
            $catID = $row[0];
            $catTITLE = $row[1];
            ?><ul id="nav_line1"><?php
            print('<li class="nav_list" id="nav_list'.$i.'"><a href="'.$catID.'">'.$catTITLE.'</a>|</li>');
            $i++
    }

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

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