Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

code igniter select query

Posted on 2016-10-03
7
53 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

809 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