php - 从mysql中下拉选择数据

标签 php jquery html mysql

我想在下拉选择的基础上获取数据mysql。我希望我的下拉菜单有 3 个选项 10、20、30。当我点击 10 时,我的页面中显示前 10 条记录。当我点击页面中显示的前 20 条记录时,我点击了 20 条记录。我试过了,但没有成功。请任何人都可以帮助我如何去做。如果给我一个这样的例子,我会对你很满意。 我正在使用 mysql 的 PDO 扩展。

<?php
  include('config.php');
  session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<script   src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<div id="resultdiv">
    <form method="POST">
         <table  class="table table-striped table-bordered" cellspacing="0" width="100%">
            <thead>
               <tr>
                 <th>Video ID</th>
                 <th>Video Name</th>
               </tr>
            </thead>
           <?php
           $access = $_SESSION['login'];
           $sql = "SELECT * FROM videos ORDER BY id desc";
           $query = $conn->query($sql);
            $i=1;
            while ($row = $query->fetch(PDO::FETCH_ASSOC)){
               $id = $row['id'];
               $video_name = $row['video_name'];
               echo "
                    <tbody>
                        <tr>
                          <td style='text-align:center;'>$id</td>
                          <td id='test10' class='text-center'><b>$video_name</b></td>
                          <td style='text-align:center;'>
                          <a href='add-video3.php?id=$id' class='btn btn-info'>EDIT</a><br><br>
                          <a href='delete_video.php?id=$id' class='btn btn-danger' rel='tooltip' title='Delete'><i class='glyphicon glyphicon-trash'></i></a>
                        </tr>
                   </tbody>";
                 } $i++;
               }
               else {
                   header ("location:index.php");
                    }
           ?>
       </table>
     </form>    
</div>
</body>

现在如何与我的数据库进行通信。

最佳答案

我看到您正在使用 Javascript。也许您应该在不重新加载页面的情况下执行此操作。只显示所有条目,然后将它们全部隐藏,并显示好的条目。

//Populate table
for(var i = 1; i<=30; i++){
  $('table tbody').append('<tr><td>Row '+i+'</td></tr>');  
}

//Listener on select change
$('select').on('change', function(){
  var value = $(this).val();
  $('table tbody tr').hide();
  for(var i = 0; i<value; i++){
    //Find the i tr on the table
    $('table tbody tr').eq(i).show(); 
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select id="dropdown">
  <option value="10">10</option>
  <option value="20">20</option>
  <option value="30" selected>30</option>  
</select>

<table>
  <tbody>
    
  </tbody>
</table>

自定义代码

<?php
include('config.php');
session_start();

$access = $_SESSION['login'];
$sql = "SELECT * FROM videos ORDER BY id desc";
$query = $conn->query($sql);

?>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
  <div id="resultdiv">
    <!-- start data table -->
    <table  class="table table-striped table-bordered" cellspacing="0" width="100%">
      <thead>
        <tr>
          <th>Video ID</th>
          <th>Video Name</th>
        </tr>
      </thead>

      <tbody>
        <?php
        while ($row = $query->fetch(PDO::FETCH_ASSOC)){
          $id = $row['id'];
          $video_name = $row['video_name'];
          echo "
          <tr>
           <td style='text-align:center;'>$id</td>
           <td id='test10' class='text-center'><b>$video_name</b></td>

           <td style='text-align:center;'>
             <a href='add-video3.php?id=$id' class='btn btn-info'>EDIT</a><br><br>
             <a href='delete_video.php?id=$id' class='btn btn-danger' rel='tooltip' title='Delete'><i class='glyphicon glyphicon-trash'></i></a>
           </tr>";
         }

         ?>
       </tbody>
     </table>
     <label for="select-row">Show </label>
     <select id="select-row">
       <option value="10">10</option>
       <option value="20">20</option>
       <option value="30">30</option>
       <option value="50">50</option>
     </select>
     <!-- End data table --> 
   </div>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
   <script type="text/javascript">
     //Listener on select change
    $('select').on('change', function(){
      var value = $(this).val();
      $('table tbody tr').hide();
      for(var i = 0; i<value; i++){
        //Find the i tr on the table
        $('table tbody tr').eq(i).show(); 
      }
    });
   </script>
 </body>

关于php - 从mysql中下拉选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39433595/

相关文章:

javascript - 仅在 IE 中的 Iframe 中执行缓慢的 javascript

html - 垂直溢出时随页面滚动的粘性侧边栏

html - 将显示应用于某些第 n 个 child

php - Magento 2 不构建产品缩略图缓存

php - 在 PHP 中使用 xor 进行变量交换是否安全

javascript - 限制函数在重置之前触发一次

html - 响应式数据表

php preg_match_all 不工作

php - 按 'X' 按钮后数据库更改

javascript - 为什么我的子节点没有被清空?