我正在尝试用php和amp实现分页阿贾克斯

时间:2017-05-11 11:19:43

标签: jquery ajax pagination

我正在尝试使用php&阿贾克斯。 如果我对数据库进行排序或搜索,它就无法工作。否则它的工作正常。我无法用搜索和放大器实现它排序操作。它只显示第一页。

    <?php

if (isset($_GET["page"]))
{
  $page  = $_GET["page"];
}
else
{
  $page=1;
}
$results_per_page = 5;
$start_from = ($page-1) * $results_per_page;
$disp = "SELECT formdata4.user_id as user_id, formdata4.name as name, formdata4.email as email, formdata4.age as age, formdata4.gender as gender, formdata4.dob as dob, formdata4.address as address, citytable.city as city FROM formdata4 LEFT JOIN citytable ON formdata4.city_id = citytable.city_id ORDER BY user_id DESC LIMIT $start_from, $results_per_page";
$result = mysqli_query($con,$disp);
$disp2="select * from skilltable";
$result1=mysqli_query($con,$disp2);
?>

      <br><h2> User Database</h2>
      <div class="col-sm-12 well">
        <div class= "col-sm-1">
          <a type= "button" class="btn btn-primary" onclick="location.href='formnew1.php';" /><i class="fa fa-plus" aria-hidden="true"></i> Add</a>
        </div>
        <div class="col-sm-10">


         <form class="form-inline" >
          <div class="form-group">
            <label for="exampleInputName2">Search</label>
             <input type = "text" class="form-control" id = "search" name= "search" placeholder="Search Term Here ..."/>
          </div>
          <div class="form-group">
            <label for="exampleInputEmail2">From </label>
            <input type = "text" class="form-control" id = "datepicker1" name= "date1" placeholder="From Date"/>
          </div>
           <div class="form-group">
            <label for="exampleInputEmail2">To </label>
             <input type = "text" class="form-control" id = "datepicker2" name= "date2" placeholder="To Date"/>
          </div>
          <div class="form-group">
              <a type= "button" class="btn btn-default-search" name= "search" onclick="searchResult()"><i class="fa fa-search" aria-hidden="true"></i>  </a>
                <a href="display1.php" class="btn btn-success"><i class="fa fa-refresh" aria-hidden="true"></i></a>

          </div>
        </form>
        </div>
        <div class= "col-sm-1 pull-right">

        <a type= "button" class="btn btn-danger" onclick="location.href='logout.php';"><i class="fa fa-power-off" aria-hidden="true"></i> </a>
        </div>
       </div>
       <input type="hidden" id="sort" value="asc">
       <table id="table" class="table table-bordered table-hover">
              <thead>
              <tr style="border-color:black">
                     <th>  <a style="background-color:transparent" id="user_id" class="btn-default" onclick="sortTable(this)";>ID <i class="fa fa-sort" aria-hidden="true"></i></a></th>
                     <th>  <a style="background-color:transparent" id="name" class="btn-default" onclick="sortTable(this)";>Name <i class="fa fa-sort" aria-hidden="true"></i></a></th>
                     <th> Email </th>
                     <th> Age </th>
                     <th> Gender </th>
                     <th> D.O.B </th>
                     <th> Address </th>
                     <th> City </th>
                     <th> Skills </th>
                     <th> Action </th>
              </tr>
              </thead>
              <tbody id="filter">
              <?php
                      while ($row = $result->fetch_assoc())
                      {
                            $id = $row['user_id'];
                            $sel = "SELECT skill from skilltable WHERE user_id=$id";
                            $sel1=mysqli_query($con, $sel);
                            $tot1 = mysqli_num_rows($sel1);
                            $rows1=array();
                            for($i=0; $i<$tot1; $i++)
                            {
                                $row1 = mysqli_fetch_assoc($sel1);
                                $rows1[] = $row1['skill'];
                            }
                            $skl=implode(',', $rows1);
                            ?>
                            <tr>
                            <td><?php echo $row['user_id']?> </td>
                            <td><?php echo $row['name'] ?></td>
                            <td><?php echo $row['email']?></td>
                            <td><?php echo $row['age']?></td>
                            <td><?php echo $row['gender']?></td>
                            <td><?php echo date('d-M-Y', strtotime($row['dob'])); ?></td>
                            <td><?php echo $row['address']?></td>
                            <td><?php echo $row['city']?></td>
                            <td><?php echo $skl ?></td>
                            <td>
                            <a id="edit"  class="btn btn-info" href="edit1.php?id=<?php echo $row['user_id']; ?>"><i class="fa fa-pencil" aria-hidden="true"></i> Edit</a>
                            <a href="#"  class="btn btn-danger" id= "<?php echo $row['user_id'] ?>" onclick="deleteRow(this)"> <i class="fa fa-trash-o" aria-hidden="true"></i> Delete</a>
                            </td>
                            </tr>
                 <?php  }  ?>
                 </tbody>
       </table>
