使用数组在数据库上显示结果

时间:2017-10-02 06:31:17

标签: php sql codeigniter

我使用数组来显示我想要的结果,它显示日期但不显示位置(网站名称)。这是我在模型中的代码

public function get($user_fname)
{
    $returnArray['sesdate'] = array();
    $returnArray['sname'] = array();

    $query = $this->db->query("SELECT sessions.session_date,sessions.site_name,CONCAT(user_fname,',',user_lname)as full_name FROM sessions 
    LEFT JOIN users using(user_fname) WHERE user_fname = ?", ['user_fname' => $user_fname]);

    foreach ($query->result() as $row)
    {                  
      $sesdate = strtotime($row->session_date);
      $sesdate = date("F j, Y",$sesdate);
      $sname = $row->site_name;
      array_push($returnArray['sesdate'],$sesdate);
      array_push($returnArray['sname'],$sname);
    }   
    return $returnArray;
}

这就是我所说的细节。

   **where I call the date**
<td>
   <?php foreach($coachact['sesdate'] as $value):?>
     <label class="addplabelcoach"><?php echo $value; ?> </label><br>    
   <?php endforeach;?>
</td>  

**where I call the site name**
 <td>
 <?php foreach($coachact['sname'] as $value):?>
  <label class="addplabelcoach"><?php echo $value; ?> </label><br>
  <?php endforeach;?>
 </td> 

为什么显示日期正常但不显示网站名称?

我的控制器

public function editview($user_fname){
    $returnData = array();

$this->load->model('User_model');
$this->User_model->checkIfLoggedIn();

$this->load->view('incf/header');
$this->load->view('schedulef/sced_tools');

$this->load->model('Schedule_model');
$coachact = $this->Schedule_model->get($user_fname);
 $message = $this->session->flashdata('message');
$this->load->view('schedulef/Schedule_manage',[
                'coachact' => $coachact,'user_fname'=>$user_fname,'message' => $message

            ]);

}

1 个答案:

答案 0 :(得分:0)

快速简单地返回codeigniter中的数组和对象正在使用

row_array()返回1个数组

result_array()返回多维数组

对于我读过的代码。您是否尝试仅返回sesdatesname

所以如果是的话

public function get($user_fname)
{
    $new_array = array();

    $query = $this->db->query("SELECT *,CONCAT(user_fname,',',user_lname)as full_name FROM sessions 
    LEFT JOIN users using(user_fname) WHERE user_fname = ?", ['user_fname' => $user_fname]);

    foreach ($query->result() as $key => $row)
    {                  
      $sesdate = strtotime($row->session_date);
      $sesdate = date("F j, Y",$sesdate);
      $sname = $row->site_name;

      $returnArray['sesdate'][$key] = $sesdate;
      $returnArray['sname'][$key] = $sname;
    }   
    return $returnArray;
}