Link to home
Start Free TrialLog in
Avatar of sinanosan
sinanosan

asked on

code igniter select query

Hi There,
I am trying to select products by supplierid but seems every product is selected from database, cant figure it out why, any help please. thx

-----Controller page-----
public function by_supplier()
    {
        //Addingg Setting Result to variable
        $this->data['store_products'] = $this->productss->get_products_by_supplier_id();
            $this->data['store_suppliers'] = $this->productss->get_join_suppliers();
            $this->data['tax_settings'] = $this->productss->get_all_taxs();
            $this->data['store_taxs'] = $this->productss->get_join_taxs();
          
        $this->load->view('products/by_supplier', $this->data);
    }
----------- Model page-----

public function get_products_by_supplier_id()
    {
        $this->db->select('*');
        $this->db->from('store_products');
            $this->db->join('store_suppliers', 'store_products.supplierid = store_suppliers.supplierid','left');
            $this->db->join('tax_settings', 'store_products.taxid = tax_settings.taxid','left');
            $this->db->where('store_suppliers.supplierid',$supplierid);
       
       
            $query =  $this->db->get();
        if ($query->num_rows() > 0)
        {
            return $query->result_array();
        }
        else
        {
                return array();
        }
    }
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Please, when you post code use the code tags you find in the toolbar.

In your code you have this:
 $this->db->where('store_suppliers.supplierid',$supplierid);

Open in new window

but $supplierid is not defined. You should first modify the function accepting a parameter:
public function get_products_by_supplier_id($supplierid)
    {
        $this->db->select('*');
        $this->db->from('store_products');
            $this->db->join('store_suppliers', 'store_products.supplierid = store_suppliers.supplierid','left');
            $this->db->join('tax_settings', 'store_products.taxid = tax_settings.taxid','left');
            $this->db->where('store_suppliers.supplierid',$supplierid);
       
       
            $query =  $this->db->get();
        if ($query->num_rows() > 0)
        {
            return $query->result_array();
        }
        else
        {
                return array();
        }
    } 

Open in new window

Then you have to call the function passing the corect id you want to get from the database:
$this->data['store_products'] = $this->productss->get_products_by_supplier_id('1');

Open in new window

The '1' is just an example, of course :)
Avatar of sinanosan
sinanosan

ASKER

thanks Marco,
I am a new with codeigniter, and not sure what u mean by modify the function accepting a parameter:
Look at your function get_products_by_supplier_id(). In your originl code it is exactly like this
public function get_products_by_supplier_id()

Open in new window

It doesn't accept any parameter: the parenthesis are empty. But in the code you use a variable called
'$supplierid' to filter your database records with  WHERE clause: where $supplierid comes from? It just doesn't exist so it cn't be used to filter records. This means you have to specify what is the $supplierid you want and tell it to the function. Look the value between the parenthesis: that is our parameter
public function get_products_by_supplier_id($supplierid)

Open in new window

So, when you call the function you hve to pass the correct parmetere, that is you ahve to tell what is the id of the record you want (in my example is '1'):
$this->data['store_products'] = $this->productss->get_products_by_supplier_id('1');

So you have just to add $supplierid to the function declaration:
public function get_products_by_supplier_id($supplierid)

Open in new window


and specify the value of the id when you call the function.

But this is not about CodeIgniter, it is about general programming. About CodeIgniter I strongly suggest to go through the online manual, a great and effective resource to learn the framework
thanks Marco, i'll go through and get more info on codeigniter and programing. after changing the code as u suggested now the Query is returning empty.
this is the url I am passing the query, is it wrong?
<a class="actioncol cursor" onclick="window.location.href='<?php echo site_url('products/by_supplier/' . base64_encode($store_supplier[$i]['supplierid'])); ?>'" title="<?php echo $store_supplier[$i]['supplier_name']; ?>"><?php echo $store_supplier[$i]['supplier_name']; ?></a>
Query returns empty.

 So I have this link to query

  1. <a class="actioncol cursor" onclick="window.location.href='<?php echo site_url('products/by_supplier/' . base64_encode($store_supplier[$i]['supplierid'])); ?>'" title="<?php echo $store_supplier[$i]['supplier_name']; ?>"><?php echo $store_supplier[$i]['supplier_name']; ?></a>

than my controller page code is

  1. //load suppliers products by supplierid
  2.    public function by_supplier($supplierid)
  3.    {
  4.        //Addingg Setting Result to variable
  5.            $this->data['store_products'] = $this->productss->get_products_by_supplier_id($supplierid);
  6.            $this->data['store_suppliers'] = $this->productss->get_join_suppliers();
  7.            $this->data['tax_settings'] = $this->productss->get_all_taxs();
  8.            $this->data['store_taxs'] = $this->productss->get_join_taxs();
  9.        $this->load->view('products/by_supplier', $this->data);
  10.    }

than my Model page code is
  1. public function get_products_by_supplier_id($supplierid)
  2.    {
  3.        $this->db->select('*');
  4.        $this->db->from('store_products');
  5.        $this->db->join('store_suppliers', 'store_products.supplierid = store_suppliers.supplierid','left');
  6.        $this->db->join('tax_settings', 'store_products.taxid = tax_settings.taxid','left');
  7.            $this->db->where('store_suppliers.supplierid',$supplierid);
  8.            $query =  $this->db->get();
  9.        if ($query->num_rows() > 0)
  10.        {
  11.            return $query->result_array();
  12.        }
  13.        else
  14.        {
  15.                return array();
  16.        }
  17.    }
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you Marco, url was the issue..