PHP - Advanced Search
Im building an advanced search feature and its mostly going fine. The only problem is that results are displayed more times than they need to be.
I have a test post in the database: p_name p_content Testing Advanced Search This is to test the advanced search When i do a search with the keywords "testing advanced search" and set it to match any keywords it does bring up this post as it should do. But it is displaying it 3 times when there is only one instance of it in the database. here is my code: $match = $_POST['match']; // for this example match === any $keywords = $_POST['keywords']; // for this example keywords === testing advanced search $within = $_POST['within']; // for this example within === p_c (post_content only) switch($within) { case 'p_s_c': default: $sql_match = 'p.p_name, p.p_content'; break; case 'p_c': $sql_match = 'p.p_content'; break; case 'p_s': $sql_match = 'p.p_name'; break; case 't_t': $sql_match = 't.t_name'; break; } $match === 'all' ? $keywords = '"'.$keywords.'"' : $keywords = $keywords; $query = $link->query("SELECT p.*, t.* FROM ".TBL_PREFIX."posts as p JOIN ".TBL_PREFIX."topics as t WHERE MATCH ($sql_match) AGAINST('$keywords' IN BOOLEAN MODE)") or die(print_link_error()); while($row = $query->fetch(PDO::FETCH_ASSOC)) { $return = preg_split('|, |', $sql_match); for($i=0; $i<count($return); $i++) { $return[$i] = substr($return[$i], 2); echo '<p>Results: '.$row[$return[$i]].'</p>'; } } And here is the echoed query: SELECT p.*, t.* FROM asf_posts as p JOIN asf_topics as t WHERE MATCH (p.p_content) AGAINST('Testing advanced search' IN BOOLEAN MODE) Any help? Similar TutorialsHey guys, at the moment I am trying to do an advanced search on events, 4 fields - county, date, title and hoster, so I am not really sure how to go about this but either way I started writing a script to do this, you learn from trying and mistaken right? Now I made sure the radio buttons all worked before I tryed this, checking the values of them on the form (seperate page) and if they get transferred correctly over to this page, basically for every field there are 2 radio boxes yes and No, which means for them to be included in the search or not, this is the code I have so far and if no radio buttons are selected and the fields are empty it comes up with the table and no results, when everything is set to no the page is blank, when everything is set to yes and I put the correct information in the fields then it comes up with the right data, so far it basically only works if all is yes (I will change it to LIKE later so they don't have to put in the exact information for certain fields) so I guess I am on the right way? Where and what am I doing wrong? Code: [Select] <?php session_start(); include 'connect.php'; $username = $_SESSION['username']; if(!isset($_SESSION['username'])) { echo '<div align="center">'; echo 'You have to be a registered member to be able to view events.<br><br> <a href="register.html">Click here to register</a>'; echo '<br><br><br><br>Or if you are already a member, please login to use this area.<br>'; echo ' <form method="POST" action="loginverification.php"> <table border="0"> <tr><td> Username: </td><td><input type="text" name="username" size="15" /></td></tr> <tr><td>Password:</td><td> <input type="password" name="password" size="15" /></td></tr> </table> <div align="center"> <p><input type="submit" value="Login" /></p> </div> </form>'; echo '</div>'; } else{ $theDate = isset($_REQUEST["date1"]) ? $_REQUEST["date1"] : ""; $eventcounty = $_POST['county']; $eventdescriptionheader = $_POST['eventdescriptionheader']; $hoster = $_POST['hoster']; if($_POST['searchcounty'] == "No") { $result = mysql_query("SELECT * FROM Events WHERE eventdate = '$theDate' AND eventdescriptionheader = '$eventdescriptionheader' AND hoster = '$hoster' ORDER BY eventdate ASC")or die(mysql_error()); } elseif($_POST['searchdate'] == "No") { $result = mysql_query("SELECT * FROM Events WHERE eventcounty = '$eventcounty' AND eventdescriptionheader = '$eventdescriptionheader' AND hoster = '$hoster' ORDER BY eventdate ASC")or die(mysql_error()); } elseif($_POST['searchtitle'] == "No") { $result = mysql_query("SELECT * FROM Events WHERE eventcounty = '$eventcounty' AND eventdate = '$theDate' AND hoster = '$hoster' ORDER BY eventdate ASC")or die(mysql_error()); } elseif($_POST['searchhoster'] == "No") { $result = mysql_query("SELECT * FROM Events WHERE eventcounty = '$eventcounty' AND eventdate = '$theDate' AND eventdescriptionheader = '$eventdescriptionheader' ORDER BY eventdate ASC")or die(mysql_error()); } else { $result = mysql_query("SELECT * FROM Events WHERE eventcounty = '$eventcounty' AND eventdate = '$theDate' AND hoster = '$hoster' AND eventdescriptionheader = '$eventdescriptionheader' ORDER BY eventdate ASC")or die(mysql_error()); echo '<br>'; echo "<table border='0'> <tr> <th>Date/Time</th> <th>Event</th> <th>Participants</th> <th>Hoster</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>"; print date('d M Y', strtotime($row['eventdate'])); echo " "; echo $row['starttime'] . "</td>"; echo '<td><a href="showevent.php?eventsID='; echo $row['eventsID']; echo '">'; echo $row['eventdescriptionheader']; echo "</a></td>"; echo "<td>" . $row['currentparticipants'] . "/" . $row['maxparticipants'] . "</td>"; echo "<td>" . $row['hoster'] . "</td>"; echo "</tr>"; } echo '</table>'; $check = mysql_num_rows($result); if ($theDate == "0000-00-00") { echo 'You did not select a date.'; echo '<INPUT TYPE="button" VALUE="Back" onClick="history.go(-1);return true;">'; } elseif ($check == 0) { echo 'No results found.'; echo '<INPUT TYPE="button" VALUE="Back" onClick="history.go(-1);return true;">'; } echo '<br><INPUT TYPE="button" VALUE="Back" onClick="history.go(-1);return true;"> '; } } ?> Can someone help me code kind of a "advanced search" form, that will get a URL: Code: [Select] <form method="get"> <input type="checkbox" name="level" value="PreK" /> <input type="checkbox" name="level" value="Elem" /> <input type="checkbox" name="level" value="MS" /> <input type="checkbox" name="level" value="HS" /> <input type="checkbox" name="subject" value="Math" /> <input type="checkbox" name="subject" value="Reading" /> <input type="checkbox" name="level" value="Science" /> <input type="submit" value="submit" /> </form> So... if I ticked PreK, Elem and Math, the resulting link would be: www.mysite.com?level=PreK&Elem&subject=Math The most complicated thing... how would I get place the "&" in between variables? Thank you once again. ~Wayne Hi guys, I have a list of products and I want to create a way to display similar products when a user views a particular product. I am thinking about doing it a certain way but need some advice on how to make it happen. I think I would do it like: Get the name of the existing product explode() the name by space" " e.g (explode(" ",$productname)); place the reuslts in a query that looks like: SELECT * FROM product WHERE name LIKE '%$arrayitem1% OR LIKE '%arrayitem2% OR LIKE '%arrayitem3%'' Im pretty fuzzy on the whole explode() part. Can anyone please give me some advice or point me in the direction of a similar tutorial/piece of code? Thanks heaps! Hi Guys, I am trying to get the below code to work - search form with textbox, dropdown and submit button. I found scripts on the web but the addition of the pagination just causes an error. I spent hours tweaking it but alas I cant see the solution. Perhaps someone could help please. I would love to see an example with 1 textbox, 2 dropdowns and a submit button or a link to such a tutorial. My googling has hit a dead end. In the below code I can run a search and the reults are listed on results.php. Results get screwed up when I go to pagination page 2,3,4 etc...I think i need to integrate $max somewhere in the Code: [Select] $data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'"); Thanks Code: [Select] <h2>Search</h2> <form name="search" method="post" action="results.php"> Seach for: <input type="text" name="find" /> in <Select NAME="field"> <Option VALUE="fname">First Name</option> <Option VALUE="lname">Last Name</option> <Option VALUE="result">Profile</option> </Select> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> //results.php <? // Grab POST data sent from form $field = @$_POST['field'] ; $find = @$_POST['find'] ; $searching = @$_POST['searching'] ; //This is only displayed if they have submitted the form if ($searching =="yes") { echo "<h2>Results</h2><p>"; //If they did not enter a search term we give them an error if ($find == "") { echo "<p>You forgot to enter a search term"; exit; } // Otherwise we connect to our Database mysql_connect("mysql.yourhost.com", "user_name", "password") or die(mysql_error()); mysql_select_db("database_name") or die(mysql_error()); // We preform a bit of filtering $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); //Now we search for our search term, in the field the user specified $data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'"); //This counts the number or results - and if there wasn't any it gives them a little message explaining that $anymatches=mysql_num_rows($data); if ($anymatches == 0) { echo "Sorry, but we can not find an entry to match your query<br><br>"; } //And we remind them what they searched for echo "<b>Searched For:</b> " .$find; } ?> <?php if(!isset($_REQUEST['pagenum'])) { $pagenum=1; } else { $pagenum=$_REQUEST['pagenum']; } //Here we count the number of results //Edit $data to be your query $rows = mysql_num_rows($data); //This is the number of results displayed per page $page_rows = 4; //This tells us the page number of our last page $last = ceil($rows/$page_rows); //this makes sure the page number isn't below one, or more than our maximum pages if ($pagenum < 1) { $pagenum = 1; } elseif ($pagenum > $last) { $pagenum = $last; } //This sets the range to display in our query $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; //This is your query again, the same one... the only difference is we add $max into it //$data = mysql_query("SELECT * FROM topsites $max") or die(mysql_error()); this works with pagination hmmm //This is where you display your query results //And we display the results while($result = mysql_fetch_array( $data )) { echo $result['fname']; echo " "; echo $result['lname']; echo "<br>"; echo $result['info']; echo "<br>"; echo "<br>"; } echo "<p>"; // This shows the user what page they are on, and the total number of pages echo " --Page $pagenum of $last-- <p>"; // First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page. if ($pagenum == 1) { } else { echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> "; echo " "; $previous = $pagenum-1; echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> "; } //just a spacer echo " ---- "; //This does the same as above, only checking if we are on the last page, and then generating the Next and Last links if ($pagenum == $last) { } else { $next = $pagenum+1; echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> "; echo " "; echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> "; } ?> Hey guys, I'm looking for some sort of tutorial on how to code a search script which would allow users to select number of results per page (and take care of the page 1, page 2, etc. links accordingly) as well as order them by date, most views, etc. I thought I'd be good to do this on my own but my script is looking very dirty and I'm pretty sure it isn't very secure.. anyone ever consult a tutorial with main guidance as how to code the kind of script I'm interested in?? thanks Good morning, I'm creating a content management system for my game. It's RPG style, so it contains many tasks, however, adding them is a bit complicated process and the system isn't simple. Therefore, simple web form is needed, but there are fields that depend on others. For instance, if quest type is k (which means killing task), a field asking how many different kinds of monsters need to be killed, then there should appear x fields with more specific information. To sum up, i need an example, advice or sth that would help me to create this type of html where new fields appear automatically depending on the chosen ones. Thanks in advance, Karolis This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=343600.0 Hello guys, I'm trying to solve my hometask . I tried in many different ways to solve the task related with Advanced SQL Syntax. Please check the pic below: The Quiz is asking for make location appear in all uppercase letters, the character associated with a given ASCII value and the table is people. I tried the last one in this way: SELECT CONCAT(name,'is from',strtoupper(location),' The ASCII character of the number ',number,' is ',ASCII(number)) FROM `people` WHERE 1 but it doesn't work. Can someone help me? Thanks Hi Guys, just been writing this script up but for some reason the validation is messing up creating account, apparently nothing is valid that i put in Any ideas? the .phps are below and the .inc ar all below. Also when i try to log in with an existing user it says i cant because the username or password is incorrect which is isn't. Any help will be much appreciated. Enlighten Code: [Select] <?php /* File: login_reg_form.inc * Desc: Contains the code for a web page that displays two html forms, side by side. One is a login form, and the second is a registration form. */ include("functions.inc"); ?> <head><title>Customer Login page</title> <style type='text/css'> <!-- label { font-weight: bold; float: left; width: 27%; margin-right: .5em; text-align: right; } legend { font-weight: bold; font-size: 1.2em; margin-bottom: .5em; } #wrapper { margin: 0; padding: 0; } #login { position: absolute; left: 0; width: 40%; padding: 1em 0; } #reg { position: absolute; left: 40%; width: 60%; padding: 1em 0; } #field { padding-bottom: .5em; } .errors { font-weight: bold; font-style: italic; font-size: 90% color: red; margin-top: 0; } --> </style> </head> <body style="margin: 0"> <?php $fields_1 = array("fusername" => "User Name", "fpassword" => "Password"); $fields_2 = array("user_name" => "User Name", "password" => "Password", "email" => "Email", "first_name" => "First Name", "last_name" => "Last Name", "street" => "Street", "city" => "City", "county" => "County", "post_code" => "Post Code", "phone" => "Phone", "fax" => "Fax"); ?> <div id="wrapper"> <div id="login"> <form action=<?php echo $_SERVER['PHP_SELF']?> method="POST"> <fieldset style='border: 2px solid #000000'> <legend>Login Form</legend> <?php if (isset($message_1)) { echo "<p class='errors'>$message_1</p>\n"; } foreach ($fields_1 as $field => $value) { if(preg_match("/pass/i", $field)) $type = "password"; else $type = "text"; echo "<div id ='field'> <label for='$field'>$value</label> <input id='$field' name='$field' type= '$type' value='".@$$field."' size='20' maxlength='50' /> </div>\n"; } ?> <input type="submit" name="Button" style='margin-left: 45%; margin-bottom: .5em' value="Login" /> </fieldset> </form> <p style='text-align: center; margin: 1em'> If you already have an account, log in.</p> <p style='text-align: center; margin: 1em'> If you do not have an account, register now.</p> </div> <div id='reg'> <form action=<?php echo $_SERVER['PHP_SELF']?> method="POST"> <fieldset style='border: 2px solid #000000'> <legend>Registration form</legend> <?php if(isset($message_2)) { echo "<p class='errors'>$message_2</p>\n"; } foreach($fields_2 as $field => $value) { if (preg_match("/pass/i", $field)) $type="password"; else $type="text"; echo "<div id='field'> <label for='$field'>$value</label> <input id='$field' name='$field' type='$type' value='".@$$field."' size='40' maxlength='65' /> </div>\n"; } // end foreach field ?> <input type="submit" name="Button" style='margin-left: 45%; margin-bottom: .5em' value="Register"> </fieldset> </form> </div> </div> </body></html> ok here is my problem... for example: i have 1000 files with 15 entrys each. but in the last one there is only 8 entrys. (i want to make a pagination) i want to display 15 entrys on each page. also how to calculate from which entry to which should it read. (i have another text file in which is the number of total entrys in all files together) for the first page it should read the first 8 and from thefirst file and the first 7 from the second file to get 15 displayed on the page. i was tryin now for some hours, but i just can't figure it out. :S and i know that i will think how stupid i'm after i will see how it should be done. :S Hello, I am a novice with php and know close to nothing about JSON. I am using wordpress and have 2 calendar type plugins I want to work together. With one plugin the user inputs dates that they are booked. The other plugin is a date picker, the cool part is it allows you to black out dates using php before the date picker is built. So what I'm trying to do is get the data from what the user chose as booked, and not let a user on the front end pick any of the booked dates with the date picker. Hope that makes sense. The problem is the formatting of the data with the date booked plugin. This is what is in the field Code: [Select] a:1:{s:9:"calendars";a:1:{i:1;a:4:{s:12:"calendarName";s:21:"Availability Calendar";s:12:"calendarJson";s:59:"{"year2011":{"month12":{"day25":"booked","day6":"booked"}}}";s:11:"dateCreated";i:1324333825;s:12:"dateModified";i:1324333825;}}} I chose 2 dates to be booked in that example: dec 25 2011, and dec 6 2011. I have no idea how I would parse the above data into what I need. I don't really need the data from the other parts of the above code, just the dates. The others are field descriptions and etc. that wordpress uses. Incidentally, this is what the date picker setup would look like in the above senario: Code: [Select] $myCalendar->setSpecificDate(array("2011-12-06","2011-12-25"), 0, ''); Hello, I need help. I have a file test.txt, and this content in it: Code: [Select] [group 1] immediate=yes links=1,14,12,22 avaliable=no [group 2] immediate=no links=1,4,14,22 Now, I also have a WEB app with database, where I can change both groups, and the only this missing is, that when a change is made in on group, that I write it to the file test.txt. I know how to write to files, but here is the problem (2 examples): If I change on the WEB page below group 1 from immediate=yes to immediate=no -> how can I change this in the file (just for group 1). If I change on the WEB page below group 2 to avaliable=yes -> how can I add this line in the file below group 2 To summarize - I have problems detecting where the group 1 or group 2 starts and end - so I only change for each group. hey now I have to put the pieces together. Hi, I am looking to fetch the contents of https://bb.kdg.be/webapps/blackboard/content/listContent.jsp?course_id=_98522_1&content_id=_1288143_1 in PHP However, the page works with cookies, javascript redirect, and https. Therefor I cannot manage to make a local copy of the actual contents. I have tried with curl, file_get_contents, file, fopen, and numerous user-written functions online, none of which worked. Please make sure you TEST your answer before posting it, because it is probably not as obvious as it seems! Thanks! Hey everyone, I am looking to create an advanced contact form but need someone to point me in the right direction. I need to create a customer service form and would like it to contain the usual fields e.g. Name, Email etc. but also want to include a drop down field which has a couple of options e.g. Sales Enquiry, Order Status etc. Once one of those options has been selected e.g. Order Status, I then want a further section of the form to appear where the user can input data such as their order number, order date etc. Does anyone have any ideas on how to achieve this or know of any demos/scripts which they think might help. The result pages is supposed to have pagination like google help me please
Hi, My output in html is : Code: [Select] <uL><li id="B1"></li> <li id="B2"></li> <li id="B3"></li> <li id="B4"></li> <li id="B5"></li> <li id="B6"></li> <li id="B7"></li> <li id="B8"></li> <li id="B9"></li> <li id="B10"></li> <li id="B11"></li> <li id="B12" class="active"></li> <li id="B13" class="no"></li> <li id="B14" class="no"></li> <li id="B15" class="no"></li> <li id="B16" class="no"></li> <li id="B17" class="no"></li> <li id="B18" class="no"></li> <li id="B19" class="no"></li> <li id="B20" class="no"></li> </ul> If MySQL query result is equal to `6`, then `<li>` tag with `id` equal to "`B12`" should have class "`active`". All the `<li>` elements occuring after this active element should have class "`no`". This shows images of horizontal rating between `0` and `10` and between 0.5 Example : 0 .5 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5 8 8.5 9 9.5 10 In the example above elements from `0` to `5` will be blue, element `6` will be white and elements from `7` to `10` are black. How could I generate this using PHP and/or MySQL? Thanks, XXXX chirstmas XXXX Hello I've got a php script that generates random number every page refresh Code: [Select] <?php srand ((double) microtime( )*1000000); $random_number = rand(1000,5000); echo "$random_number"; ?> I would like some advanced things... first of all i don't want the random number to be generated every page refresh I would like something like... based on pc hour example: - between 12 AM and 5 AM it will generate a number from 500 to 1000 randomly but the number should change also to a determined time i set such as every 5 minutes and not everytime i refresh the website page - betweem 5 AM - 2 PM will generate a number from 3500 to 8000 and this number will change every 5 minutes too with one that exists between this interval - and so on Would appreciate if someone could help me with that Thanks in advance Hello, here is the PHP situation I'm in. I'll type it out in pseudo-ish code first. <?php $multi_array = array( array("cats", "dogs", "sheep", "walruses"), array("jazz", "larva", "pencils", "derp"), array("to", "much") ); $string = array("the", "cats", "ate", "too", "many", "pencils"); $count = count($string); for($i = 0; $i < $count; ++$i) { if($string[$i] IN ARRAY $multi_array) { THEN replace $string[$i] with a random word in the sub array of the multi-array for example, if $string[$i] is cat, then it would replace it with any of the words in the sub array of multi-array, so it could be "cats", "dogs", "sheep", or "walruses" } } ?> I have no idea how to do this and tried thinking of ways for an hour and a half. This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=358297.0 |