Codeigniter - 如何在我的视图页面中按ID来预测和回显项目名称

时间:2017-11-14 05:07:45

标签: php codeigniter

$data['posts'] = $this->whiteboard_model->u_getTSrowsindex(array('limit'=>$this->perPage));
$data['postsexp'] = $this->whiteboard_model->u_getTSrowsexp(array('limit'=>$this->perPage));
//print_r($data['postsexp']); exit;
foreach($data['posts']  as $post)
{
    $project_id =$post['project_name'];
    $data['projectName'] = $this->whiteboard_model->get_project_name($project_id);
    print_r($data['projectName']);
}
//This is my controller

public function get_project_name($project_id) { 
    $this->db->select('project_name');
    $this->db->from('project_list');
    $this->db->where('project_id',$project_id);
    $query = $this->db->get();
    //echo $this->db->last_query(); 
    //return fetched data
    return ($query->num_rows() > 0)?$query->result_array():FALSE;
}
//my model

<?php if(!empty($posts)): foreach($posts as $post):                     

//print_r($post);?>


<tr>
    <td>  <?php echo $post['advisor']; ?></td>
    <td>  <?php echo $post['spentdate']; ?></td>
    <td><?php echo $post['project_name']; ?>  
    <?php if(!empty($projectName)){
        foreach($projectName as $prj_name){                     //print_r($projectName); 
            echo $prj_name['project_name']; 
        }
    }    ?>
    </td>
    <td>  <?php echo $post['activity']; ?></td>
    <td>  <?php echo $post['billtype']; ?></td>
    <td>  <?php echo $post['timespent']; ?></td>
    <td>  <?php if($post['status']  == 1){?> 
    <b style = "color:blue;">Pending</b>
    <?php }else if($post['status']  == 2){?>
    <b style = "color:green;">Approved</b>
    <?php }else{?>                                      <b style = "color:red;">Rejected</b>
    <?php } ?>                           </td>
</tr>
<?php endforeach; else: ?>`enter code here`

//my view file

最后,我没有从projectName数组中获取正确的值..仅提供一个名称

1 个答案:

答案 0 :(得分:0)

您似乎正在使用project_id

获取数据
$this->db->select('project_name');
$this->db->from('project_list');
$this->db->where('project_id',$project_id);
$query = $this->db->get();

在这里,您可能只获得一个项目的详细信息其中project_id = $ project_id

注释掉该行并查看是否获得了所有项目名称。另外,使用print_r($query->result());验证您的数据。

这样看起来像是:

$this->db->select('project_name');
$this->db->from('project_list');
//$this->db->where('project_id',$project_id);
$query = $this->db->get();
print_r($query->result());

您可以推荐更多here