dqpfkzu360216 2018-09-04 09:55
浏览 8
已采纳

php从表中选择有限数量的数据,并按日期顺序进行延迟加载

I am using lazy loading to load data from database to device the limit is 20 but my problem lies when I pull the data from table because I have to order it by latest next date.

eg. I pull all data for today then tommorrow and then so on I cant use the id to keep track of what I loaded becuase the entries have different dates when entered

this is and example of my table. please note that my table can have thousands of entries

|  id   | name  |    date    |   time   |
|   0   | name1 | 2018-09-30 | 01:00:00 |
|   1   | name2 | 2019-01-10 | 15:30:00 |
|   2   | name3 | 2019-09-30 | 10:20:00 |
|   3   | name4 | 2018-05-20 | 14:10:00 |
|   4   | name5 | 2020-09-02 | 01:30:00 |
|   5   | name6 | 2018-10-30 | 14:00:00 |

this is what my sql php script looks like. I know its not right but its an example of what im triying to do

SELECT * FROM table WHERE date >= '$today' AND id >= '$lastId' ORDER BY date ASC LIMIT 20

as you can see the id increments but the dates dont increment because it can be entered with any future date. I apologize for my bad english. Please can anyone help me

  • 写回答

1条回答 默认 最新

  • dongwen6743 2018-09-04 10:06
    关注

    to paginate : you can use php or ajax and the last's is recommended:

    <?php
    
    $limit = 2;  
    if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };  
    $start_from = ($page-1) * $limit;  
    
    $sql = "SELECT * FROM table ORDER BY field ASC LIMIT $start_from, $limit";  
    $rs_result = mysql_query ($sql);  
    ?>
    <table class="table table-bordered table-striped">  
    <thead>  
    <tr>  
    <th>title</th>  
    <th>body</th>  
    </tr>  
    </thead>  
    <tbody>  
    <?php  
    while ($row = mysql_fetch_assoc($rs_result)) {  
    ?>  
                <tr>  
                <td><? echo $row["field"]; ?></td>  
    
                </tr>  
    <?php  
    };  
    ?>  
    </tbody>  
    </table>    
    

    finally add this to your html code:

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

报告相同问题?

悬赏问题

  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码