Jump to content

search term


phpabcd

Recommended Posts

Well to sum it up in easy terms.

 

Make a form.

Have the form perform a mysql select query by connecting to your database.

In the select are additional words as LIKE,MATCH,against, Boolean mode for full text searches that require full text indexing on the fields wanted to search on the tables. % is used in front, after or around the search terms depending on how would like to search the words, as in from the first characters, within the word and so on.

 

This may help

http://www.joedolson.com/Search-Engine-in-PHP-MySQL.php

 

Or from mysql boolean mode search

http://dev.mysql.com/doc/refman/5.1/en/fulltext-boolean.html

 

or maybe this clump of code I just made up the other day, this is just to give an idea, and also mine is kinda unique because I did it multi-selects and for different type searches.

 

For you to try and integrate this would require your database connect information, and also add the values of the query selects to suit your needs. The display of the query results and should have some sort of pagination as well. Don't forget to also close the connection at the end.

 

 

<?php
$display = mysql_real_escape_string($_GET['display']);
$order = mysql_real_escape_string($_GET['order']);
$search_name = mysql_real_escape_string($_GET['search_name']);
$search_words = mysql_real_escape_string($_GET['search_words']);

//search get variables from search form
if ($search_name == "first_begins_characters") {
    $result = mysql_query("SELECT * FROM users WHERE firstname LIKE '".$search_words."%' ORDER BY $display $order LIMIT $startrow,$posts_per_page" );
$total_count = mysql_query("SELECT * FROM users WHERE firstname LIKE '".$search_words."%'");
} elseif ($search_name == "first_contains_characters") {
    $result = mysql_query("SELECT * FROM users WHERE firstname LIKE '%"."$search_words"."%' ORDER BY $display $order LIMIT $startrow,$posts_per_page" );
$total_count = mysql_query("SELECT * FROM users WHERE firstname LIKE '%"."$search_words"."%'");
} elseif ($search_name == "last_begins_characters") {
    $result = mysql_query("SELECT * FROM users WHERE lastname LIKE '".$search_words."%' ORDER BY $display $order LIMIT $startrow,$posts_per_page" );
$total_count = mysql_query("SELECT * FROM users WHERE lastname LIKE '".$search_words."%'");
} elseif ($search_name == "last_contains_characters") {
    $result = mysql_query("SELECT * FROM users WHERE lastname LIKE '%"."$search_words"."%' ORDER BY $display $order LIMIT $startrow,$posts_per_page" );
$total_count = mysql_query("SELECT * FROM users WHERE lastname LIKE '%"."$search_words"."%'");
} elseif ($search_name == "all") {
$result = mysql_query("SELECT * FROM users ORDER BY $display $order LIMIT $startrow,$posts_per_page" );
$total_count = mysql_query("SELECT * FROM users");
} else {

//if anything goes wrong above or nothing selected, this will be used as the default query instead
$result = mysql_query("SELECT * FROM users ORDER BY $display $order LIMIT $startrow,$posts_per_page" );
$total_count = mysql_query("SELECT * FROM users");
}

?>

<form name="input" action="" method="get">
<?php
if (!$_GET['display']) {
$display = "firstname";
}
?>
Display:<Select style="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #000000;" name="display">
<option "Input" style="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #000000;" size="15" value="<?php echo $display; ?>"><?php echo $display; ?></option>
<option style="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #000000;" size="15" value="firstname">firstname</option>
<option style="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #000000;" size="15" value="lastname">lastname</option>
</select>
<?php
if (!$_GET['order']) {
$order = "ASC";
}
?>
Order:<Select style="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #000000;" name="order">
<option style="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #000000;" size="15" value="<?php echo $order; ?>"><?php echo $order; ?></option>
<option style="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #000000;" size="15" value="ASC">Ascending</option>
<option style="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #000000;" size="15" value="DESC">Descending</option>
</select>

<?php
if (!$_GET['search_name']) {
$search = "all";

}
?>
Search Type:<Select style="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #000000;" name="search">
<option style="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #000000;" size="15" value="<?php echo $search; ?>"><?php echo $search; ?></option>
<option style="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #000000;" size="15" value="all">all</option>
<option style="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #000000;" size="15" value="first_begins_characters">User Begins Character(s)</option>
<option style="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #000000;" size="15" value="first_contains_characters">User Contains Character(s)</option>
<option style="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #000000;" size="15" value="last_begins_characters">Last Begins Character(s)</option>
<option style="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #000000;" size="15" value="last_contains_characters">Last Contains Character(s)</option>
</select>
<br />
Search Word(s) or Char(s):<input size="40"type="text" name="search_words" style="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #000000;" size="15" value="<?php echo $_GET['search_words']; ?>">
<input type="submit" style="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #000000;" size="15" value="Search Name" />

