Solved

code igniter select query

Posted on 2016-10-03
7
35 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 30

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 30

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 30

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

707 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

15 Experts available now in Live!

Get 1:1 Help Now