Welcome Guest, Not a member yet? Register   Sign In
How can I make my select variable match exact
#1

[eluser]msank[/eluser]
Hey there,

I need some help with getting my select statement to return EXACTLY what I asked it to look for, not 'search variable' + anything else that has that in it's value; eg Magnetics is from the row 'name'. I am searching for ALL products from "Magnetics" and ONLY them. Currently I am getting all companies with "Magnetics" in their name... I have tried the following in my model:

Code:
function searchInventory($search) {
  $this->db->or_like('name', '%{$search}%');

but it returns no results.

This is my form:

Code:
<?php
  echo form_open('/products/search') . "\n";
  echo form_hidden('search', 'Magnetics') . "\n";
  echo form_submit('submit', 'Go') . "\n";
  echo form_close() . "\n";
        ?>

This is my controller for the page the form is on:

Code:
function search() {

  $search = $this->input->post('search');
  $this->load->model('search_manu_model');
  $inventory = $this->search_manu_model->searchInventory($search);
  if($inventory !== FALSE) {
   $results = $inventory->result_array();
  } else {
   $results = 'There are no results';
  }
  
  $data = array(
   'search' => $search,
   'inventory' => $results,
   'title' => 'Search Results for "' . $search . '"',
   'locations' => $this->locations,
   'readMore' => $this->readMore
  );

  $this->load->view('search_view', $data);
}
}

Any ideas?

Thanks!
#2

[eluser]Matalina[/eluser]
don't add the % to the like statement.
#3

[eluser]msank[/eluser]
I added that to try to see if it would work, after I was having issues with getting more than I was wanting (Other companies aside from Magnetics) and it would return nothing. Before I added that, it was returning more than I needed, and wanted.
#4

[eluser]Matalina[/eluser]
Code:
function searchInventory($search) {
  $this->db->or_like('name', $search);

did you try that?
#5

[eluser]msank[/eluser]
Yep... That's what I had originally and is returning more than I want.
#6

[eluser]Matalina[/eluser]
1. Why are you using a like statement if you want ONLY Magnetics? Why not a straight Where = clause?

Code:
$this->db->or_where('name', $search);

2. Use the third parameter (http://ellislab.com/codeigniter/user-gui...ecord.html)
Needed quote:
Quote:If you do not want to use the wildcard (%) you can pass to the optional third argument the option 'none'.

Code:
$this->db->or_like('name', $search,'none');


#7

[eluser]msank[/eluser]
Quote:
Code:
$this->db->or_like('name', $search,'none');

This fixed my issue!

Thanks so much for the stellar help! I really appreciate it!

#8

[eluser]Matalina[/eluser]
no problem.




Theme © iAndrew 2016 - Forum software by © MyBB