PDO分页从数据库中跳过数据

时间:2015-11-13 06:58:51

标签: mysql pdo pagination

使用数据库中的PDO创建了这个小分页,它从数据库中跳过数据,我无法弄清楚原因。它正在跳过数据库中的第一个和一些中间行。一旦我从表单中插入数据,它就会选择另一个中间行,但它会跳过另一行。如果我删除分页,包括$start_from它可以正常工作。

代码:

<?php
        echo '<table id="ravimiTable"  class="table table-bordered table-responsive">';
        echo '<tbody>';

        if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };
        $start_from = ($page-1) * 10;       
        $result = $DB_con->prepare("SELECT * FROM ravim WHERE arsti_id = ".$user_id." ORDER BY patsiendi_kood ASC LIMIT $start_from, 10");
        $result->execute();
        for($i=0; $row = $result->fetch(); $i++){
            foreach ($result  as $row ){
                echo '<tr class="">
                                      <td class="info">'.$row['ravimi_nimi'].'</td>
                                      <td class="">'.$row['patsiendi_kood'].'</td>
                                      <td class="">'.$row['patsiendi_tsykkel'].'</td>
                                      <td class="">'.$row['ravimi_kood'].'</td>
                                      <td class="">'.$row['patsiendi_vanus'].'</td>
                                      <td class="">'.$row['haigla'].'</td>
                                      <td class="danger">'.$row['diagnoos'].'</td>
                                      <td class="">'.$row['diagnoosi_kuup2ev'].'</td>
                                      <td class="">'.$row['pakend'].'</td>
                                      <td class="">'.$row['annustamine'].'</td>
                                      <td class="">'.$row['pakkide_arv'].'</td>
                                      <td class="warning">'.$row['patsiendi_kategooria'].'</td>
                                      <td class="">'.$row['rasestumisvastane_vahend'].'</td>
                                      <td class="">'.$row['rasedusuuringu_kuup2ev'].'</td>
                                      <td class="">'.$row['rasedusuuringu_tulemus'].'</td>
                                      <td class="">'.$row['arsti_kuup2ev'].'</td>
                                </tr>';
            }
            echo '</tbody></table></div>';

        ?>

        <div id="pagination" style="margin:0 auto; text-align:center;">
            <?php 

            $result = $DB_con->prepare("SELECT COUNT(patsiendi_kood) FROM ravim WHERE arsti_id = ".$user_id);
            $result->execute(); 
            $row = $result->fetch(); 
            $total_records = $row[0]; 
            $total_pages = ceil($total_records / 10); 

            for ($i=1; $i<=$total_pages; $i++) { 
                echo "<a href='forms.php?page=".$i."'";
                if($page==$i)
                {
                    echo "id=active";
                }
                echo ">";
                echo "".$i."</a> "; 
            }; 
            ?>
            <?php } ?>
        </div>

1 个答案:

答案 0 :(得分:1)

$result = $DB_con->prepare("SELECT * FROM ravim WHERE arsti_id = ".$user_id." ORDER BY patsiendi_kood ASC LIMIT $start_from, 10");
$result->execute();
$rowsArray = $result->fetchAll(PDO::FETCH_ASSOC);
            foreach ($rowsArray as $row ){
                echo '<tr class="">
                                      <td class="info">'.$row['ravimi_nimi'].'</td>
                                      <td class="">'.$row['patsiendi_kood'].'</td>
                                      <td class="">'.$row['patsiendi_tsykkel'].'</td>
                                      <td class="">'.$row['ravimi_kood'].'</td>
                                      <td class="">'.$row['patsiendi_vanus'].'</td>
                                      <td class="">'.$row['haigla'].'</td>
                                      <td class="danger">'.$row['diagnoos'].'</td>
                                      <td class="">'.$row['diagnoosi_kuup2ev'].'</td>
                                      <td class="">'.$row['pakend'].'</td>
                                      <td class="">'.$row['annustamine'].'</td>
                                      <td class="">'.$row['pakkide_arv'].'</td>
                                      <td class="warning">'.$row['patsiendi_kategooria'].'</td>
                                      <td class="">'.$row['rasestumisvastane_vahend'].'</td>
                                      <td class="">'.$row['rasedusuuringu_kuup2ev'].'</td>
                                      <td class="">'.$row['rasedusuuringu_tulemus'].'</td>
                                      <td class="">'.$row['arsti_kuup2ev'].'</td>
                                </tr>';
            }

您正在$row中提取结果数组,但正在迭代$result