在mysqli php中显示同一列的多个值

时间:2018-04-10 10:33:32

标签: php mysqli

我想在复选框中显示值。我在数据库中有多个值,如上图所示。请看一看。我希望不同复选框中的值不在同一个复选框中

How its looking now[![My table.[![Expected result] 1] 2

我正在尝试的代码

<div class="list-group">
            <h3>Name</h3>
            <?php 
                $query = "select distinct(name) from info_user where user_status = '1'";  
                $rs = mysqli_query($con,$query) or die("Error : ".mysqli_error());
                while($color_data = mysqli_fetch_assoc($rs)){

            ?>
                <a href="javascript:void(0);" class="list-group-item"> 
                <input type="checkbox" class="item_filter colour" value="<?php echo $color_data['name']; ?>"  >
                &nbsp;&nbsp; <?php echo $color_data['name']; ?></a>
            <?php } ?>  
            </div>

我自己尝试了什么

  <div class="list-group">
            <h3>Name</h3>
            <?php 
                $column = array();
                $query = "select name from info_user where user_status = '1'";  
                $rs = mysqli_query($con,$query) or die("Error : ".mysqli_error());
                while($color_data = mysqli_fetch_assoc($rs)){
                    $column[] = $color_data['name'];


            ?>
                <a href="javascript:void(0);" class="list-group-item"> 
                <input type="checkbox" class="item_filter colour" value="<?php foreach($column as $value)echo $value['name']; ?>"  >
                &nbsp;&nbsp;
                 <?php foreach($column as $value)echo $value['name']; ?></a>
            <?php } ?>  
            </div> 

尝试代码后出现此错误

Getting error after my putting new code in page

2 个答案:

答案 0 :(得分:1)

您可以尝试这种方式,更新您的代码

<div class="list-group">
    <h3>Name</h3>
    <?php
    $column = array();
    $query = "select distinct(name) from info_user where user_status = '1'";
    $rs = mysqli_query($con, $query) or die("Error : " . mysqli_error());
    while ($color_data = mysqli_fetch_assoc($rs)) {
        $column = array_merge($column, explode(',',$color_data['name']));
    }
    // to remove repeated names  
    $column = array_filter($column);
    ?>
    <a href="javascript:void(0);" class="list-group-item"> 
        <input type="checkbox" class="item_filter colour" value="<?php
               foreach ($column as $value) {
                   echo $value;
                   ?>">
            &nbsp;&nbsp;
            <?php
            echo $value;
        }
        ?>
    </a>
</div> 

答案 1 :(得分:1)

我认为这就是你所需要的。

merge