• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search multiple coloumn with multiple field

#1
[eluser]harris_priambudi[/eluser]
Hi Everybody..

I have a problem
I want to create an search module with 3 dropdown box in 3 different coloumn database

this for simple example model
Code:
$car_name = $this->input->post('car_name');
$car_colour = $this->input->post('car_id');
$car_horsepower = $this->input->post('car_horsepower');

$string_query = "SELECT * FROM car_data WHERE
(car_name = '$car_name') or
(car_colour = '$car_colour') or
(car_horsepower = '$car_horsepower')"
$query = $this->db->query($string_query);
when I try this code.. I choose
car name = x
carcolour = blue
car horsepower = 1000

but the result is not right..
the carname x who has colour red is appear..

can you help me to solve this problem? thx
(sorry for bad english)

#2
[eluser]Tominator[/eluser]
Just change 'or' to 'AND' Smile

#3
[eluser]harris_priambudi[/eluser]
[quote author="Tominator" date="1338989435"]Just change 'or' to 'AND' Smile[/quote]
I was try that...
but it make the search suggestion will not flexible

if I choose the dropbox
car_name = x
car_colour = blue
car_horsepower = null

it will no data appear..
what I want is will appear some data which
have car_name = x, car_colour = blue, and horse_power = "any"


#4
[eluser]Tominator[/eluser]
You can do that using Active Record:

Code:
$this->db->from('car_data');

if ( ! empty( $car_name ))
$this->db->where('car_name', $car_name);


if ( ! empty( $car_colour ))
$this->db->where('car_colour', $car_colour);


if ( ! empty( $car_horsepower ))
$this->db->where('car_horsepower', $car_horsepower);

$query = $this->db->get();

Or you can try to code your own, like that:

Code:
if ( ! empty( $car_name ))
$array[] = 'car_name = '. $car_name);

if ( ! empty( $car_colour ))
$array[] = 'car_colour = '. $car_colour);

if ( ! empty( $car_horsepower ))
$array[] = 'car_horsepower = '. $car_horsepower);

$where = implode(' or ', $array);

$query = "SELECT * FROM car_data WHERE ". $where;

I hope this helps Smile I'm thinking about other solutions ...

#5
[eluser]harris_priambudi[/eluser]
[quote author="Tominator" date="1338990430"]You can do that using Active Record:

Code:
$this->db->from('car_data');

if ( ! empty( $car_name ))
$this->db->where('car_name', $car_name);


if ( ! empty( $car_colour ))
$this->db->where('car_colour', $car_colour);


if ( ! empty( $car_horsepower ))
$this->db->where('car_horsepower', $car_horsepower);

$query = $this->db->get();

Or you can try to code your own, like that:

Code:
if ( ! empty( $car_name ))
$array[] = ('car_name = '. $car_name);

if ( ! empty( $car_colour ))
$array[] = ('car_colour = '. $car_colour);

if ( ! empty( $car_horsepower ))
$array[] = ('car_horsepower = '. $car_horsepower);

$where = implode(' or ', $array);

$query = "SELECT * FROM car_data WHERE ". $where;

I hope this helps Smile I'm thinking about other solutions ...[/quote]

Wow thx for the answer, I understand the logic for this problem Big Grin
but I confuse about this code
Code:
$array[]
its error when im compile..

#6
[eluser]Tominator[/eluser]
Just write at beginning:
Code:
$array = array();


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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