【问题标题】:Checkbox values to be displayed in the same page要在同一页面中显示的复选框值
【发布时间】:2015-03-07 08:53:20
【问题描述】:

我有一个包含多行多列的页面。

因此,当用户登录时,如果他在后端有任何条目,则应该在前端显示。显示,如果后端表中的值为1,如果后端表中为0,则应取消选中。

                        {$i=0}
                        {foreach from=$last7days item=date}
                        <tr>

                             <td><b>&nbsp;&nbsp;{$date}</b><input type='hidden' name='date[]' value ='{$date}'></td>
                             <td><input type='checkbox' name='BREAKFAST[]' value='1' ><input type='hidden' name='BREAKFAST[]' value='0'></td>
                              <td><input type='checkbox' name= 'LUNCH[]' value='1'><input type='hidden' name= 'LUNCH[]' value='0'></td>
                             <td><input type='checkbox' name= 'EVENING[]' value='1'><input type='hidden' name= 'EVENING[]' value='0'></td>
                             <td><input type='checkbox' name= 'DINNER[]' value='1'><input type='hidden' name= 'DINNER[]' value='0'></td>
                              <td><input type='checkbox' name= 'MIDNIGHT[]' value='1'><input type='hidden' name= 'MIDNIGHT[]' value='0'></td>
                        </tr>
                        {/foreach}

对于 php :-

   $show_details =   mysql_query("select id,user,date,BREAKFAST,LUNCH,EVENING,DINNER,MIDNIGHT,timeUpdate from phpgroupware_new.foodPlan
                        user='$name'");

表:-

mysql> 从 foodPlan 中选择 *; +----+------------+------------+------------+------ -+---------+--------+----------+------------------ ---+ |编号 |用户 |日期 |早餐 |午餐 |晚上 |晚餐 |午夜 |时间更新 | +----+------------+------------+------------+------ -+---------+--------+----------+------------------ ---+ | 1 |莫妮莎.md | 2015-03-07 | 1 | 1 | 1 | 1 | 1 | 2015-03-07 11:43:45 | | 2 |莫妮莎.md | 2015-03-08 | 0 | 0 | 0 | 0 | 0 | 2015-03-07 11:43:45 | | 3 |莫妮莎.md | 2015-03-09 | 0 | 0 | 0 | 0 | 0 | 2015-03-07 11:43:45 | | 4 |莫妮莎.md | 2015-03-10 | 0 | 0 | 0 | 0 | 0 | 2015-03-07 11:43:45 | | 5 |莫妮莎.md | 2015-03-11 | 0 | 0 | 0 | 0 | 0 | 2015-03-07 11:43:45 | | 6 |莫妮莎.md | 2015-03-12 | 0 | 0 | 0 | 0 | 0 | 2015-03-07 11:43:45 | | 7 |莫妮莎.md | 2015-03-13 | 0 | 0 | 0 | 0 | 0 | 2015-03-07 11:43:45 | | 8 |管理员 | 2015-03-07 | 1 | 1 | 0 | 0 | 0 | 2015-03-07 12:35:57 | | 9 |管理员 | 2015-03-08 | 1 | 1 | 0 | 0 | 0 | 2015-03-07 12:35:57 | | 10 |管理员 | 2015-03-09 | 1 | 1 | 0 | 0 | 0 | 2015-03-07 12:35:57 | | 11 |管理员 | 2015-03-10 | 1 | 1 | 0 | 0 | 0 | 2015-03-07 12:35:57 | | 12 |管理员 | 2015-03-11 | 1 | 1 | 0 | 0 | 0 | 2015-03-07 12:35:57 | | 13 |管理员 | 2015-03-12 | 1 | 1 | 0 | 0 | 0 | 2015-03-07 12:35:57 | | 14 |管理员 | 2015-03-13 | 1 | 1 | 0 | 0 | 0 | 2015-03-07 12:35:57 | +----+------------+------------+------------+------ -+---------+--------+----------+------------------ ---+ 14 行一组(0.00 秒)

【问题讨论】:

  • 有什么问题?这里需要更多细节。
  • 无法添加图片,因为缺乏声誉。需要知道,如何显示在前端选中的复选框。
  • 您必须动态打印输入。在任何情况下您都不应该使用 mysql_*,因为 它不再安全 但是,假设您将使用 mysqli_* 或 PDO,您可以轻松地进行查询,遍历结果(在您的情况,您可以在查询时直接执行此操作)并根据所选值解析每个
  • 您的后端表设计不佳。它需要存储用户、日期和餐点,所以 3 列(如果添加 last_modified 列,则为 4 列)

标签: php mysql smarty


【解决方案1】:

我认为您应该删除定义不同值 0 的其他隐藏输入,并仅包含值为 1 的第一个输入。也许这可以帮助您开始,除非我误解了您的问题

if (isset($_POST['BREAKFAST'])
{
$breakfast = $_POST['BREAKFAST'];

foreach($breakfast as $item) 
if($item == 1) //that means box is checked
// what do you want to do
else
// what you want to do if box is not checked
}

您实际上不需要为您的输入使用数组,只需一个变量就可以了

【讨论】:

    【解决方案2】:

    试试这个,

    <input type="checkbox" <?php
    echo($breakfast == 1)? 'checked="checked"' : ''?> name="BREAKFAST[]" value="<?php echo $breakfast; ?>" > 
    

    $breakfast 是来自 for 循环的数据库中的值。

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签