Kate Kate - 3 years ago 92
PHP Question

Displaying the results on database using array

I'm using array to display the result I want to, It displays the date but not the location(site name). Here is my code in model

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;
}


And this is where I call the details.

**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>


Why is it that it displays the date fine but not the site name?

My controller

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

]);


}

Answer Source

quick simple to return array and object in codeigniter is using

row_array() to return 1 array

result_array() to return multidimensional array

For your code that i have read. Do you trying to return only sesdate and sname ?

so if yes

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;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download