dongyan1808 2015-04-15 04:05
浏览 40
已采纳

使用php mysql面临分页困难

I am showing results arranged in table from mysql database using php, but I am not being able to find a way to paginate my results, because of the data being arranged in table and because some of the rows are missing and as webpage will be dynamic so user will be able to delete rows and insert new rows again and again, so because of that I can't get my results paginated using auto_increment value as well.

Following is my script:

<?php
    $mysql_session = mysql_query("SELECT * FROM session");
    $no_of_rows_session = mysql_num_rows($mysql_session);
    $msg_session='';
    $last_row = mysql_query("SELECT * from session ORDER BY id DESC LIMIT 1");
    $last_id = '';

    while($select_id_of_last_row = mysql_fetch_array($last_row)) {
        $last_id = $select_id_of_last_row['id'];
    }
?>



<?php echo $msg_session; ?>
<?php
    if($no_of_rows_session < 1) {
        $msg_session = 'No Session has been added yet';
    } else {
        for($i = 1; $i<=$last_id; $i++) {                   
            ${'mysql_session_every_single_query_' . $i} = mysql_query("SELECT * FROM session WHERE id LIKE '%$i%'");
            ${'mysql_existance_of_session_id_' . $i} = mysql_num_rows(${'mysql_session_every_single_query_' . $i});
            while(${'mysql_every_session_data_' . $i} = mysql_fetch_array(${'mysql_session_every_single_query_' . $i})){
                ${'id_session_' . $i} = ${'mysql_every_session_data_' . $i}['id'];
                ${'session_start_' . $i} = ${'mysql_every_session_data_' . $i}['start'];
                ${'session_end_' . $i} = ${'mysql_every_session_data_' . $i}['end'];
                echo "<table>";
                    echo "<tr id='tr_session_hover_" . $i . "'
                                class='tr_session_hover' 
                                onClick=\"document.location='session_edit.php?ss=${'session_start_' . $i}&se=${'session_end_' . $i}';\">
                            <td>" . ${'session_start_' . $i} . "-" . ${'session_end_' . $i} . "</td>
                        </tr>";
                echo "</table>";
            }
        }

    }     
?>
  • 写回答

2条回答 默认 最新

  • duanchen1937 2015-04-15 10:29
    关注

    I have got a solution. If you are also facing such kind of problem and want to proceed with variables only (like me):

    suppose you need to show 6 results on one page and your auto_increment (suppose a column named id is the auto_increment one, in your table) values are't regular, then what you can do is, get id of the 6th element by

    I am supposing that you have already connected to phpmyadmin and selected a database

    $no_of_results_per_page = 6;
    
    if(isset($_GET['page_no'])) {
      $page_no = $_GET['page_no']; 
    } else{
      $page_no = 1;
    
    }
    
    
      $upper_limit = ($page_no - 1)*$no_of_result_per_page - 1;
    
    if($page_no > 1){
      $mysql_getting_id_le = mysql_query("SELECT id FROM table LIMIT $upper_limit, 1");
    
      while($getting_id_last_elem = mysql_fetch_array($mysql_getting_id_le)) {
        $id_last_element = $getting_id_last_elem['id'];
      }
    } else {
      $mysql_getting_id_le = mysql_query("SELECT id FROM table LIMIT 0, 1");
    
      while($getting_id_last_elem_p_1 = mysql_fetch_array($mysql_getting_id_le)) {
        $id_last_element = $getting_id_last_elem_p_1['id'];
    $id_last_element = $id_last_element - 1;
      }
    }
    

    The above query will help us in getting id of the last element of the page

    Now we will be creating the buttons to navigate from one page to another.

    $final_mysql_query = mysql_query("SELECT * FROM table WHERE id > '%id_last_element%' LIMIT $no_of_results_per_page");
    
    //Now, we can get all data limited to show no. of results you have defined per page using a loop
    
    $total_results = mysql_query("SELECT * FROM table");
    
    $total_results_count = mysql_num_rows($total_results);
    
    $total_pages = ceil($total_results_count/$no_of_results_per_page);
    
    $page_no = '';
    
    for($i = 1; $i <= $total_pages; $i++) {
      $page_no .= "<a href='?page_no=" . $i . "'>" . $i . "</a>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题