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_product s'] = $this->productss->get_prod ucts_by_su pplier_id( );
$this->data['store_supplie rs'] = $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('product s/by_suppl ier', $this->data);
}
----------- Model page-----
public function get_products_by_supplier_i d()
{
$this->db->select('*');
$this->db->from('store_pro ducts');
$this->db->join('store_sup pliers', 'store_products.supplierid = store_suppliers.supplierid ','left');
$this->db->join('tax_setti ngs', 'store_products.taxid = tax_settings.taxid','left' );
$this->db->where('store_su ppliers.su pplierid', $supplieri d);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
return $query->result_array();
}
else
{
return array();
}
}
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_product
$this->data['store_supplie
$this->data['tax_settings'
$this->data['store_taxs'] = $this->productss->get_join
$this->load->view('product
}
----------- Model page-----
public function get_products_by_supplier_i
{
$this->db->select('*');
$this->db->from('store_pro
$this->db->join('store_sup
$this->db->join('tax_setti
$this->db->where('store_su
$query = $this->db->get();
if ($query->num_rows() > 0)
{
return $query->result_array();
}
else
{
return array();
}
}
ASKER
thanks Marco,
I am a new with codeigniter, and not sure what u mean by modify the function accepting a parameter:
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_i d(). In your originl code it is exactly like this
'$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
$this->data['store_product s'] = $this->productss->get_prod ucts_by_su pplier_id( '1');
So you have just to add $supplierid to the function declaration:
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
public function get_products_by_supplier_id()
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)
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_product
So you have just to add $supplierid to the function declaration:
public function get_products_by_supplier_id($supplierid)
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
ASKER
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.h ref='<?php echo site_url('products/by_supp lier/' . base64_encode($store_suppl ier[$i]['s upplierid' ])); ?>'" title="<?php echo $store_supplier[$i]['suppl ier_name'] ; ?>"><?php echo $store_supplier[$i]['suppl ier_name'] ; ?></a>
this is the url I am passing the query, is it wrong?
<a class="actioncol cursor" onclick="window.location.h
ASKER
Query returns empty.
So I have this link to query
than my controller page code is
than my Model page code is
So I have this link to query
- <a class="actioncol cursor" onclick="window.location.h
ref='<?php echo site_url('products/by_supp lier/' . base64_encode($store_suppl ier[$i]['s upplierid' ])); ?>'" title="<?php echo $store_supplier[$i]['suppl ier_name'] ; ?>"><?php echo $store_supplier[$i]['suppl ier_name'] ; ?></a>
than my controller page code is
- //load suppliers products by supplierid
- public function by_supplier($supplierid)
- {
- //Addingg Setting Result to variable
- $this->data['store_product
s'] = $this->productss->get_prod ucts_by_su pplier_id( $supplieri d); - $this->data['store_supplie
rs'] = $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('product
s/by_suppl ier', $this->data); - }
than my Model page code is
- public function get_products_by_supplier_i
d($supplie rid) - {
- $this->db->select('*');
- $this->db->from('store_pro
ducts'); - $this->db->join('store_sup
pliers', 'store_products.supplierid = store_suppliers.supplierid ','left'); - $this->db->join('tax_setti
ngs', 'store_products.taxid = tax_settings.taxid','left' ); - $this->db->where('store_su
ppliers.su pplierid', $supplieri d); - $query = $this->db->get();
- if ($query->num_rows() > 0)
- {
- return $query->result_array();
- }
- else
- {
- return array();
- }
- }
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you Marco, url was the issue..
In your code you have this:
Open in new window
but $supplierid is not defined. You should first modify the function accepting a parameter:Open in new window
Then you have to call the function passing the corect id you want to get from the database:Open in new window
The '1' is just an example, of course :)