pixeltrace Posted February 28, 2007 Share Posted February 28, 2007 guys, i need help on my quick search menu assuming that this is my quick search menu item keywords (textfield) job specialization (list) submit button <form id="form1" name="form1" method="post" action="view.php"> Keywords : <input type="text" name="textfield" /> <br><br> Job Specialization : <select name="select"> <option value="president">president</option> <option value="vice-president">vice-president</option> <option value="director">director</option> <option value="mananger">manager</option> <option value="supervisor">supervisor</option> </select> <input type="submit" name="Submit" value="Search" /> </form> what will be the proper code in view.php normally we usually put the query select and placed the values of the keywords, job specialization but i dont have any idea how to construct it considering the possibility that if there is no data found how do i show the word no item found on the page and assuming the data from my database has 2 words but the user only input the first word how will the codes in view.php function that whether they type the first or second word only you can still be able to view the item hope you guys can help me with this thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/40544-need-help-on-quick-search-menu/ Share on other sites More sharing options...
pixeltrace Posted February 28, 2007 Author Share Posted February 28, 2007 any update or help on this? thanks! Quote Link to comment https://forums.phpfreaks.com/topic/40544-need-help-on-quick-search-menu/#findComment-196216 Share on other sites More sharing options...
boo_lolly Posted February 28, 2007 Share Posted February 28, 2007 any update or help on this? thanks! it's pretty simple: <?php /*view.php*/ $db = mysql_connect("xxx", "xxx", "xxx"); mysql_select_db("your_db", $db); $sql = "SELECT * FROM your_table WHERE column1 LIKE '%". $_POST['textfield'] ."%' AND column2 = '". $_POST['select'] ."'"; $query = mysql_query($sql) OR die(mysql_error()); $numRows = mysql_num_rows($query); if($numRows < 1){ echo "<font color=\"FF0000\">Sorry, there are no matches. Please try again.</font><br />\n"; }else{ while($row = mysql_fetch_array($query)){ echo $row['column1'] .", ". $row['column2'] .", ". $row['column3'] ." etc..etc..."; } } ?> does that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/40544-need-help-on-quick-search-menu/#findComment-196231 Share on other sites More sharing options...
itsmeArry Posted February 28, 2007 Share Posted February 28, 2007 here is the code that will do it for you... you need to specify the hostname, username, password, databasename, table name. <?php $localhost = ''; $user = ''; $pass = ''; $dbName = ''; $keyword = ''; $jobSpe = ''; $table1 = ''; if(isset($_POST['textfield']) && $_POST['textfield'] !='') { $keyword = $_POST['textfield']; } if(isset($_POST['select']) && $_POST['select'] !='') { $jobSpe = $_POST['select']; } $conn = mysql_connect($localhost, $user, $pass) or die("Could Not connect to database."); mysql_select_db($dbName); $where = array(); if($keyword != '') $where[] = " keyword like %".$keywords."%"; if($jobSpe != '') $where[] = " jobSpecification Like %".$jobSpe."%"; $QueryCond = " where "; foreach($where as $cond) { $QueryCond .= $cond . " AND "; } $QueryCond = rtrim($QueryCond,"AND"); if(!empty($where)) { $query = "Select * from ".$table1." ".$QueryCond; $result = mysql_query($query); $totalRows = mysql_num_rows($result); if($totalRpws == 0) { echo "No records found"; die(); } // do wht ever you want to do here... } else { echo "Please select atleast one Job Spec or write some keyword"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/40544-need-help-on-quick-search-menu/#findComment-196234 Share on other sites More sharing options...
pixeltrace Posted February 28, 2007 Author Share Posted February 28, 2007 thanks! i will try and test these two. Quote Link to comment https://forums.phpfreaks.com/topic/40544-need-help-on-quick-search-menu/#findComment-196243 Share on other sites More sharing options...
boo_lolly Posted February 28, 2007 Share Posted February 28, 2007 thanks! i will try and test these two. you're gonna get parse errors with arry's code. Quote Link to comment https://forums.phpfreaks.com/topic/40544-need-help-on-quick-search-menu/#findComment-196246 Share on other sites More sharing options...
itsmeArry Posted February 28, 2007 Share Posted February 28, 2007 boo_lolly can u tell me where the parse error will be.. Thanks... Quote Link to comment https://forums.phpfreaks.com/topic/40544-need-help-on-quick-search-menu/#findComment-196251 Share on other sites More sharing options...
boo_lolly Posted February 28, 2007 Share Posted February 28, 2007 boo_lolly can u tell me where the parse error will be.. Thanks... for example your sql query. LIKE %something% needs to be LIKE '%something%' your dynamic sql query by adding AND to the end if isset, isn't that efficient. there is room for improvement. try doing something like, if isset, put it into an array. then, implode the array with AND in between. Quote Link to comment https://forums.phpfreaks.com/topic/40544-need-help-on-quick-search-menu/#findComment-196257 Share on other sites More sharing options...
itsmeArry Posted February 28, 2007 Share Posted February 28, 2007 Thanks for your suggestion I will keep that in mind... Quote Link to comment https://forums.phpfreaks.com/topic/40544-need-help-on-quick-search-menu/#findComment-196260 Share on other sites More sharing options...
pixeltrace Posted March 1, 2007 Author Share Posted March 1, 2007 hi, what will be the code if let say i never type anything from the keywords field and just select any item from the drop down. with that i can still view all the list of items that fall on that same category? this is my code for my index.php <form id="form1" name="form1" method="post" action="jobs/view1.php"> Keywords : <input name="keywords" type="text" size="35" /> <br> <br> Job Specialization : <select name="specialization"> <? include 'admean/db_connect.php'; $uSql = "SELECT specialization FROM specialization ORDER by sid"; $uResult = mysql_query($uSql, $connection); if(!$uResult){ echo 'no data found'; }else{ while($uRow = mysql_fetch_array($uResult)){ ?> <option value="<?= $uRow[specialization]?>"><?= $uRow[specialization]?></option> <? } } ?> </select> <input type="submit" name="Submit" value="Search" /> </form> and this is my code for my view1.php <?php include '../admean/db_connect.php'; $sql = "SELECT * FROM job_ads WHERE j_position LIKE '%". $_POST['keywords'] ."%' AND specialization = '". $_POST['specialization'] ."'"; $query = mysql_query($sql) OR die(mysql_error()); //$uResult = mysql_query($uSql, $connection); $numRows = mysql_num_rows($query); if($numRows < 1){ echo "<font color=\"FF0000\">Sorry, there are no matches. Please try again.</font><br />\n"; }else{ while($row = mysql_fetch_array($query)){ echo $row['j_position'] .", ". $row['specialization'] .", ". $row['date_posted'] ." etc..etc..."; } } ?> need help on this. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/40544-need-help-on-quick-search-menu/#findComment-196600 Share on other sites More sharing options...
pixeltrace Posted March 1, 2007 Author Share Posted March 1, 2007 another scenario is what if i didnt select any items on the drop down but i just type the word on the keyword area what will be the code for this second scenario. need help please. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/40544-need-help-on-quick-search-menu/#findComment-196602 Share on other sites More sharing options...
boo_lolly Posted March 1, 2007 Share Posted March 1, 2007 another scenario is what if i didnt select any items on the drop down but i just type the word on the keyword area what will be the code for this second scenario. need help please. thanks! this is an example. i've achieved the same task like this: <?php /* *checks to see which input fields had data *then depending on the input field *enter in the correct sql query for that input field *into a new index in the array $query_array */ $query_array = array(); if(!empty($lname)){ $query_array[] = "brideLname LIKE '%". $lname ."%' OR groomLname LIKE '%". $lname ."%'"; } if(!empty($fname)){ $query_array[] = "brideFname LIKE '%". $fname ."%' OR groomFname LIKE '%". $fname ."%'"; } if(!empty($event_day)){ $query_array[] = "event_day LIKE '%". $event_day ."%'"; } if(!empty($event_month)){ $query_array[] = "event_month LIKE '%". $event_month ."%'"; } if(!empty($event_year)){ $query_array[] = "event_year LIKE '%". $event_year ."%'"; } /* *$query string becomes all the sql queries *with 'AND' in between them */ $query_string = implode(" AND ", $query_array); /*check sql query string*/ #echo $query_string ."<br />\n"; $result = mysql_query("SELECT * FROM my_search_table WHERE ". $query_string ."") OR die(mysql_error()); ?> understand? Quote Link to comment https://forums.phpfreaks.com/topic/40544-need-help-on-quick-search-menu/#findComment-196678 Share on other sites More sharing options...
itsmeArry Posted March 1, 2007 Share Posted March 1, 2007 The code I gave you above will handel this as well. just change these lines if($keyword != '') $where[] = " keyword like %".$keywords."%"; if($jobSpe != '') $where[] = " jobSpecification Like %".$jobSpe."%"; to if($keyword != '') $where[] = " keyword like '%".$keywords."%'"; if($jobSpe != '') $where[] = " jobSpecification Like '%".$jobSpe."%'"; Quote Link to comment https://forums.phpfreaks.com/topic/40544-need-help-on-quick-search-menu/#findComment-196699 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.