【问题标题】:PHP show data in checkboxPHP 在复选框中显示数据
【发布时间】:2017-01-24 21:34:19
【问题描述】:

我如何将笑话类别表中的复选框显示为选中,以及那些不显示但未选中的复选框。这是我目前拥有的:

<div class="control-group">
        <?php 
        $sql = 'SELECT jokecategory.joke_id, jokecategory.category_id, category.name 
        FROM jokecategory
        INNER JOIN category ON category.id = jokecategory.category_id
        WHERE joke_id= :joke_id';
        $stmt = $dbConnection->prepare($sql);
        $stmt->bindValue(':joke_id', $id);
        $stmt->execute();

        ?>

        <fieldset>
        <legend class="control-label">Categories:</legend>
        <?php foreach ($stmt as $row) { ?>
            <div class="controls">
                <label for="category<?php echo($row['category_id']);?>">
                <input type="checkbox" id="category<?php echo $row['category_id']; ?>" name="checkbox[]" value="<?php echo $row['category_id']; ?>" checked>
                <?php echo($row['name']); ?></label>
            </div>
        <?php } ?>

with(类别表中的数据):

<?php $sql = 'SELECT id, name FROM category'; 
                    foreach ($dbConnection->query($sql) as $data) { ?>
                    <div class="controls">      
                        <label for="category<?php echo($data['id']);?>">
                        <input type="checkbox" name="categories[]" id="category<?php echo($data['id']); ?>" value="<?php echo($data['id']); 



                        ?>">
                        <?php echo($data['name']); ?></label>
                    </div>
                    <?php } ?>

            </fieldset>
        </div>

目前它分为两个部分,因为我不确定如何将输出数据显示到一个合并组中。所以它是这样的:

Joke id: xxxxx
joke_text: xxxxxx
author_name: xxxxx

categories 
1: [x]
2: [x]
3: [ ]
4: [ ]

【问题讨论】:

  • 首先,您是否从查询中获取数据?获取后您是否尝试过print_r($data)?其次,$joke_id and other variables 将始终具有查询的最后一个数据,因为这些变量不是数组。第三,你的foreach调用$data['category_id'],对吗?
  • 我接收到所有其他字段的数据。这只是我不确定如何组合的循环元素。
  • 使用 'print_r($data)' 和 'joke.id = 28',它在 jokecategory 表下有 4x 记录。但是,'$data['category_id'] 仅显示 jokecategory 表中的 1x 数据项。
  • 你能分享$data数组吗? ( print_r() )
  • $data 输出:数组([id] => 28 [joke_text] => jhohio [joke_date] => 2017-01-23 12:41:11 [name] => user [email] => user@email.com ) 数组 ( [joke_id] => 28 [category_id] => 4 [name] => 走吧)

标签: php checkbox foreach


【解决方案1】:

我不确定,但是 - $data["category_id"] 真的是一个数组吗?如果是这样,您可以像这样使用该数组中的密钥对:

<?php foreach ($data['category_id'] as $id => $checkbox) { ?>
  <input type="checkbox" id="<?php echo $id; ?>" name="checkbox[]" />
<?php } ?>

【讨论】:

  • 我如何将 $data["category_id"] 变成一个数组?
【解决方案2】:

可能如下所示,检查您的 sql 查询并添加类别名称以获取和显示,例如。 category_name 下面使用 并进行如下更改

<fieldset>
        <legend class="control-label">Categories:</legend>
        <?php foreach ($data as $checkbox) { ?>
            <div class="controls">      
                <label for="category<?php echo($checkbox['category_id']); ?>">
                    <input type="checkbox" name="categories[]" id="chk_category<?php echo($checkbox['category_id']); ?>" value="<?php echo($checkbox['category_id']); ?>">
                    <?php echo($checkbox['category_name']); ?></label>
            </div>
        <?php } ?>
    </fieldset>

【讨论】:

    【解决方案3】:

    我不确定我是否完全理解了这个问题,但你试过了吗

    foreach ($data['category_id'] as $id =&gt; $checkbox)

    【讨论】:

      猜你喜欢
      • 2016-09-05
      • 2016-02-23
      • 2019-11-10
      • 2016-01-29
      • 1970-01-01
      • 2012-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多