0

我正在制作一个侧边栏提要,它将显示提交到我的数据库中的 10 个最新内容。我对这一切都很陌生,所以我想知道..这是一种可行的方法吗?它可以工作..不是自动的,但是当我将某些内容提交到我的数据库中时,它会在那里..我提交其他内容,然后顶部转到第二个..!我只是无法摆脱这种感觉,也许这不是一个好方法。

前 3 个部分

<div class="span3 offset3">

<?php include 'feed/one.php'; ?>

    <ul class="nav nav-list well">


        <li class="nav-header"></li>
        <li class="active"><a href="#">HIT INFO</a></li>
        <?php while($row = $data->fetch_assoc()) { ?>

        <li><a href="<?php print $row['link']?>"><?php 
             Print "<tr>"; 
             Print "<th>Hit:</th> <td>".$row['hit'] . "</td> "; 
             Print "<th>Amount:</th> <td>".$row['amount'] . " </td>"; 
             Print "<th>Category:</th> <td>".$row['category'] . "</td></tr> ";
             Print "<br><br/>"; 
             Print "</table>";?></a></li>
        <?php } ?>

        <li class="divider"></li>
        <?php $data = $mysqli->query("SELECT * FROM hit ORDER BY hit_id DESC LIMIT 1, 1"); ?>
        <?php while($row = $data->fetch_assoc()) { ?>

        <li><a href="<?php print $row['link']?>"><?php 
             Print "<tr>"; 
             Print "<th>Hit:</th> <td>".$row['hit'] . "</td> "; 
             Print "<th>Amount:</th> <td>".$row['amount'] . " </td>"; 
             Print "<th>Category:</th> <td>".$row['category'] . "</td></tr> ";
             Print "<br><br/>"; 
             Print "</table>";?></a></li>
        <?php } ?>
        <li class="divider"></li>
        <?php $data = $mysqli->query("SELECT * FROM hit ORDER BY hit_id DESC LIMIT 2, 1"); ?>
        <?php while($row = $data->fetch_assoc()) { ?>

        <li><a href="<?php print $row['link']?>"><?php 
             Print "<tr>"; 
             Print "<th>Hit:</th> <td>".$row['hit'] . "</td> "; 
             Print "<th>Amount:</th> <td>".$row['amount'] . " </td>"; 
             Print "<th>Category:</th> <td>".$row['category'] . "</td></tr> ";
             Print "<br><br/>"; 
             Print "</table>";?></a></li>
        <?php } ?>
        <li class="divider"></li>
        <?php $data = $mysqli->query("SELECT * FROM hit ORDER BY hit_id DESC LIMIT 3, 1"); ?>
        <?php while($row = $data->fetch_assoc()) { ?>

        <li><a href="<?php print $row['link']?>"><?php 
             Print "<tr>"; 
             Print "<th>Hit:</th> <td>".$row['hit'] . "</td> "; 
             Print "<th>Amount:</th> <td>".$row['amount'] . " </td>"; 
             Print "<th>Category:</th> <td>".$row['category'] . "</td></tr> ";
             Print "<br><br/>"; 
             Print "</table>"; ?></a></li>
             <?php } ?>

提交的数据会显示在我的主页上,直到提交其他内容,然后转到“提要”,然后转到显示过去数据的另一个页面。

4

3 回答 3

1

而不是每次查询数据库并获取一条记录来显示它,您应该在单个查询中执行它,该查询获取您需要的行数:

<?php $data = $mysqli->query("SELECT * FROM hit ORDER BY hit_id DESC LIMIT 1, N"); ?> 

这会获取前 N 行,其中 N 当然必须是数字。然后你应该循环抛出行并按照你现在的方式打印它。唯一的区别是您可能需要一些时间才能正确地了解打开/循环/关闭表格的细微差别,但这是您花费时间来围绕它进行的一项非常好的投资!

于 2013-05-04T02:05:15.537 回答
1

您似乎没有任何用户可以拦截的内容,因此“安全”不是问题。

你在不合逻辑地查询。当您可以最初获取数据并使用该对象在需要的地方中继内容时,没有理由在整个页面上重复具有多个偏移量的相同查询。在文件的开头运行这样的查询,或者最好在呈现任何输出之前运行。

"SELECT * FROM hit ORDER BY hit_id DESC LIMIT 4"

现在您有 4 个项目,因此在整个页面中访问这些项目可能是这样的:

<ul class="nav nav-list well">
    <li class="nav-header"></li>
    <li class="active"><a href="#">HIT INFO</a></li>
        <?php while($row = $data->fetch_assoc()): ?>

            <li>
                <a href="<?php print $row['link']?>">
                <table>
                    <tr>
                        <th>Hit:</th> <td><?php echo $row['hit']; ?></td>
                        <th>Amount:</th> <td><?php echo $row['amount']; ?></td>
                        <th>Category:</th> <td><?php echo $row['category']; ?></td>
                    </tr>
                    <br><br/> 
                </table>
              </a>
          </li>
          <li class="divider"></li>
        <?php endwhile; ?>
 </ul>

现在,您有一些标记错误。你关闭了一张你从未打开过的桌子。这只能在很小的程度上被认为是表格数据,因此表格可能无论如何都不是最好的方法。我自己不理解您以这种方式使用 th 和 td ,但是如果它对您有用,那就太好了。

你应该跳出 php 来显示 html。呼应类似的东西"<th>"是完全没有必要的。

于 2013-05-04T02:06:06.203 回答
0

您实际上是在使用该hit_id字段来对从数据库中获取的数据进行排序。

如果您想真正检索“提交到我的数据库中的 10 个最新内容”,则需要除自动递增 ID 之外的其他内容。(类似于COUNT行中的 a amount)。这将取决于填充此字段的实际值/方式,但它可能看起来像这样:

SELECT * FROM hit GROUP BY Category ORDER BY amount DESC;

关于这部分:

但是当我将某些内容提交到我的数据库中时,它会在那里..我提交其他内容然后顶部转到第二个

原因很简单,您使用的是ORDER BY hit_id DESC,这就是为什么最后插入的 ID 先出现的原因。

于 2013-05-04T02:08:34.523 回答