0

我有:

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

我对带有复选框的动态创建表并将数据存储在数据库中没有问题:

数据以worker_id,job_id的形式存储在表中!
通常,工人可能会从事一项以上的工作,因此我从存储数据的表中创建多维数组!

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

我的问题是:

如何基于array_worked数组 创建带有选中复选框的动态表?

4

2 回答 2

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.='';

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

于 2012-09-14T05:48:30.593 回答
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;
........
}
?>
于 2012-09-14T05:51:22.937 回答