Forums

How to modify the search engine?

CreamDealer 22 Sep, 2009
Hello to everyone!

I use this code for my search engine:
<?php
$search_array = array('usluga', 'branza', 'price', 'doradca');
$where = array();
foreach ( $search_array as $search ) {
  $value = JRequest::getVar($search, '' , 'post');
  if ( $value ) {
    $where[] = " $search LIKE '%$value%' ";
  }
}
if ( !empty($where) ) {
  echo " WHERE ".implode(' AND ', $where);
}?>

I don't know how to change it if I want to search 'price' between two values from two input boxes? You know, I want to find all records with prices between 100$ and 500$...

I'm not a webmaster.

Please help me.
Thank You in advance!
GreyHead 22 Sep, 2009
Hi Creamdealer,

Add these extra line to the where box
$min_value = JRequest::getVar('min_value, '' , 'post');
if ( $min_value ) {
    $where[] = " `value` > '$min_value'";
}
$max_value = JRequest::getVar('max_value, '' , 'post');
if ( $max_value ) {
    $where[] = " `value` <= '$max_value'";
}
You'll need to change min_value, max_value & value to match whatever the inputs and columns are called in your form and table.

Bob
CreamDealer 22 Sep, 2009
Thank You very much Bob, but I still have some problems. I made smth like this:
<?php
$search_array = array('usluga', 'branza', 'kwota', 'doradca', );
$where = array();
$min_value = JRequest::getVar('kwota', '' , 'post');
if ( $min_value ) {
    $where[] = " 'min' > '$min_value'";
}
$max_value = JRequest::getVar('kwota', '' , 'post');
if ( $max_value ) {
    $where[] = " 'max' <= '$max_value'";
}
foreach ( $search_array as $search ) {
  $value = JRequest::getVar($search, '' , 'post');
  if ( $value ) {
    $where[] = " $search LIKE '%$value%' ";
  }
}
if ( !empty($where) ) {
  echo " WHERE ".implode(' AND ', $where);
}?>

But it doesn't work. When I use search, the values "kwota" are the same...
GreyHead 22 Sep, 2009
Hi CreamDealer,

You have straight quotes '' round max and min, in my version they are backticks `max` `min`

You may need t check the logic too.

Bob
CreamDealer 22 Sep, 2009
OK, Thank You VERY VERY MUCH!

Now it sorts but it sorts alphabeticaly. So if I search beetween 1500 and 3000 it shows me 199 too...

What can be wrong?
GreyHead 22 Sep, 2009
Hi CreamDealer,

Are the values all integers?

Bob
This topic is locked and no more replies can be posted.