如何在sqli中进行分页?

时间:2016-11-21 09:50:44

标签: php html5 sqlite

我从数据库中获取数据,但数据量巨大。出于这个原因,我想将这些数据分页为每页10条记录。

我已经完成了一些分页代码,但它无法正常工作。 下面是我为分页所做的代码:

<div class="col-md-8 about_img about_img_left">
            <div class="choose-grid wow fadeInRight animated" data-wow-delay=".5s">
                <h4>Product Result : </h4></br>
<?php 
$num_rec_per_page=10;
mysqli_connect('localhost','root','');
mysqli_select_db('sample');
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * $num_rec_per_page; 
$sql = "SELECT * FROM data1 LIMIT $start_from, $num_rec_per_page"; 
$rs_result = mysqli_query ($connection ,$sql); //run the query
?> 
   <div class="table-responsive">                   
     <table class="table" width="700" border="2" cellspacing="0"    cellpadding="4">
  <tr>
    <td width="90" bgcolor="#CCCCCC"><strong>Drug category</strong></td>
    <td width="95" bgcolor="#CCCCCC"><strong>Product Name</strong></td>
    <td width="159" bgcolor="#CCCCCC"><strong>Brand name</strong></td>
    <td width="191" bgcolor="#CCCCCC"><strong>Dosage Form</strong></td>
    <td width="113" bgcolor="#CCCCCC"><strong>Strengths </strong></td>
  </tr>
    <?php

    if ($_REQUEST["string"]<>'') {
        $search_string = " AND (Brand_name LIKE     '%".mysqli_real_escape_string($connection,$_REQUEST["string"])."%')";
    }

    if ($_REQUEST["string1"]<>'') {
        $search_string1 = " AND (Product_name LIKE '%".mysqli_real_escape_string($connection,$_REQUEST["string1"])."%')";
    }

    if ($_REQUEST["Strengths"]<>'') {
        $search_Strengths = " AND Strengths='".mysqli_real_escape_string($connection,$_REQUEST["Strengths"])."'";   
    }

    if ($_REQUEST["Drug_category"]<>'') {
        $search_Drug = " AND Drug_category='".mysqli_real_escape_string($connection,$_REQUEST["Drug_category"])."'";    
    }

    if ($_REQUEST["Dosage_form"]<>'') {
        $search_dosage = " AND Dosage_form='".mysqli_real_escape_string($connection,$_REQUEST["Dosage_form"])."'";  
    }


    if ($_REQUEST["from"]<>'' and $_REQUEST["to"]<>'') {
        $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE from_date >= '".mysqli_real_escape_string($_REQUEST["from"])."' AND to_date <= '".mysqli_real_escape_string($_REQUEST["to"])."'".$search_string.$search_city;
    } else if ($_REQUEST["from"]<>'') {
        $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE from_date >= '".mysqli_real_escape_string($_REQUEST["from"])."'".$search_string.$search_city;
    } else if ($_REQUEST["to"]<>'') {
        $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE to_date <= '".mysqli_real_escape_string($_REQUEST["to"])."'".$search_string.$search_city;
    } else {
        $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE id>0".$search_string.$search_Strengths.$search_email.$search_dosage.$search_string1.$search_Drug ;
    }

    $sql_result = mysqli_query ($connection,$sql) or die ('request "Could not execute SQL query" '.$sql);
    if (mysqli_num_rows($sql_result)>0) {
        while ($row = mysqli_fetch_assoc($sql_result)) {
    ?>
  <tr>
     <td><?php echo $row["Drug_category"]; ?></td>
     <td><?php echo $row["Product_name"]; ?></td>
     <td><?php echo $row["Brand_name"]; ?></td>
     <td><?php echo $row["Dosage_form"]; ?></td>
     <td><?php echo $row["Strengths"]; ?></td>
 </tr>
 <?php
      }
      } else {
       ?>
         <tr><td colspan="5">No results found.</td>
       <?php    
        }    
       ?>
 </table>
  <?php 
  $sql = "SELECT * FROM data1"; 
  $rs_result = mysqli_query($connection,$sql); //run the query
  $total_records = mysqli_num_rows($rs_result);  //count number of records
  $total_pages = ceil($total_records / $num_rec_per_page); 

  echo "<a href='search.php?page=1'>".'|<'."</a> "; // Goto 1st page  

  for ($i=1; $i<=$total_pages; $i++) { 
        echo "<a href='search.php?page=".$i."'>".$i."</a> "; 
   }; 
   echo "<a href='search.php?page=$total_pages'>".'>|'."</a> "; // Goto last    page
   ?>

  </div>

请帮我分页这些数据。

0 个答案:

没有答案