使用$ _post对搜索结果进行分页

时间:2017-01-11 07:35:01

标签: php pagination

您好我有一个搜索表单,我正在搜索该表单的结果。 它适用于获取数据,但我想将这些结果放入分页或加载更多数据。 iam newbie to pagination.below是mycode。如何将分页应用于下面的代码。

           <form method="post" action="searchresults_t1.php"   enctype="multipart/form-data" >
    <table width="60%" padding="8px" >
        <tr>
        <td><label><font style="font-size:20px">Run No:</font></label> </td>
        <td><input  type="text" name="run" placeholder="Eg:46A" size=32 style="font-size:18px"></td>
       </tr>
        <tr>
         <td><label><font style="font-size:20px">Project Name:</font>           <label></td>
          <td><input  type="text" name="project" placeholder="Eg:chickpea3000 genome" size=32 style="font-size:18px"></td>
           </tr>
          <tr>
              <td><label><font style="font-size:20px">Sample Name: </font><label></td>
             <td><input  type="text" name="sample" placeholder="Eg:ICC2177" size=32 style="font-size:18px"></td>
            </tr>
             <tr>
            <td><label><font style="font-size:20px">Bases:</font>                        </label>
            <select style="font-size:18px" name="select" >
             <option value="greaterthan" selected="selected">&gt;                 </option>
               <option value="greaterthanorequal">&ge;</option>
               <option value="lessthan">&lt;</option>
               <option value="lessthanorequal">&le;</option>
                  <option value="equal">&#x3d;</option>
                </select>
                </td>
           <td><input  type="text" name="bases" placeholder="Eg:10125918250" size=32 style="font-size:18px"></td>
           </tr>
            <tr>
           <td><label><font style="font-size:20px">Reads:</font></label>
          <select style="font-size:18px" name="select1" >
           <option value="greaterthan" selected="selected">&gt;  </option>
          <option value="greaterthanorequal">&ge;</option>
         <option value="lessthan">&lt;</option>
        <option value="lessthanorequal">&le;</option>
          <option value="equal">&#x3d;</option>
         </select>
           </td>
              <td><input  type="text" name="reads" placeholder="Eg:81007346" size=32 style="font-size:18px"></td>
               </tr>
                  <tr>
               <td></td>
           <td><input type="reset" name ="reset" value="Reset" style="font-size:18px">
               <input type="submit" name="submit" value="Search" style="font-size:18px"></td>
                 </tr>
                  </table>
                </form>

        and searchesults_t1.php code:

           <?php
          include_once 'dbconnect.php';

          if(isset($_POST['submit']))
          {
            if(!empty($_POST['run']))
               {
             $value1=$_POST['run'];
            $sql="SELECT a.run, a.lane, a.project,  a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE a.run like '%$value1%' ";
              }
           else if(!empty($_POST['project']))
            {
              $value1=$_POST['project'];
           $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE a.project like '%$value1%'"; 
               }
             else if(!empty($_POST['sample']))
                {
              $value1=$_POST['sample'];
              $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE a.sample_id like '%$value1%'"; 
             }
             else if(!empty($_POST['bases']))
             {
             $values=$_POST['select'];
              if($values=='single')
              {
              $values1=$_POST['select1'];
              if($values1=='greaterthan')
                {
                $value1=$_POST['bases'];
                  $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE a.end_type='single' and b.raw_bases >'$value1' order by b.raw_bases"; 
             } 
               else if($values1=='greaterthanorequal')
             {
              $value1=$_POST['bases'];
              $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE a.end_type='single' and b.raw_bases >='$value1'order by b.raw_bases"; 
          } 
       else if($values1=='lessthan')
         {
           $value1=$_POST['bases'];
              $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE a.end_type='single' and  b.raw_bases <'$value1'order by b.raw_bases desc"; 
          }
          else if($values1=='lessthanorequal')
          {
          $value1=$_POST['bases'];
           $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE a.end_type='single' and b.raw_bases <='$value1' order by b.raw_bases desc"; 
             }
             else if($values1=='equal')
              {
                   $value1=$_POST['bases'];
                  $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE a.end_type='single' and b.raw_bases ='$value1'"; 
               }
               }
              else if($values=='pair')
             {
               $values1=$_POST['select1'];
                if($values1=='greaterthan')
                  {
              $value1=$_POST['bases'];
               $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE (b.raw_bases + c.raw_bases2 ) >'$value1' order by (b.raw_bases + c.raw_bases2 )"; 
               } 
              else if($values1=='greaterthanorequal')
               {
               $value1=$_POST['bases'];
                $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE (b.raw_bases + c.raw_bases2 ) >='$value1' order by (b.raw_bases + c.raw_bases2 )"; 
                } 
                 else if($values1=='lessthan')
                 {
                 $value1=$_POST['bases'];
                 $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE (b.raw_bases + c.raw_bases2 ) <'$value1' order by (b.raw_bases + c.raw_bases2 ) desc"; 
                    }
                else if($values1=='lessthanorequal')
                {
                $value1=$_POST['bases'];
                  $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE (b.raw_bases + c.raw_bases2 ) <='$value1' order by (b.raw_bases + c.raw_bases2 ) desc"; 
}
       else if($values1=='equal')
           {
           $value1=$_POST['bases'];
                $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE (b.raw_bases + c.raw_bases2 ) ='$value1'"; 
             }
              } 
           }
             else if(!empty($_POST['reads']))
      {
$values3=$_POST['select3'];
if($values3=='single')
 {
  $values4=$_POST['select4'];
   if($values4=='greaterthan')
   {
    $value5=$_POST['reads'];
    $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE a.end_type='single' and b.raw_reads >'$value5' order by b.raw_reads";
   }
   else if($values4=='greaterthanorequal')
   {
    $value5=$_POST['reads'];
    $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE a.end_type='single' and b.raw_reads >='$value5' order by b.raw_reads";
    }
       else if($values4=='lessthan')
     {
           $value5=$_POST['reads'];
             $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE a.end_type='single' and b.raw_reads <'$value5' order by b.raw_reads desc";
   }
  else if($values4=='lessthanorequal')
  {
   $value5=$_POST['reads'];
   $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE a.end_type='single' and b.raw_reads <='$value5' order by b.raw_reads desc";
        }
     else if($values4=='equal')
       {
  $value5=$_POST['reads'];
  $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE a.end_type='single' and b.raw_reads ='$value5'";
      }
        }
       else if($values3=='pair')
         {
            $values4=$_POST['select4'];
          if($values4=='greaterthan')
          {
             $value5=$_POST['reads'];
              $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE (b.raw_reads + c.raw_reads2 ) >'$value5' order by (b.raw_reads + c.raw_reads2 )"; 
              } 
           else if($values4=='greaterthanorequal')
           {
             $value5=$_POST['reads'];
            $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE (b.raw_reads + c.raw_reads2 ) >='$value5' order by (b.raw_reads + c.raw_reads2 )"; 
           } 
             else if($values4=='lessthan')
              {
                $value5=$_POST['reads'];
              $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE (b.raw_reads + c.raw_reads2 ) <'$value5' order by (b.raw_reads + c.raw_reads2) desc"; 
               }
            else if($values4=='lessthanorequal')
              {
               $value5=$_POST['reads'];
           $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE (b.raw_reads + c.raw_reads2 ) <='$value5' order by (b.raw_reads + c.raw_reads2) desc"; 
              }
             else if($values4=='equal')
                {
            $value5=$_POST['reads'];
             $sql="SELECT a.run, a.lane, a.project, a.sample_id,a.end_type,b.raw_bases, b.raw_reads, c.raw_bases2, c.raw_reads2 FROM cp_sampleSheet a left JOIN cp_samp_R1 b ON a.sample_id = b.sample_id left JOIN cp_samp_R2 c ON a.sample_id =c.sample_id WHERE (b.raw_reads + c.raw_reads2 ) ='$value5'"; 
            }   
           }
         }
       }
      $counter=0;

        if($result = mysqli_query($link, $sql)){
             if(mysqli_num_rows($result) > 0){
              ?>
       <table id='t01' >
          <tr>
         <th>Sno</th>
        <th>Run</th>
         <th>Lane</th>
         <th>Sample Name</th>
          <th>End Type</th>
        <th>Project Name</th>
          <th>Total Raw Bases</th>
          <th>Bases(In Gbp)</th>
         <th>Total Raw Reads</th>
        <th>Reads(In Mbp)</th>
           </tr>

             <?php
    while($row = mysqli_fetch_array($result)){

             ?>
           <tr>
           <td><?php echo ++$counter;?> </td>       
           <td><?php echo $row['run'] ;?> </td>
           <td><?php echo $row['lane'] ;?> </td>
           <!-- <td><?php echo $row['sample_id'] ;?> </td> -->

           <td><a  onclick="popupCenter('fetchsample_sheet.php?id=<?php echo $row['sample_id']; ?>','myPop1',1000,180);" href="javascript:void(0);"> <?php echo $row['sample_id']; ?></a></td>   

            <td><?php echo $row['end_type'] ;?> </td>
           <td><?php echo $row['project'] ;?> </td>

                                <!--  <?php 
                $rawsql = "SELECT * FROM cp_samp_R1 where sample_id='".$row['sample_id']."'"; 
                 $res2= mysqli_query($link, $rawsql);
                 $rowres2 = mysqli_fetch_array($res2);

                 $rawsql1 = "SELECT * FROM cp_samp_R2 where sample_id='".$row['sample_id']."'"; 
                 $res3= mysqli_query($link, $rawsql1);
                 $rowres3 = mysqli_fetch_array($res3);
                ?> 

            <td> <?php $a= (($rowres2['raw_bases']) + ($rowres3['raw_bases2'])); echo $a; ?></td> -->
            <td><?php  $a=(($row['raw_bases'])+($row['raw_bases2'])); echo $a;?> </td>
             <td><?php  $a=(($row['raw_bases'])+($row['raw_bases2']))/1000000000; $var = floor($a*100)/100; echo $var;?> </td>
              <td><?php  $b=(($row['raw_reads'])+($row['raw_reads2'])); echo $b;?> </td>
               <td><?php  $b=(($row['raw_reads'])+($row['raw_reads2']))/1000000; $var = floor($b*100)/100; echo $var;?>             </td>

            </tr>

         <?php
                }

             // Close result set
                mysqli_free_result($result);
              } 
              else{
             echo  " No records matching your query were found.";
              }
              } 
           else{
           echo  "ERROR: Could not able to execute $sql. " .                 mysqli_error($link);
                }

         //Close connection
          mysqli_close($link); 
              ?>

