Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

code igniter select query

Posted on 2016-10-03
7
Medium Priority
?
95 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
[X]
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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

722 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