tuchawat tuchawat - 1 month ago 8
PHP Question

Codeigniter - If I have fetch row from 3 query, How to send from model to view?

I am using Codeigniter 3 and I have multiple queries look like below

<?php
class Address_model extends CI_Model
{
public function fetchAddress()
{


$this->db->where('username', $this->session->userdata('username'));
$query = $this->db->get('member');
$row = $query->row();

$sql2 = "SELECT * FROM member_shipping_info WHERE member_id = ?";
$query2 = $this->db->query($sql2, $row->member_id);
$row2 = $query2->row();

$data['get_firstname'] = $row2->firstname;
$data['get_lastname'] = $row2->lastname;
$data['get_gender'] = $row2->gender;
$data['get_address'] = $row2->address;
$data['get_district'] = $row2->district;
$data['get_amphur'] = $row2->amphur;
$data['get_province'] = $row2->province;
$data['get_zipcode'] = $row2->zipcode;
$data['get_email'] = $row->email;
$data['get_phone'] = $row2->phone;


$this->load->view('shopping/billing_view', $data);


The problem is it
$row->email
it come from
$query1
not 2 how should I send from model to controller and from controller to view? I put in model because I have to use these queries many times in my project.

Answer

You need to refactor your code. First your insertAddress method name is misleading because it does not insert any address to database. Second your doing too much thing in one method.

<?php
class Member_model extends CI_Model
{
    public function getMemberData($username) {
        $this->db->where('username', $username);
        $query = $this->db->get('member');
        return $query->row();
    }

    public function getShippingInfo($member_id)
    {
         $sql2 = "SELECT * FROM member_shipping_info WHERE member_id = ?";
         $query2 = $this->db->query($sql2, $member_id);
         return $query2->row();
    }
}

Then inside your controller:

<?php
    ...
     $username = $this->session->userdata('username');
     $memberData = $memberModel->getMemberData($username);
     $shippingInfo = $memberModel->getShippingInfo($memberData->member_id);

     $data['get_firstname'] = $shippingInfo->firstname;
     $data['get_lastname'] = $shippingInfo->lastname;
     ...
     $data['get_email'] = $memberData->email;
     $data['get_phone'] = $shippingInfo->phone;

     $this->load->view('shopping/billing_view', $data);

Other alternative is to use SQL join to combine both queries in model into one query.

<?php
class Member_model extends CI_Model
{
    public function getMemberShippingInfo($username)
    {
         $sql2 = "SELECT member.*, ".
                 "member_shipping_info.* ".
                 "FROM member ".
                 "INNER JOIN member_shipping_info ".
                 "ON member.member_id = member_shipping_info.member_id ".
                 "WHERE member.username = ?";

         $query2 = $this->db->query($sql2, $username);
         return $query2->row();
    }
}

In your controller:

<?php
    ...
     $username = $this->session->userdata('username');
     $memberData = $memberModel->getMemberShippingInfo($username);

     $data['get_firstname'] = $memberData->firstname;
     $data['get_lastname'] = $memberData->lastname;
     ... 
     $data['get_email'] = $memberData->email;
     $data['get_phone'] = $memberData->phone;