2 个答案:

答案 0 :(得分:0)

最简单的分页

原始答案:Simple PHP Pagination script

try {

    // Find out how many items are in the table
    $total = $dbh->query('
        SELECT
            COUNT(*)
        FROM
            table
    ')->fetchColumn();

    // How many items to list per page
    $limit = 20;

    // How many pages will there be
    $pages = ceil($total / $limit);

    // What page are we currently on?
    $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
        'options' => array(
            'default'   => 1,
            'min_range' => 1,
        ),
    )));

    // Calculate the offset for the query
    $offset = ($page - 1)  * $limit;

    // Some information to display to the user
    $start = $offset + 1;
    $end = min(($offset + $limit), $total);

    // The "back" link
    $prevlink = ($page > 1) ? '<a href="?page=1" title="First page">&laquo;</a> <a href="?page=' . ($page - 1) . '" title="Previous page">&lsaquo;</a>' : '<span class="disabled">&laquo;</span> <span class="disabled">&lsaquo;</span>';

    // The "forward" link
    $nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">&rsaquo;</a> <a href="?page=' . $pages . '" title="Last page">&raquo;</a>' : '<span class="disabled">&rsaquo;</span> <span class="disabled">&raquo;</span>';

    // Display the paging information
    echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, ' pages, displaying ', $start, '-', $end, ' of ', $total, ' results ', $nextlink, ' </p></div>';

    // Prepare the paged query
    $stmt = $dbh->prepare('
        SELECT
            *
        FROM
            table
        ORDER BY
            name
        LIMIT
            :limit
        OFFSET
            :offset
    ');

    // Bind the query params
    $stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
    $stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
    $stmt->execute();

    // Do we have any results?
    if ($stmt->rowCount() > 0) {
        // Define how we want to fetch the results
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $iterator = new IteratorIterator($stmt);

        // Display the results
        foreach ($iterator as $row) {
            echo '<p>', $row['name'], '</p>';
        }

    } else {
        echo '<p>No results could be displayed.</p>';
    }

} catch (Exception $e) {
    echo '<p>', $e->getMessage(), '</p>';
}