<?php
$sqli = "SELECT * from formdata4";
$sqli1 = mysqli_query($con, $sqli);
$total = mysqli_num_rows($sqli1);
$total_pages = ceil($total/$results_per_page);
?>
<ul class='pagination' id="pagination">
<?php
for ($i=1; $i<=$total_pages; $i++)
{
           if($page==$i)
           {
           echo "<li class='page-item active'><a class='page-link' href='display1.php?page=".$i."'>$i</a></li>";
           }
           else
           {
            echo "<li class='page-item'><a class='page-link' href='display1.php?page=".$i."'>$i</a></li>";
           }
}
?>
</ul>
       </div>
   <script type="text/javascript">
       $("#datepicker1").datepicker({ dateFormat: 'dd-MM-yy', defaultDate: null });
       $("#datepicker2").datepicker({ dateFormat: 'dd-MM-yy', defaultDate: null });

      function searchResult()
      {
        var fromDate = $("#datepicker1").val();
        var toDate = $("#datepicker2").val();
        var search = $("#search").val();
        $.post(
          "search.php",
          {search:search, fDate:fromDate, tDate:toDate},
          function(data)
          {
            var divs = data.split('|');
            $('#filter').html(divs[0]);
            $('#pagination').html(divs[1]);
          }
        );
      }

      function sortTable(column)
      {
        var columnName=column.id;
        var sort = $("#sort").val();
         $.post(
         "sort.php",
         {columnName:columnName,sort:sort},
         function(response){
          $('#filter').html(response);
          if(sort == "asc")
          {
            $("#sort").val("desc");
          }
          else
          {
            $("#sort").val("asc");
          }
         }
        );
      }
   </script>
?>

的search.php

   <?php
require_once("dbconnect.php");
$outputData = "";
$outputData1 = "";
$search = $_POST['search'];
$fromdate = $_POST['fDate'];
$todate = $_POST['tDate'];

//if empty query - No rows found
    if (empty($fromdate) && empty($todate) && empty($search))
               {
                    echo "<tr><td colspan='10'  class='text-center'>No Rows Found</td></tr>"; die;
               }

//process date
    if(empty($fromdate))
    {
    $date1 = "";
    }
    else
    {
    $date1 = date('Y-m-d', strtotime($fromdate));
    }
    if(empty($todate))
    {
    $date2 = date('Y-m-d');
    }
    else
    {
    $date2 = date('Y-m-d', strtotime($todate));
    }

//Pagination
if (isset($_GET["page"]))
{
  $page  = $_GET["page"];
}
else
{
  $page=1;
}
$results_per_page = 5;
$start_from = ($page-1) * $results_per_page;    

//process skill  
if(($search == 'html')||($search == 'php')||($search == 'javascript')||($search == 'css'))
{
    $skil="SELECT user_id from skilltable WHERE skill like '%$search%' ";
    $skil1 = mysqli_query($con, $skil);
    $tot = mysqli_num_rows($skil1);
    $rows=array();
    for($i=0; $i<$tot; $i++)
        {
            $row = mysqli_fetch_assoc($skil1);
            $rows[] = $row['user_id'];
        }
     $rowid=implode(',', $rows);   
     $disp = "SELECT formdata4.user_id as user_id, formdata4.name as name, formdata4.email as email, formdata4.age as age, formdata4.gender as gender, formdata4.dob as dob, formdata4.address as address, citytable.city as city FROM formdata4 LEFT JOIN citytable ON formdata4.city_id = citytable.city_id where user_id IN ($rowid) AND (dob between '".$date1."' AND '".$date2."') order by user_id DESC";// LIMIT $start_from, $results_per_page";
     $result = mysqli_query($con,$disp);
     $noRows = mysqli_num_rows($result);
     $total_pages = ceil($noRows/5);
}

 else
 {
     $disp = "SELECT formdata4.user_id as user_id, formdata4.name as name, formdata4.email as email, formdata4.age as age, formdata4.gender as gender, formdata4.dob as dob, formdata4.address as address, citytable.city as city FROM formdata4 LEFT JOIN citytable ON formdata4.city_id = citytable.city_id where (dob between '".$date1."' AND '".$date2."') AND (user_id like ('%$search%') OR name like ('%$search%') OR age like ('%$search%') OR gender like ('%$search%') OR address like ('%$search%') OR city like ('%$search%')) order by user_id DESC";
     $result = mysqli_query($con,$disp);
     $noRows = mysqli_num_rows($result);
     $total_pages = ceil($noRows/5);
 }

               if ($noRows!=null)
               {
                      while ($row = $result->fetch_assoc())
                      {
                            $id = $row['user_id'];
                            $sel = "SELECT skill from skilltable WHERE user_id=$id";
                            $sel1=mysqli_query($con, $sel);
                            $tot1 = mysqli_num_rows($sel1);
                            $rows1=array();
                            for($i=0; $i<$tot1; $i++)
                            {
                                $row1 = mysqli_fetch_assoc($sel1);
                                $rows1[] = $row1['skill'];
                            }
                            $skl=implode(',', $rows1);                    
                            $outputData.= "<tr>";
                            $outputData.="<td>".$row['user_id']."</td>";
                            $outputData.="<td>".$row['name']."</td>";
                            $outputData.="<td>".$row['email']."</td>";
                            $outputData.="<td>".$row['age']."</td>";
                            $outputData.="<td>".$row['gender']."</td>";
                            $outputData.="<td>".date('d-M-Y', strtotime($row['dob']))."</td>";
                            $outputData.="<td>".$row['address']. "</td>";
                            $outputData.="<td>".$row['city']."</td>";
                            $outputData.="<td>".$skl."</td>";
                            $outputData.="<td>";
                            $outputData.="<a id='edit' class='btn btn-info' href='edit1.php?id=".$row['user_id']."'><i class='fa fa-pencil' aria-hidden='true'></i> Edit</a>";
                            $outputData.="<a href='#' class='btn btn-danger' id= ".$row['user_id']." onclick='deleteRow(this)'><i class='fa fa-trash-o' aria-hidden='true'></i> Delete</a>";
                            $outputData.="</td>";
                            $outputData.="</tr>";
                  }
                         for ($i=1; $i<=$total_pages; $i++)
                         {
                              $outputData1.= "<li class='page-item'><a class='page-link' href='display1.php?page=".$i."'>$i</a></li>";

                         }

                    echo "$outputData|$outputData1";
               }
               else
               {
                    echo "<tr><td colspan='10'  class='text-center'>No Rows Found</td></tr>";
               }
?>  

0 个答案:

没有答案