Php复选框从数据库中检索数据

时间:2012-09-14 05:28:25

标签: php checkbox

我有:

$array_worker['$worker_id']=$worker_name;  
$array_job['$job_id']=$job_name;  

我的动态创建表没有问题,复选框和数据存储在数据库中:

数据作为worker_id,job_id!
存储在表中 通常,worker可能会工作多个作业,所以我从表中创建了多维数组,其中存储了数据!

$array_worded['$worker_id'][]=$job_id;  

我的问题是:

如何使用基于 array_worked 数组的选中复选框创建动态表格?

2 个答案:

答案 0 :(得分:0)

$table='';

  foreach($array_worker as $key=>$value){
    $table.=''.$value.''; // worker name
      $worker_id = // get worker id from $array_worker

      foreach($array_job as $key_job=>$val_job)
      {
        $job_id = // get job id from $array_job 

        $checked = false;
        foreach( $array_worked[$worker_id] as $key_worked => $val_worked )
        {
          if( $job_id == $val_worked ) // $val_worked contains $job_id
          {
            $checked = true;
            break;
          }
        }
        $table.='<input type="checkbox"' . ( $checked ? ' checked="checked"' : '') . '/>'.$val_job.''; // all jobs from database    
      }
    $table.='';           
  }
$table.='';

我可能在语法上犯了一些错误,但代码演示了基本原则。

答案 1 :(得分:0)

这很简单:

<input type="checkbox" name="formWheelchair"  
<?php
$DATABASE-VALUE = $array_worded['$worker_id'][] = $job_id; // OR WHAT EVER
switch ($DATABASE-VALUE) {
    case 0:
        echo checked />"
        break;
........
}
?>