2016-08-18 37 views
2

我想做一個下拉菜單,用戶選擇一個字母,然後輸出在我創建的表中以這個字母開頭的所有名稱。 名稱從數據庫中選擇。現在,我希望當用戶還沒有選擇一個字母或者沒有數據庫結果時隱藏這張表。所以我希望只有當數據庫有結果時纔會顯示錶格。這裏是我的代碼:隱藏所有的html表,其中沒有從mysql數據庫的結果

<html> 
 

 
<head> 
 
    <title>Title</title> 
 
    <style> 
 
    table { 
 
     width: 100%; 
 
     border-collapse: collapse; 
 
    } 
 
    table, 
 
    td, 
 
    th { 
 
     border: 1px solid black; 
 
     padding: 5px; 
 
    } 
 
    th { 
 
     text-align: left; 
 
    } 
 
    </style> 
 

 
</head> 
 

 
<body> 
 
    <?php $con=mysqli_connect('localhost', 'root', 'evolvia2016', 'presta_prova'); if (!$con) { die('Could not connect: ' . mysqli_error($con)); } ?> 
 
    <form method="post"> 
 
    <select name="users"> 
 
     <option value="">Scegliete una lettera:</option> 
 
     <option value="A">A</option> 
 
     <option value="B">B</option> 
 
     <option value="C">C</option> 
 
     <option value="D">D</option> 
 
    </select> 
 
    <input type="submit" name="submit" value="Go" /> 
 
    </form> 
 
    <br> 
 
    <div id="txtHint"><b>Vedi qui i tipi ti marche:</b> 
 
    </div> 
 
    <table id='results'> 
 
    <tr> 
 
     <th>Marca</th> 
 
     <th>Descrizione</th> 
 
    </tr> 
 
    <?php $q=$ _POST[ "users"]; //$sql="SELECT * FROM presta_prova WHERE marca LIKE 'B%' " ; $sql="SELECT * FROM presta_prova WHERE marca LIKE '" . $q . "%' "; $result=mysqli_query($con,$sql); while($row=mysqli_fetch_array($result)) { echo "<tr>"; echo 
 
    "<td>" . $row[ "marca"] . "</td>"; echo "<td>" . $row[ "descrizione"] . "</td>"; echo "</tr>"; } echo "</table>"; ?> 
 
    </table> 
 
</body> 
 
<?php mysqli_close($con); ?> 
 

 
</html>

我嘗試插入一個隱藏屬性,但它並沒有解決問題。有人能告訴我如何讓它隱藏在上面的情況下?謝謝!

+0

邏輯是檢查你的結果,如果結果爲空/不是。根據條件將表格CSS屬性更改爲display =「none」同樣 – Karthi

回答

0

這裏是邏輯

<?php 
$con=mysqli_connect('localhost', 'root', 'evolvia2016', 'presta_prova'); if (!$con) { die('Could not connect: ' . mysqli_error($con)); } 
$sql= mysqli_query($con, "SELECT * FROM presta_prova WHERE marca LIKE 'B%'") ; 
$row = mysqli_fetch_assoc($sql); 
$totalRows_results = mysqli_num_rows($sql);  
?> 


<?php if($totalRows_results > 0) { ?> 

Anything inside here will be visible if the query returns any result 
//e.g you may echo out your results here <?php echo $row['column']; ?> 


    you can also do your while loop inside here 
    <?php do { ?> 
    //your repeating region 
     <?php echo $row['column']; ?> //i.e. <?php echo $row['marca']; ?> 
    <?php } while ($row = mysqli_fetch_assoc($sql)); ?> 

<?php } ?> 



<?php if($totalRows_results == 0) { ?> 

Anything inside here will be visible if the query does not return any result 
//e.g you may display an error message here like. sorry no result found 

<?php } ?> 
0

這可能會幫助你,簡單的方法

PHP作品只在頁面加載而已,所以你需要使用的Javascript/jQuery來隱藏表,否則使用PHP mysqli_num_rows()函數,該函數返回數記錄取出,如果取行,生成表,否則留下一些警告信息,我添加了樣本代碼

<html> 
    <head> 
     <title>Title</title> 
     <style> 
     table { 
      width: 100%; 
      border-collapse: collapse; 
     } 
     table, 
     td, 
     th { 
      border: 1px solid black; 
      padding: 5px; 
     } 
     th { 
      text-align: left; 
     } 
     </style> 
    </head> 
    <body> 
     <?php $con=mysqli_connect('localhost', 'root', 'evolvia2016', 'presta_prova'); if (!$con) { die('Could not connect: ' . mysqli_error($con)); } ?> 
     <form method="post"> 
     <select name="users"> 
      <option value="">Scegliete una lettera:</option> 
      <option value="A">A</option> 
      <option value="B">B</option> 
      <option value="C">C</option> 
      <option value="D">D</option> 
     </select> 
     <input type="submit" name="submit" value="Go" /> 
     </form> 
     <br> 
     <div id="txtHint"><b>Vedi qui i tipi ti marche:</b> 
     </div> 
     <?php $q=$ _POST[ "users"]; //$sql="SELECT * FROM presta_prova WHERE marca LIKE 'B%' " ; $sql="SELECT * FROM presta_prova WHERE marca LIKE '" . $q . "%' "; 
$result=mysqli_query($con,$sql); 
if(mysqli_num_rows($result)>0) // if record found, this generate the table 
{ 
echo "<table id='results'> 
     <tr> 
      <th>Marca</th> 
      <th>Descrizione</th> 
     </tr>"; 
while($row=mysqli_fetch_array($result)) { echo "<tr>"; echo 
     "<td>" . $row[ "marca"] . "</td>"; echo "<td>" . $row[ "descrizione"] . "</td>"; echo "</tr>"; } echo "</table>"; 
} 
else 
    echo "No Output for the Query"; 
?> 
    </body> 
    <?php mysqli_close($con); ?> 
    </html> 
+0

現在輸出爲空。 –

+0

@Doe檢查您的查詢,打印您的查詢並檢查Phpmyadmin中是否獲取數據 – Keerthivasan

相關問題