phpabcd Posted December 21, 2010 Share Posted December 21, 2010 I would like to know how to do search terms. For example, someone key in the terms in the text field, and click search, thn it will show the data. Quote Link to comment https://forums.phpfreaks.com/topic/222277-search-term/ Share on other sites More sharing options...
BK87 Posted December 21, 2010 Share Posted December 21, 2010 search what? database? Quote Link to comment https://forums.phpfreaks.com/topic/222277-search-term/#findComment-1149795 Share on other sites More sharing options...
phpabcd Posted December 21, 2010 Author Share Posted December 21, 2010 yup. Quote Link to comment https://forums.phpfreaks.com/topic/222277-search-term/#findComment-1149805 Share on other sites More sharing options...
QuickOldCar Posted December 21, 2010 Share Posted December 21, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/222277-search-term/#findComment-1149821 Share on other sites More sharing options...
phpabcd Posted December 21, 2010 Author Share Posted December 21, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/222277-search-term/#findComment-1149947 Share on other sites More sharing options...
QuickOldCar Posted December 21, 2010 Share Posted December 21, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/222277-search-term/#findComment-1150074 Share on other sites More sharing options...
QuickOldCar Posted December 22, 2010 Share Posted December 22, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/222277-search-term/#findComment-1150121 Share on other sites More sharing options...
phpabcd Posted December 22, 2010 Author Share Posted December 22, 2010 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/222277-search-term/#findComment-1150197 Share on other sites More sharing options...
QuickOldCar Posted December 22, 2010 Share Posted December 22, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/222277-search-term/#findComment-1150202 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.