douzong3599 2016-11-29 16:32
浏览 17
已采纳

MySQL - 每个类别都有一个单独的id

My table looks like this: enter image description here

So in PHP I have the following:

$result = db_query("SELECT * FROM siteData WHERE category = '$temp_cat' ORDER BY id DESC LIMIT $offset, $rowsperpage");
// db_query() is a custom function which executes mysqli queries to the db

I'm displaying the id in my post HTML and use it as a permalink to the post. That's all good, but when I decide to filter by category I see the following:

Main page:

-Post Title-

-2- <-- Post ID

-Post Content-

-Post Footer-


-Post Title-

-1- <-- Post ID

-Post Content-

-Post Footer-

About Page:

-Post Title-

-4- <-- Post ID

-Post Content-

-Post Footer-


-Post Title-

-3- <-- Post ID

-Post Content-

-Post Footer-

And so on... What I mean with this is that posts have misleading IDs that are set upon post creation and automatically increase. This leads to About Page beginning with post that has id = 3 and not id = 1. Is there any possible way to make it count IDs for different categories separately?

  • 写回答

1条回答 默认 最新

  • donglie7778 2016-11-29 20:54
    关注

    As per discussion, this is what I have in my select.php file:

    <?php
        while ($row = mysqli_fetch_array($result)) {
            $id = $row['id'];
            $title = $row['title'];
            $category = $row['category'];
            $date = $row['date'];
    ?>
            Category: &#8220;<?php echo $category; ?>&#8221;
            &#160;<span class="fa fa-fw fa-lg fa-chevron-right" aria-hidden="true"></span>&#160;
            Title: <a href="edit.php?id=<?php echo $id; ?>" hreflang="en">&#8220;<?php echo $title ?>&#8221;</a>
            &#160;<span class="fa fa-fw fa-lg fa-chevron-right" aria-hidden="true"></span>&#160;
            Date: <time datetime="<?php echo $date; ?>"><?php echo $date; ?></time><br>
    <?php
        }
    ?>
    

    I then SELECT the post by the id which I $_GET from the URI and display some form-specific elements that hold the post values. Then I update the data in the db.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看