【问题标题】:Show/Hide records PHP MySQL显示/隐藏记录 PHP MySQL
【发布时间】:2019-03-20 20:01:15
【问题描述】:

由于我找不到任何关于此的信息,我想我应该在这里寻求帮助。

我正在处理一个当前看起来像这样的表 (PHP/MySQL):

https://i.gyazo.com/4115cf6fb14921ea9109580ec9c6c531.png

我想制作一个按钮,我可以在其中显示/隐藏一整列。所以假设我想隐藏“经理”,我可以按一个按钮,整个列都会隐藏。

我是编程新手,我已经尝试过不同的代码,但我无法让它工作。

if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
        echo "<table class='table table-bordered table-striped'>";
        echo "<thead>";
        echo "<tr>";
     ?>
        <th class="rotate2"><div><span>Naam</span></div></th>
        <th class="rotate2"><div><span>Functie</span></div></th>
        <th class="rotate2"><div><span>Afdeling</span></div></th>
        <th class="rotate2"><div><span>Contract</span></div></th>                      
        <th class="rotate2"><div><span>DID</span></div></th>
        <th class="rotate2"><div><span>DUD</span></div></th>
        <th class="rotate2"><div><span>Manager</span></div></th>
        <th class="rotate2"><div><span>Profiel</span></div></th>
 <?php 
   while($row = mysqli_fetch_array($result)){
        echo "<tr>";
        echo "<td>" . $row['naam'] . "</td>";
        echo "<td>" . $row['functie'] . "</td>";
        echo "<td>" . $row['afdeling'] . "</td>";
        echo "<td>" . $row['contract'] . "</td>";
        echo "<td>" . $row['DID'] . "</td>";
        echo "<td>" . $row['DUD'] . "</td>";
        echo "<td>" . $row['manager'] . "</td>";
        echo "<td>" . $row['profiel'] . "</td>";

希望有人能帮帮我,

我无法让图像工作,所以我做了一个 gyzago。

谢谢

【问题讨论】:

  • 你可以使用 javascript/JQuery。为列的所有标签赋予相同的唯一类并将它们一起显示/隐藏。
  • 为此使用JQuery
  • 好的,谢谢,我试试
  • 或者,您可以使用老式的方式进行操作:重新加载页面并且不呈现您不想要的列。这样你就可以在 PHP 中完成所有操作,而无需学习 JQuery,这在开始时可能会有些困难。

标签: php mysql button hide show


【解决方案1】:

你需要清理你的 PHP 代码,如下所示:-

<table class='table table-bordered table-striped'>
    <thead>
        <tr>
            <th class="rotate2"><div><span>Naam</span></div></th>
            <th class="rotate2"><div><span>Functie</span></div></th>
            <th class="rotate2"><div><span>Afdeling</span></div></th>
            <th class="rotate2"><div><span>Contract</span></div></th>                      
            <th class="rotate2"><div><span>DID</span></div></th>
            <th class="rotate2"><div><span>DUD</span></div></th>
            <th class="rotate2"><div><span>Manager</span><br><button class="show">Show</button><button class="hide">Hide</button></div></th>
            <th class="rotate2"><div><span>Profiel</span></div></th>
            <th class="rotate2"><div><span>Action</span></div></th>
        </tr>
    </thead>
    <tbody>
        <?php 
            if($result = mysqli_query($link, $sql)){
                if(mysqli_num_rows($result) > 0){
                    while($row = mysqli_fetch_array($result)){
                        echo "<tr>";
                        echo "<td>" . $row['naam'] . "</td>";
                        echo "<td>" . $row['functie'] . "</td>";
                        echo "<td>" . $row['afdeling'] . "</td>";
                        echo "<td>" . $row['contract'] . "</td>";
                        echo "<td>" . $row['DID'] . "</td>";
                        echo "<td>" . $row['DUD'] . "</td>";
                        echo "<td>" . $row['manager'] . "</td>";
                        echo "<td>" . $row['profiel'] . "</td>";
                        echo "</tr>";
                    }
                }
            }
        ?>
    </tbody>
</table>

然后在其下添加Jquery代码:-

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $('.show').hide();
  $('.hide').on('click',function(){
    $('tr').find('td:eq('+$(this).closest('th').index()+')').css('visibility','hidden');
    $(this).closest('th').find('.show').show();
    $(this).hide();
  });
  $('.show').on('click',function(){
   $('tr').find('td:eq('+$(this).closest('th').index()+')').css('visibility','visible');
    $(this).closest('th').find('.hide').show();
    $(this).hide();
  });
});
</script>

示例工作代码:-

$(document).ready(function(){
  $('.show').hide();
  $('.hide').on('click',function(){
    $('tr').find('td:eq('+$(this).closest('th').index()+')').css('visibility','hidden');
    $(this).closest('th').find('.show').show();
    $(this).hide();
  });
  $('.show').on('click',function(){
   $('tr').find('td:eq('+$(this).closest('th').index()+')').css('visibility','visible');
    $(this).closest('th').find('.hide').show();
    $(this).hide();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class='table table-bordered table-striped'>
  <thead>
    <tr>
      <th class="rotate2"><div><span>Naam</span></div></th>
      <th class="rotate2"><div><span>Functie</span></div></th>
      <th class="rotate2"><div><span>Afdeling</span></div></th>
      <th class="rotate2"><div><span>Contract</span></div></th>
      <th class="rotate2"><div><span>DID</span></div></th>
      <th class="rotate2"><div><span>DUD</span></div></th>
      <th class="rotate2"><div><span>Manager</span><br><button class="show">Show</button><button class="hide">Hide</button></div></th>
      <th class="rotate2"><div><span>Profiel</span></div></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>A</td>
      <td>A1</td>
      <td>A2</td>
      <td>A3</td>
      <td>A4</td>
      <td>A5</td>
      <td>A6</td>
      <td>A7</td>
    </tr>
    <tr>
      <td>B</td>
      <td>B1</td>
      <td>B2</td>
      <td>B3</td>
      <td>B4</td>
      <td>B5</td>
      <td>B6</td>
      <td>B7</td>
    </tr>
    <tr>
      <td>C</td>
      <td>C1</td>
      <td>C2</td>
      <td>C3</td>
      <td>C4</td>
      <td>C5</td>
      <td>C6</td>
      <td>C7</td>
    </tr>
  </tbody>
</table>

【讨论】:

  • 感谢您的回答,但我的意思是垂直隐藏列。就像隐藏“经理”列
  • 另外,我是否将 jQuery 添加到同一个文件中?还是在不同的文件中?
  • @Tim 你可以在同一个文件中添加
  • 非常感谢,但是你能告诉我把jquery代码放在哪里吗?
  • jquery库可以添加到header中。代码可以放在单独的文件中,然后将该文件包含在您的php 文件中,直到&lt;script src="add file path here"&gt;&lt;/script&gt;。如果只有一次你会使用这个代码,那么你最后可以直接在你的 php 文件中添加脚本代码。
猜你喜欢
  • 2011-12-10
  • 2011-12-24
  • 2014-12-01
  • 2012-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 2015-01-27
相关资源
最近更新 更多