【问题标题】:Pass mutlideimensional array to codeigniter view and display it in a table将多维数组传递给 codeigniter 视图并将其显示在表格中
【发布时间】:2015-04-24 09:26:28
【问题描述】:

我正在使用 codeigniter 开发一个 Web 应用程序。我有一个在表格中列出所有投诉的视图。我正在将一系列投诉从控制器传递到视图。我的控制器数组如下所示:

Array ( [0] => Array ( [com_id] => 71 [engine_model] => 2 [assigned_to] => Sreejith KM ) 
[1] => Array ( [com_id] => 70 [engine_model] => KSERIES [assigned_to] => shaun ) 
[2] => Array ( [com_id] => 68 [engine_model] => fhg [assigned_to] => Din ) 
[3] => Array ( [com_id] => 69 [engine_model] => HA294 [assigned_to] => Don ) 
[4] => Array ( [com_id] => 64 [engine_model] => gshsh [assigned_to] => Don,Shaun ) )

我尝试使用 foreach 循环在视图中显示此数组。但它不起作用。创建了相应的行,但数据未显示在 td 中。即,创建了空白行。谁能建议我解决这个问题。提前致谢 这是我的代码:

控制器:

function re_assigncomplaint()
{
$data['returntech'] = $this->complaint_model->getTechnicians();
$data['returnitems']= $this->complaint_model->getallComplaints_reasign_test();
$data['page_title'] = $this->lang->line("reassign_complaints");
$this->load->view('commons/header', $meta);
$this->load->view('reasign', $data);
$this->load->view('commons/footer');    
}

查看:

<table id="slData" class="table table-bordered table-hover table-striped table-condensed" style="margin-bottom: 5px;">
<thead>
<tr>
<th></th>
<th><?php echo $this->lang->line("complaint_id"); ?></th>
<th><?php echo $this->lang->line("engine_model"); ?></th>
<th><?php echo $this->lang->line("assigned_to"); ?></th>
</tr>
</thead>
<tbody>

<?php
$i=0;
if(!empty($returnitems))
{
foreach ($returnitems as $return) {
?>
<tr>
<td style="text-align:center;"><input type="checkbox" value="<?php echo $return[$i]['com_id'];?>" name="idcheckbox[]" id="idcheckbox"></td>
<td style="text-align:center;"><?php echo $return[$i]['com_id'];?></td>
<td style="text-align:center;"><?php echo $return[$i]['engine_model'];?></td>  
<td style="text-align:center;"><?php echo $return[$i]['assigned_to'];?></td>
</tr>
<?php
$i++;
}
}
?>
</tbody>
</table>

型号:

public function getallComplaints_reasign_test()
{
$myQuery = "select * from app_complaint where assigned_to != 'not assigned'";
$q1 = $this->db->query($myQuery);
if($q1->num_rows() > 0)
{
foreach (($q1->result()) as $row1)
{
$compid=$row1->com_id;
$engine=$row1->engine_model;
$tch = $row1->assigned_to;
$tch1=explode(',',$tch);
$tch2=array();
foreach($tch1 as $types)
{
$techname="select technician_name from technicians where id='$types'";
$q2 = $this->db->query($techname);
if($q2->num_rows() > 0)
{
$row2 = $q2->row();
$tch2[]= $row2->technician_name;
}
}
$tech=implode(',',$tch2);
$cmpl[]=array('com_id'=>$compid,'name'=>$cust,'com_type'=>$comptype,'engine_model'=>$engine,'assigned_to'=>$tech);
}
}
return $cmpl;
}

【问题讨论】:

  • 您从哪里得到&lt;td&gt; 内的com_typecom_type,正如您在数组中显示的那样,我看不到这两个值
  • 请检查我的编辑。我已经更正了。
  • 请注意:请在代码中使用缩进(为了您自己的利益;])
  • $return[$i]['com_id']; 中删除[$i] 使其成为$return['com_id']; 和其他人。

标签: php arrays codeigniter multidimensional-array


【解决方案1】:

您同时使用了foreach 循环和$i var。你不需要$i。用作$return['com_type'] 而不是$return[$i]['com_type']

foreach ($returnitems as $return) 
{
     $return['com_type'];
}

或使用foor 循环如下:

for($i = 0; $i < count($returnitems); $i++) 
{
     $returnitems[$i]['com_type'];
}

【讨论】:

  • 非常感谢您的建议。它现在工作正常.. :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多