答案 1 :(得分:0)

在此链接中尝试Nev Stokes提供的此脚本:Simple PHP Pagination script

 try {

            // Find out how many items are in the table
        $total = $dbh->query('
            SELECT COUNT(*) FROM table WHERE column_name LIKE %searched_value%
        ')->fetchColumn();

        // How many items to list per page
        $limit = 20;

        // How many pages will there be
        $pages = ceil($total / $limit);

        // What page are we currently on?
        $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
            'options' => array(
                'default'   => 1,
                'min_range' => 1,
            ),
        )));

        // Calculate the offset for the query
        $offset = ($page - 1)  * $limit;

        // Some information to display to the user
        $start = $offset + 1;
        $end = min(($offset + $limit), $total);

        // The "back" link
        $prevlink = ($page > 1) ? '<a href="?page=1" title="First page">&laquo;</a> <a href="?page=' . ($page - 1) . '" title="Previous page">&lsaquo;</a>' : '<span class="disabled">&laquo;</span> <span class="disabled">&lsaquo;</span>';

        // The "forward" link
        $nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">&rsaquo;</a> <a href="?page=' . $pages . '" title="Last page">&raquo;</a>' : '<span class="disabled">&rsaquo;</span> <span class="disabled">&raquo;</span>';

        // Display the paging information
        echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, ' pages, displaying ', $start, '-', $end, ' of ', $total, ' results ', $nextlink, ' </p></div>';

        // Prepare the paged query
        $stmt = $dbh->prepare('
            SELECT
                *
            FROM
                table
            WHERE column_name 
            LIKE %:searched_value%
            ORDER BY
                name
            LIMIT
                :limit
            OFFSET
                :offset
        ');

        // Bind the query params
        $stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
        $stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
        $stmt->bindParam(':searched_value', $searched_value, PDO::PARAM_STR);
        $stmt->execute();

        // Do we have any results?
        if ($stmt->rowCount() > 0) {
            // Define how we want to fetch the results
            $stmt->setFetchMode(PDO::FETCH_ASSOC);
            $iterator = new IteratorIterator($stmt);

            // Display the results
            foreach ($iterator as $row) {
                echo '<p>', $row['name'], '</p>';
            }

        } else {
            echo '<p>No results could be displayed.</p>';
        }

    } catch (Exception $e) {
        echo '<p>', $e->getMessage(), '</p>';
    }

由于您还是新手,我建议您参加此处https://www.codeofaninja.com/2011/12/php-and-mysql-crud-tutorial.html

中的CRUD教程

它包括分页和许多其他有趣的东西。

祝你好运