Showing inactive products using where('status','active');

#1
[eluser]shinokada[/eluser]
The following code supposed to search and display products which has status is active.
However it shows all the products.

Could anyone tell me what I am missing here?

<b>database</b>
Code:
...
...
CREATE TABLE IF NOT EXISTS `products` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `shortdesc` varchar(255) NOT NULL,
  `longdesc` text NOT NULL,
  `thumbnail` varchar(255) NOT NULL,
  `image` varchar(255) NOT NULL,
  `grouping` varchar(16) DEFAULT NULL,
  `status` enum('active','inactive') NOT NULL,
  `category_id` int(11) NOT NULL,
  `featured` enum('true','false') NOT NULL,
  `price` float(4,2) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;



INSERT INTO `products` (`id`, `name`, `shortdesc`, `longdesc`, `thumbnail`, `image`, `grouping`, `status`, `category_id`, `featured`, `price`) VALUES
(1, 'Game 1', 'This is a very good game.', 'What a product! You''ll love the way your kids will play with this game all day long. It''s terrific!', 'images/dummy-thumb.jpg', 'images/dummy-main.jpg', 'fun', 'inactive', 6, '', 19.95),
(2, 'Game 2', 'This is a very good game.', 'What a product! You''ll love the way your kids will play with this game all day long. It''s terrific!', 'images/dummy-thumb.jpg', 'images/dummy-main.jpg', 'fun', 'inactive', 6, '', 19.95),
...
...



<b>models/mproducts.php</b>

Code:
function search($term){
    $data = array();
    $this->db->select('id,name,shortdesc,thumbnail');
    $this->db->like('name',db_clean($term));
    $this->db->or_like('shortdesc',db_clean($term));
    $this->db->or_like('longdesc',db_clean($term));
    $this->db->where('status','active');
    $this->db->orderby('name','asc');
    $this->db->limit(50);
    $Q = $this->db->get('products');
  
    if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[] = $row;
       }
    }
    $Q->free_result();    
    return $data;
}

#2
[eluser]czetsuya[/eluser]
Hi,

This is because of the active class functions that you have used:
Code:
$this->db->like('name',db_clean($term));
$this->db->or_like('shortdesc',db_clean($term));
$this->db->or_like('longdesc',db_clean($term));
$this->db->where('status','active');

//that statements produced
//WHERE name LIKE '%$term%' OR shortdesc LIKE '%$term%' OR longdesc LIKE '%$term%' AND status='active'

Note the OR.

Try this:
Code:
$this->db->where("(name LIKE '%$term%' OR shortdesc LIKE '%$term%' OR longdesc LIKE '%$term%') AND status='active'")

Take note of the parenthesis ( )

Regards,
czetsuya

#3
[eluser]shinokada[/eluser]
Thanks it fixed.
And I understand it.

Thanks again.
Regards.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.