PHP - Search Filter Function Not Working
Hey guys for a project im making a search filter function. Only one filter seems to be working and that's the genre filter :(
This is my code:
function filterFilms(){ global $conn; $loop = 0; if(isset($_POST['titel']) && !empty($_POST['titel'])){ $titel = sanatize($_POST['titel']); } if(isset($_POST['genre']) && !empty($_POST['genre'])){ $genre = sanatize($_POST['genre']); } $sql = "SELECT TOP 20 * From Movie Inner Join Movie_Director On Movie_Director.movie_id = Movie.movie_id Inner Join Movie_Genre On Movie_Genre.movie_id = Movie.movie_id Inner Join Person On Movie_Director.person_id = Person.person_id"; if(isset($titel) && !empty($titel)){ if($loop == 0){ $sql .= " WHERE ( title = ':titel' OR title like ':titel')"; $loop++; } else{ $sql .= " AND ( title = ':titel' OR title like ':titel')"; } } if(isset($genre) && !empty($genre)){ if($loop == 0){ $sql .= " WHERE genre_name = :genre"; $loop++; } else{ $sql .= " AND genre_name = :genre"; } } $query = $conn->prepare($sql); if(isset($titel) && !empty($titel)){ $query->bindValue('titel', $titel,PDO::PARAM_STR); } if(isset($genre) && !empty($genre)){ $query->bindParam('genre', $genre,PDO::PARAM_STR); } $query->execute(); $rows = $query->fetchAll(); return $rows; }
Anyone any idea??
Similar TutorialsI have some text boxes for searching the students in database also i have a drop down box for selecting student with balance amount to be paid tell me how to filter or search the list of stuents Code: [Select] <select> <option value="">All </option> <option value="">Unpaid </option> <option value="">Paid </option> </select> hi, I have a search page with a checkbox $departuk that returns the value "true" if checked. I want to return search results if this returns true and the flag 'Y' is available if ($departuk == "true"){ if ($data[$i]['departuk'] == 'Y'){ $result = $obj->result; } else{ echo "do some thing else"; } else{ echo"there was a problem"; } any pointers / advice is appreciated thanks hello. i have a search box that will display some results. I also have a drop down list with values "1" and "2". the values for each results are stored into database in a "req" field. Code: [Select] <select name="req" id="req"> <option value="1">yes</option> <option value="2">no</option> </select> $sql = 'SELECT * FROM topic WHERE (Title LIKE '."'%$search1%') AND req='1' what am i missing. thanks OK guys so I have my final project for school due next quarter and Its a database management application. I'm dont the upload edit delete scripting but now I need to do the search feature on the front end. I have a basic understanding of the search abilities in PHP but what i'd love to do is create something like Istockphoto.com and many other inventory sites that allow for a "search" field and also a filter. I'd assume there needs to be global variables that will set and adjust the query as things are checked or filled out. does anyone know of any examples/tutorials/demos that I can browse to start figuring this out? Thanks I'm ready for the next step on my site, which is learning how to filter results down with a second search. The scenario is: A viewer searches for a plant by 4 variables to describe the leaf. After submit, they go to the results page, with 200 paginated results. (This is where I've gotten to) Now, on that same page, they choose to further narrow their results by searching those 200 results for plants that have purple flowers. Maybe they get 40 results and can use another variable to narrow further - and so on. What's the best way to set this up? I've been looking into temporary tables, but it seems to be a challenge because the table deletes the moment they leave that page or close the connection? Sessions seem like another possibility, but I read there's security issues. What's the best way to set this up? Just re-query the database with the entire set of variables from both the first and second searches? is it quicker to build some type of permanent table with a timestamp that I can use to delete it after the user has gone away? This topic has been blindfolded and driven across the boarder to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=344623.0 (That was a pun, not a typo.) Hi all. I have three scripts interacting with one another to create a checkbox that when selected will filter out any products from the table that are custom products (any products with product_type = 3 in the sql database). Right now the page reloads when the checkbox is clicked to the correct url, but the table hasn't actually has custom products filtered out. I'm very new to programming and made these code changes by copying and modifying another checkbox for our website that already worked. Please take a look and let me know if you see where I might be going wrong. I have tried to include all relevant parts of the scripts: Code: [Select] // index.php if (isset( $_GET['exclude_custom'] )) { if($_GET['exclude_custom'] == "noexclude"){ $exclude_custom = ''; $exclude = "noexclude"; $AppUI->setState( 'QuoteIdxCond', 'noexclude'); } else{ $exclude_custom = "exclude"; $exclude = "exclude"; $AppUI->setState( 'QuoteIdxCond', 'exclude'); } } $exclude = $AppUI->getState( 'QuoteIdxCond' ) ? $AppUI->getState( 'QuoteIdxCond' ) : ''; if($exclude == "exclude") $exclude_custom = "exclude"; else $exclude_custom = ''; Code: [Select] // vw_idx_products.php global $sort_state, $products_view_mode, $exclude_custom; include_once("{$dPconfig['root_dir']}/modules/quotes/utility.php"); load_all_products(); <script LANGUAGE="JavaScript"> function excludeCustom() { f = document.productQuotes.exclude_custom.checked; if(f == true) window.location =('./index.php?m=quotes&exclude_custom=exclude'); else // window.location =('./index.php?m=quotes&exclude_custom=noexclude'); window.location =('./index.php?m=quotes&exclude_custom=noexclude'); } </script> <FORM ACTION="?m=quotes" METHOD="post" NAME="productQuotes" ID="productQuotes"> <B> Stock Products & Services Only: </B><input type="checkbox" onclick="excludeCustom()" NAME="exclude_custom" id="exclude_custom" <?php echo ($exclude_custom ? 'checked="checked"' : '');?> /> <THEAD> Code: [Select] // utility. php global $item_list, $drawn_array, $item_list_parents, $sort_state, $AppUI, $exclude_custom; function load_all_products() { global $AppUI; global $sorted_item_list; global $additional_pfilter; load_type_list(); $orderby = 'product_id'; $search_map = array($orderby, 'product_number','product_name'); $where = $AppUI->getState( 'QuoteIdxWhere' ) ? $AppUI->getState( 'QuoteIdxWhere' ) : '*'; $not =" "; $op = " OR"; $check = substr(trim($where),0,4); if(stristr($check,"NOT")){ $op ="AND"; $not = " NOT "; $where = substr(trim($where),4); $where = trim($where); } // assemble the sql statement $q = new DBQuery; $q->addTable('products'); $q->addJoin('companies', 'com', 'products.product_company_id = com.company_id'); $q->addJoin('users', 'u', 'products.product_owner = u.user_id'); $q->addQuery('products.*, com.company_name, u.user_username'); $where_filter = " "; foreach($search_map as $search_name) $where_filter .=" $op $search_name $not REGEXP '$where'"; $where_filter = substr($where_filter, 5); // echo $where_filter; if($where != "*") $q->addWhere("($where_filter)"); $q->addOrder('product_id'); if (isset( $_POST['exclude_custom'] )) { $where_filter .= "AND PRODUCT_TYPE != '3'"; } $sql = $q->prepare(); $q->clear(); $sql_list = db_exec( $sql ); if ($sql_list) $rn = db_num_rows( $sql_list ); else { echo db_error(); $rn = 0; } Thanks! Friends, I have a code to filter out stopwords (from text file) out of a string supplied. Seems its not filtering out the stop words. Code: [Select] <?php $CommonWords = file_get_contents('http://minimate.co.uk/stopwords.txt'); $CommonWords = explode("\n", $CommonWords); $keywords = 'This is did samle a as any ass'; $search_keywords = strtolower(trim($keywords)); $arrWords = explode(' ', $search_keywords); $arrWords = array_unique($arrWords); foreach ($arrWords as $word) { if (!in_array($word, $CommonWords) && (trim($word) != '')) { $searchWords[] = $word; } } print_r($searchWords); ?> Output is: Quote Array ( => this [1] => is [2] => did [3] => sample [4] => a [5] => as [6] => any [7] => ass ) So the output is not filtering out any words from that text file. Can someone help me with this? Cheers Natasha T Hey all, While the filter itself is functioning properly, the flag doesn't seem to be. Here's how I have it set up: Code: [Select] $UserInput = filter_var($UserInput , FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); // Test Format 1 $UserInput = filter_input(INPUT_POST, 'UserInput', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); // Test Format 2 As you can see, I have set up to test methods however, each one fails regarding the flag..or so it's seeming to me. FILTER_FLAG_STRIP_LOW is supposed to strip out anything > 32 in ascii, but it isn't. '&' (38) is greater than 32 but it still displays in the browser. Am I missing something here? Hi all How do I modify the below code to search multiple tables in mySQL database? $query = "select * from store_items where description like \"%$trimmed%\" or title like \"%$trimmed%\" or dimensions like \"%$trimmed%\" order by id ASC"; $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); It is for a search function and I need it to search another table called 'about_text' and 'faq_text' Thanks Pete Ok i have seen many solutions for using a search function to post onto webpage directly from the mysql db but my question is
is there a way to seach the results that were already outputted via a php query, for instance i have a query that returns
1 MisFit_Kay1 811 2 sajoma 806 3 Atlfan 708 4 karlsbad70 633 5 Booo 570 6 MisFit_Mimi_shg 541 7 PRO_Viking 540 8 tm7_cryzal 540 9 TM7__mina 499 10 PRO_DRFRANK 488 11 PRO_FLAK 488 12 Mhoram 464 13 TM7__CEDAR 462 14 ProSultan 456 15 UBG_Steely1 450but with lots more results now what im hoping for is a point in a direction where i could add a search box that will highlight the name from the list provided . any help will be appreciated Edited by Justafriend, 20 October 2014 - 01:56 AM. Hi guys! I want to improve the search function of a wordpress site with AliPlugin. The products shown are completely off topic, you have to be very accurate to get what you need, if you are too accurate of course, you will get nothing.
The site is: http://www.beta.cfcbazar.com Its the site for testing.
Can anyone help me please? hi im just trying to do a search on a database and display results based on what the user selected: form code: Code: [Select] html> <body> <form action="search.php" method="POST"> Type of property: <select name="Type_of_Property"> <option>Terraced</option> <option>Detached</option> <option>Semi-Detached</option> <option>Bungalow</option> <option>Flat </option> </select> <p><input type="submit" value="Send Details" name="B1"></p> </form> php code: Code: [Select] <html> <body> <?php error_reporting(E_ALL); mysql_connect("localhost", "root") or die(mysql_error()); // makes a connection mysql_select_db("estate_agents") or die('I cannot connect to the database because: ' . mysql_error()); //conects to the database $result = mysql_query("SELECT * FROM properties WHERE Type_of_Property ='".$_POST['Type_of_Property']); echo "<table border=1>\n"; echo "<tr><td>Address 1</td><td>Address 2</td><td>Postcode</td><td>Type of Property</td><td>Number of Bedrooms</td><td>Number of Bathrooms</td></tr>\n"; while ($row = mysql_fetch_row($result)) { } print "Data base updated with: " .$_POST["Type_of_Property"] ; ?> </body> </html> the propbelm im having is being able to display the results in the table in the while loop any help is appreciated thank you I have been using this code which i found on here which I have gotten to work with my website but I would just like to know how I can make it so that when I click the search button it can take me to a new page which will show the results in a table. Code: [Select] <?php // Set up our error check and result check array $error = array(); $results = array(); // First check if a form was submitted. // Since this is a search we will use $_GET if (isset($_GET['search'])) { $searchTerms = trim($_GET['search']); $searchTerms = strip_tags($searchTerms); // remove any html/javascript. if (strlen($searchTerms) < 3) { $error[] = "Search terms must be longer than 3 characters."; }else { $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection. } // If there are no errors, lets get the search going. if (count($error) < 1) { $searchSQL = "SELECT id, product_name, product_price, product_description FROM product WHERE "; // grab the search types. $types = array(); $types[] = isset($_GET['name'])?"`product_name` LIKE '%{$searchTermDB}%'":''; $types[] = isset($_GET['price'])?"`product_price` LIKE '%{$searchTermDB}%'":''; $types[] = isset($_GET['description'])?"`product_description` LIKE '%{$searchTermDB}%'":''; $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked) if (count($types) < 1) $types[] = "`product_name` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked $andOr = isset($_GET['matchall'])?'AND':'OR'; $searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `product_name`"; // order by title. $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}"); if (mysql_num_rows($searchResult) < 1) { $error[] = "The search term provided {$searchTerms} yielded no results."; }else { $results = array(); // the result array $i = 1; while ($row = mysql_fetch_assoc($searchResult)) { $results[] = "{$i}: {$row['product_name']}<br />{$row['product_price']}<br />{$row['product_description']}<br /><br />"; $i++; } } } } <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="get" name="searchForm" id="searchForm"> Search For: <input type="text" name="search" value="<?php echo isset($searchTerms)?htmlspecialchars($searchTerms):''; ?>" /> <br /> <br /> <input type="submit" name="submit" value="Search!" /> </form> <?php echo (count($results) > 0)?"Your search term: {$searchTerms} returned:<br /><br />" . implode("", $results):""; ?> I would be very grateful if someone could help me to get this done. Edit (KP): Next time, please use code tags. Hi, im trying to do a search function that will search through the database Examples : table game with id, name I want to get the ID by searching with keywords BUT my keywords is like 'battle field' and I search only 'battle' How do I get battle field ID from the database with just 'battle' ? Please help me out or show me tutorials. thanks. hello all, im running a search and it will work if i search one letter, but not anything more. any ideas? Code: [Select] <?php include("style.php"); //If we submitted the form if(isset($_POST['submitMe'])) { echo '<div id="musicinfo" align="center"> <table border="0" width="100%" cellspacing="0" cellpadding="0"> <tr> <td align="center"> <form action="" method="POST"> <center> <input type="text" name="name" size="24"> <input type="submit" value="Search" name="submitMe"> </form> </td> </tr> </table> </div>'; $hmm = simplexml_load_file('http://www.durhamit.ca:8181/1.0/?method=database.search&searcharg='.$_POST['name'] .''); foreach($hmm->tracks->track as $tracks) { $artist = $tracks->artist; $title = $tracks->title; $album = $tracks->album; $id = $tracks->id; echo '<table border="0" width="60%" align="center">'; echo '<tr>'; echo '<td width="25%">'.$artist.'</td>'; echo '<td width="25%">'.$title.'</td>'; echo '<td width="25%">'.$album.'</td>'; echo '<td width="25%"><a href="http://www.durhamit.ca:8181/1.0/?method=player.playDatabaseItem&id='.$id.'">Play</a></td>'; echo '</tr>'; echo '<tr>'; echo '<td colspan="4"><hr></td>'; echo '</tr>'; echo '</table>'; } } //If we haven't submitted the form else { ?> <form action="" method="POST"> <center> <input type="text" name="name" size="24"> <input type="submit" value="Search" name="submitMe"> </form> <? } ?> Won't let me type in this box more then 1 line . See Post 3,4 Thanks I was showing a friend what I've been working on, and as I watched him type in an entry to search - then not get a result that I know was indeed in the database I realized I needed to accommodate "user laziness." He didn't use punctuation. So I'm trying to figure out how to make that a nonfactor in searches. In other words, my database may have an entry called "St. Patrick's Cathedral" If somebody types "st patricks cathedral" I want the result to still show up. Here's what I'm using right now: Code: [Select] if(isset($_POST[submitted])) { $item = $_REQUEST['find']; $filter1 = trim($_POST['filter1']); if ($filter1 == 'sci-names') { $data = mysql_query("SELECT * FROM table WHERE CONCAT(taxonomic_genus,' ',scientific_name) LIKE '%$item%'"); Hi, I am trying to build a search function and so far I have it working with just one DB field but I need it to search for different fields. This is my search form: Code: [Select] <form action="t_articulo_Results.php" method="get" name="SearchForm" id="SearchForm"> <label> <input type="text" name="S_articulo_esp" id="S_articulo_esp" value="search" size="30" /> <input type="hidden" name="Search" id="Search" value="Buscar" /> </label> </form> where the field "S_articulo_esp" is a text field to enter the search criteria. This is the code on my results page inclung query: Code: [Select] <?php $MySearch_DefaultWhere = ""; if (!session_id()) session_start(); if ((isset($_GET["Search"]) && $_GET["Search"] != "")) { $MySearch = new FilterDef; $MySearch->initializeQueryBuilder("MYSQL","1"); //keyword array declarations //comparison list additions $MySearch->addComparisonFromEdit("company_name","S_articulo_esp","AND","Includes",0); //save the query in a session variable if (1 == 1) { $_SESSION["MySearch_t_articulo_Results"]=$MySearch->whereClause; } } else { $MySearch = new FilterDef; $MySearch->initializeQueryBuilder("MYSQL","1"); //get the filter definition from a session variable if (1 == 1) { if (isset($_SESSION["MySearch_t_articulo_Results"]) && $_SESSION["MySearch_t_articulo_Results"] != "") { $MySearch->whereClause = $_SESSION["MySearch_t_articulo_Results"]; } else { $MySearch->whereClause = $MySearch_DefaultWhere; } } else { $MySearch->whereClause = $MySearch_DefaultWhere; } } $MySearch->whereClause = str_replace("\\''", "''", $MySearch->whereClause); $MySearchwhereClause = ''; ?> <?php $maxRows_RecordSett_articulo = 10; $pageNum_RecordSett_articulo = 0; if (isset($_GET['pageNum_RecordSett_articulo'])) { $pageNum_RecordSett_articulo = $_GET['pageNum_RecordSett_articulo']; } $startRow_RecordSett_articulo = $pageNum_RecordSett_articulo * $maxRows_RecordSett_articulo; mysql_select_db($database_MySQLconnect, $MySQLconnect); $query_RecordSett_articulo = "SELECT id_cliente, company_name, cliente_calle, cliente_numero, cliente_local, cliente_poblacion, cliente_cp, cliente_pais, cliente_tel, cliente_fax, cliente_movil, cliente_personadecontacto, cliente_email, cliente_web FROM t_clientes ORDER BY company_name ASC"; setQueryBuilderSource($query_RecordSett_articulo,$WADbSearch1,false); $query_limit_RecordSett_articulo = sprintf("%s LIMIT %d, %d", $query_RecordSett_articulo, $startRow_RecordSett_articulo, $maxRows_RecordSett_articulo); $RecordSett_articulo = mysql_query($query_limit_RecordSett_articulo, $MySQLconnect) or die(mysql_error()); $row_RecordSett_articulo = mysql_fetch_assoc($RecordSett_articulo); if (isset($_GET['totalRows_RecordSett_articulo'])) { $totalRows_RecordSett_articulo = $_GET['totalRows_RecordSett_articulo']; } else { $all_RecordSett_articulo = mysql_query($query_RecordSett_articulo); $totalRows_RecordSett_articulo = mysql_num_rows($all_RecordSett_articulo); } $totalPages_RecordSett_articulo = ceil($totalRows_RecordSett_articulo/$maxRows_RecordSett_articulo)-1; ?> please note this line Code: [Select] $MySearch->addComparisonFromEdit("company_name","S_articulo_esp","AND","Includes",0);//comparison list additions because here is where I am getting confused . How can I add more fields to it? Thanks |