Solved

code igniter select query

Posted on 2016-10-03
7
38 Views
Last Modified: 2016-10-04
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();
        }
    }
0
Comment
Question by:sinanosan
  • 4
  • 3
7 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41827092
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 :)
0
 

Author Comment

by:sinanosan
ID: 41827099
thanks Marco,
I am a new with codeigniter, and not sure what u mean by modify the function accepting a parameter:
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41827105
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:sinanosan
ID: 41827121
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>
0
 

Author Comment

by:sinanosan
ID: 41827144
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.    }
0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 41827643
Why re you using base64_encode? That doesn't make sense to me: learn here about that function; it is needed
to make binary data survive transport through transport layers that are not 8-bit clean, such as mail bodies

Now, $supplierid is supposed to be an integer or a string, so you can send it withou encoding it. Try to change your link this way:
<a class="actioncol cursor" onclick="window.location.href='<?php echo site_url('products/by_supplier/' . $store_supplier[$i]['supplierid']); ?>'" title="<?php echo $store_supplier[$i]['supplier_name']; ?>"><?php echo $store_supplier[$i]['supplier_name']; ?></a> 
 

Open in new window

1
 

Author Closing Comment

by:sinanosan
ID: 41828035
Thank you Marco, url was the issue..
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP extract HTML using DOMXpath from nested tables 8 48
Calculate values in an array 18 33
Paypal 502 Error 3 70
can windows 10 wamp send mail()? 3 32
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now