Page 1 of 1

Setting up a search

Posted: Mon Aug 07, 2006 5:06 pm
by davidppppppppppp
I want to set up a search criteria.

What I have is one table. One of the fields is named number. In it are numbers ranging from 1 to 66.

I would like to create a search form that will allow a visitor to select which section of the database to search. Here are the options:

all - number 1 - 66 (select * FROM table WHERE text LIKE '%$keyword%')
old - number 1- 39 (select * FROM table WHERE number <40 AND text LIKE '%$keyword%')
new - number 40 - 66 (select * FROM table WHERE number >39 AND text LIKE '%$keyword%')

What I have so far is this:

Code: Select all

<form name="form" action="search.php" method="get">
Search: 
<input type="radio" name="pull" value="all"> &nbsp;All &nbsp;&nbsp;
<input type="radio" name="pull" value="old"> &nbsp;Old &nbsp;&nbsp;
<input type="radio" name="pull" value="new"> &nbsp;New

<br />
<br />

Keyword: <input type="text" name="q" />
  <input type="submit" name="Submit" value="Search" />
</form>

<?php

  // Get the search variable from URL
  $var = @$_GET['q'] ;
  $keyword = trim($var); //trim whitespace from the stored variable

How would I set the variables from the radio inputs to get me the first part of the WHERE clause?

Thanks
David P.

Posted: Mon Aug 07, 2006 5:22 pm
by blackbeard
Try this, I'm assuming a mysql database:

Code: Select all

$keyword = trim($_GET['q']);
$keyword = "'%".$keyword."%'";  //  Need to surround the keword in '%      %'

if (isset($_GET['pull'])) {

  switch ($_GET['pull']) {
     case "all":
     default:
       $searchString = "text LIKE $keyword";
       break;
     case "old":
        $searchString = "number < 40 AND text LIKE $keyword";
        break;
     case "new":
        $searchString = "number >39 AND text LIKE $keyword";
        break;
}

//  Make sure to connect to the database

$query = mysql_query ("SELECT * FROM TABLE WHERE $searchString");
$result = mysql_fetch_assoc($query);

Posted: Mon Aug 07, 2006 5:25 pm
by Chris Corbyn
Is your P key broken?

Posted: Mon Aug 07, 2006 5:26 pm
by nincha
concatnation operator `.` , learn it... very important.

Posted: Mon Aug 07, 2006 5:45 pm
by davidppppppppppp
Is your P key broken?
concatnation operator `.` , learn it... very important.
I'm not sure what you mean. Where should I use the concatnation operator in this query?

Posted: Tue Aug 08, 2006 1:26 am
by davidppppppppppp
blackbeard,

I couldn't get it to work.

Posted: Tue Aug 08, 2006 1:37 am
by RobertGonzalez

Code: Select all

<?php
if (isset($_GET) && !empty($_GET))
{
    $keyword = trim($_GET['q']);

    if ( isset($_GET['pull']) ) {
        $pull = $_GET['pul'];
        switch ($pull) {
            case "all":
            default:
                $searchString = "text LIKE '%$keyword%'";
                break;

            case "old":
                $searchString = "number < 40 AND text LIKE '%$keyword%'";
                break;

            case "new":
                $searchString = "number >39 AND text LIKE '%$keyword%'";
                break;
        }

        //  Make sure to connect to the database
        $query = mysql_query ("SELECT * FROM TABLE WHERE $searchString");
        $result = mysql_fetch_assoc($query);
    }
}
?>