dpzbzp8728 2018-02-01 13:47
浏览 48

您好我正在尝试使用php mysql进行多次排序,我正在搜索然后对数据进行排序

i'm printing a table in php where data is coming from mysql now i'm creating functionalities like searching and sorting so when i click on sort it sorts the data and when i click on search i get searched data now the problem is i want to perform sorting on searched data like for example i sorted the data and then i searched for words starting with a i.e i got results like adam,azan,anand so i want to perform resorting on these searched data and get data as adam,anand,azan my approach is:

<?php

if(isset($_GET['search_btn'])){
  $search=$_GET['search'];
  $result=GetWords(mysqli_escape_string($conn,$search));
}
/*if(isset($_GET['q'])){
      $id=$_GET['q'];
      $result=GetWordsById($id);
    }*/
    if(isset($_GET['sort'])){
      $sort=$_GET['sort'];
    }
    if(isset($_GET['sort'])){
      if($sort=="asc"){
        $result=SortContent();//Here Get Sort Content is a function calling Store Procedure SortContent which is working at first sorting
      }
      if($sort=="desc"){
        $result=SortContent2();
      }
    }


    else{
  $result=GetAdminWords();
}
  if(mysqli_num_rows($result)>0)
  ?>  
    <thead>
      <tr>

        <th>Word</th>
        <th>Meaning</th>
        <th>Synonym</th>
        <th>Antonym</th>

      </tr>
    </thead>
    <?php
    while($row=mysqli_fetch_array($result)){
    ?>
      <tbody>
        <tr>

          <td><a href="view.php?q=<?php echo $row['id'];?>"><?php echo $row['word'];?></a></td>
          <td><?php echo $row['meaning'];?></td>
          <td><?php echo $row['synonym'];?></td>
          <td><?php echo $row['antonym'];?></td>
         <td><a href="admin.php?id1=<?php echo $row['id'];?>"><i class="fa fa-edit"></i></a> <a onClick="javascript: return confirm('Please confirm deletion');" href="view.php?id=<?php echo $row['id'];?>"><i class="fa fa-trash"></i></a> </td>
        </tr>
      </tbody>
      <?php
  }?>

and i'm talking in context of large amount of data i hope i have made myself clear and if possible how can i implement ajax using mysqli

  • 写回答

1条回答 默认 最新

  • douxin9135 2018-02-01 14:26
    关注

    You will need to trigger an event in JavaScript, which in turn will use your HTML search input, which is then sent to the server, where a query will be executed and the results returned (as HTML) to the JavaScript code, and finally placed back on the page. At least this is how I solve my ajax searches...

    So the flow could be something like:

    Input -> JavaScript event -> ajax -> result -> page

    Here is some code that might get you started, though I haven't tested i myself:

    HTML:

    <input type="text" id="my_search_input">
    <div id="my_search_result"></div>
    

    JS (jQuery):

    var $inputField = $( "#my_search_input" ); 
    var $result = $( "#my_search_result" );
    $inputField.on('keyup', function(){ //triggered when a pressed key is lifted
        var searchTerm = $inputField.val();
        $.ajax({
             url:"/mySearch.php",
             method:"post",
             data:{searchTerm:searchTerm},
             success:function(response){ //response contains the data from mySearch.php
                 var parsedResponse = JSON.parse(response);
                 var resultHtml = parsedResponse.html; //this is the array key of what the PHP script returns
                 $result.append(resultHtml);
             }
        });
    });
    

    PHP

    $searchTerm = $_POST['searchTerm']; //$_POST['searchTerm'] is what we defined in data:{... in the ajax call
    // here is where you need to retrieve data from your database
    // the db result needs to be processed into HTML and assigned to a variable
    $html = "<div>My result based on data</div>";
    return json_encode(['html' => $html]);
    
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?