</form>

Link to comment
Share on other sites

I tried this, but they keep showing error and i can't resolve it.

 

<?

include '../script/database.php';//locate the database

$conn = dbConnect();

if (!$conn)

die("Could not connect to the database.");

 

$search_words = mysql_real_escape_string($_GET['search_words']);

 

$query =

"SELECT * FROM User WHERE name LIKE '".$search_words."%'

UNION

SELECT name,login_id,password, email FROM User ORDER BY name ASC "; // my query to display the necessary fields

$result = mysql_query ($query, $conn); // get results by running the query

$i = 1;

while ($row = mysql_fetch_row($result)) //use loop to get the results

{

?>

    <td><?= $i?></td>

    <td><?= $row['name']?></td>

    <td><?= $row['login_id']?></td>

    <td><?= $row['password']?></td>

    <td><?= $row['email']?></td>

</tr>

<? $i++; ?>

<? 

}

dbDisconnect($conn); // to disconnect database

?> 

</table>

Link to comment
Share on other sites

Change this

$query =
"SELECT * FROM User WHERE name LIKE '".$search_words."%'
UNION
SELECT name,login_id,password, email FROM User ORDER BY name ASC ";

 

to this

$query =
"SELECT * FROM User WHERE name LIKE '".$search_words."%' ORDER BY name ASC LIMIT $startrow,$posts_per_page" );

 

And you should certainly be limiting these results, and is why I left the limit in the query, If have any type pagination use your values there for where to start the row for mysql and where to stop.

Link to comment
Share on other sites

I came back and looked at the code more too, why not try like this.

 

 <?php
include '../script/database.php';//locate the database
$conn = dbConnect();
if (!$conn)
   die("Could not connect to the database.");
   
$query =
"SELECT * FROM User WHERE name LIKE '".$search_words."%' ORDER BY name ASC" ); // my query to display the necessary fields
$result = mysql_query ($query, $conn); // get results by running the query

while ($row = mysql_fetch_row($result)) //use loop to get the results
{
$name = $row['name'];
$login_id = $row['login_id'];
$password = $row['password'];//really want to display passwords?
$email = $row['email'];
?>
    <td><?php echo $name;?></td>
    <td><?php echo $login_id;?></td>
    <td><?php echo $password;?></td>
    <td><?php echo $email;?></td>
</tr>
<?php
}
dbDisconnect($conn); // to disconnect database
?> 
</table>

 

So this is it no limits or pagination, I really don't know the rest of your page coding or the way using this, but this should at least show you some results and work so can work with it. Naturally if there are no search words inserted there would be no results, is why i did the if and else statements for the query in the first code I wrote.

Link to comment
Share on other sites

how can i filter out based on the search terms? Now, it can work with no errors. In the form page, there's only one field to search the variable is search_words.

 

<?php

include '../script/database.php';//locate the database

$conn = dbConnect();

if (!$conn)

die("Could not connect to the database.");

 

$search_words = mysql_real_escape_string($_GET['search_words']);

 

$query =

"SELECT name,login_id,password,email FROM User WHERE name LIKE '".$search_words."%' ORDER BY name ASC

";// my query to display the necessary fields

$result = mysql_query ($query, $conn); // get results by running the query

$i = 1;

while ($row = mysql_fetch_assoc($result)) //use loop to get the results

{

$name = $row['name'];

$login_id = $row['login_id'];

$password = $row['password'];

$email = $row['email'];

?>

    <td><?php echo $i;?></td>

    <td><?php echo $name;?></td>

    <td><?php echo $login_id;?></td>

    <td><?php echo $password;?></td>

    <td><?php echo $email;?></td>

</tr>

<? $i++; ?>

<?php 

}

dbDisconnect($conn); // to disconnect database

?> 

Link to comment
Share on other sites

how can i filter out based on the search terms? Now, it can work with no errors. In the form page, there's only one field to search the variable is search_words.

 

I don't fully understand the question, do you mean how can you also search emails and such as well? If so show both your current codes for the form and also the php select page.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.