2016-09-11 1 views
1

Ich möchte Daten MySQL auf der Grundlage der Dropdown-Auswahl abrufen. Ich möchte, dass mein Dropdown 3 Optionen 10,20,30 hat. wenn ich 10 anklickte, dann wurden die ersten 10 Datensätze auf meiner Seite angezeigt. und als ich auf 20 geklickt habe als die ersten 20 auf den Seiten gezeigten Datensätze. Ich habe es versucht, habe es aber nicht geschafft. Bitte irgendjemand kann mir helfen, wie es geht. Ich werde großartig voll sein, wenn Sie mir ein Beispiel dafür geben. Ich verwende PDO-Erweiterung von MySQL.Drop-Down-Auswahl von Daten von MySQL

<?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> 

Nun, wie es mit meiner Datenbank zu kommunizieren.

+1

Sie sollten den Code teilen, die Sie eher versucht, als – RamRaider

+0

Bro für ein volles Beispiel zu fragen Eigentlich habe ich keine Ahnung, wie dies zu implementieren . Bitte hilf mir, meine Lösung zu finden. Ich bin großartig für dich. –

Antwort

1

Ich sehe, dass Sie Javascript verwenden. Vielleicht sollten Sie es tun, ohne die Seite neu zu laden. Zeigen Sie einfach alle Einträge an, blenden Sie sie alle aus und zeigen Sie die guten Einträge an.

//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>

Individuelle Code

<?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> 
+0

Dies ist die genaue Antwort, die ich will. Aber ich verstehe noch nicht, wie ich das in meinen Code einfügen soll. Kannst du mir bitte helfen, dies in meinen Code zu schreiben? Ich bearbeite meinen Code in meiner Frage. dann hilf mir bitte. –

+0

Ich brauche mehr Code, du zeigst im Grunde nichts. – Treast

+0

Ich füge meinen Code in meine Frage ein. Bitte helfen Sie mir, Ihren Code in Minen zu setzen. Großartig für dich, Bruder. –

0
//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>