Jump to content
Larry Ullman's Book Forums

jpokusa

Members
  • Content Count

    16
  • Joined

  • Last visited

Community Reputation

0 Neutral

About jpokusa

  • Rank
    Newbie
  1. So in my current code I need to replace $keyword with $search_term. Is it an easy fix to my current code? Thank you for your help
  2. Hi HartleySan, Here is the page http://www.trueacewebdesign.com/animal-search/main.html I created the two tables as you mentioned. I am not able to get any result for the query when I type in rabbits. I put you code in my page. Do I need to change anything besides just putting in your query code? <?php /************************************************************************************** * Main Search Page - search.php * Author: Your Name <email@something.com> * This file searches the database **************************************************************************************/ //Get variables from config.php to connect to mysql server require 'config.php'; // connect to the mysql database server. mysql_connect ($dbhost, $dbusername, $dbuserpass); //select the database mysql_select_db($dbname) or die('Cannot select database'); //search variable = data in search box or url if(isset($_GET['search'])) { $search = $_GET['search']; } //trim whitespace from variable $search = trim($search); $search = preg_replace('/\s+/', ' ', $search); //seperate multiple keywords into array space delimited $keywords = explode(" ", $search); //Clean empty arrays so they don't get every row as result $keywords = array_diff($keywords, array("")); //Set the MySQL query if ($search == NULL or $search == '%'){ } else { for ($i=0; $i<count($keywords); $i++) { $query = "SELECT a.* FROM articles AS a, keywords AS k WHERE a.id = k.article_id AND k.keyword = '$search_term';"; "ORDER BY content"; } //Store the results in a variable or die if query fails $result = mysql_query($query) or die(mysql_error()); } if ($search == NULL or $search == '%'){ } else { //Count the rows retrived $count = mysql_num_rows($result); } //If search variable is null do nothing, else print it. if ($search == NULL) { } else { echo "You searched for <b><FONT COLOR=\"blue\">"; foreach($keywords as $value) { print "$value "; } echo "</font></b>"; } echo "<p> </p><br />"; echo "</center>"; //If users doesn't enter anything into search box tell them to. if ($search == NULL){ echo "<center><b><FONT COLOR=\"red\">Please enter a search parameter to continue.</font></b><br /></center>"; } elseif ($search == '%'){ echo "<center><b><FONT COLOR=\"red\">Please enter a search parameter to continue.</font></b><br /></center>"; //If no results are returned print it } elseif ($count <= 0){ echo "<center><b><FONT COLOR=\"red\">Your query returned no results from the database.</font></b><br /></center>"; //ELSE print the data in a table } else { //Table header echo "<center><table id=\"search\" bgcolor=\"#AAAAAA\">"; echo "<tr>"; echo "<td><b>Results:</b></td>"; echo "<tr>"; echo "</table></center>"; //While there are rows, print it. while($row = mysql_fetch_array($result)) { //table background color = row_color variable echo "<table bgcolor=".$row_color.">"; echo "<tr>"; echo "<td>".$row['articles']."</td>"; echo "</tr>"; echo "<tr>"; echo "<td>"."</td>"; echo "</tr>"; echo "<tr>"; echo "<td>".$row['content']."</td>"; echo "</tr>"; echo "<tr>"; echo "<td>"."</td>"; echo "</tr>"; echo "<tr>"; echo "<td>"."</td>"; echo "</tr>"; echo "</table>"; $row_count++; //end while } //end if } echo "</body>"; echo "</html>"; if ($search == NULL or $search == '%') { } else { //clear memory mysql_free_result($result); } ?>
  3. Thank you for your help so far. Typing more than one word is fine. Here is my table structure above. I am still a newbie at php and I understand that this may not be the most secure and proper way of doing this basic search engine. I have one table called (table_name) I have three fields in my table. The field called hotwords is queried to display my article and content field. Really basic. Here is my query //Set the MySQL query if ($search == NULL or $search == '%'){ } else { for ($i=0; $i<count($keywords); $i++) { $query = "SELECT * FROM table_name " . "WHERE hotwords LIKE '%".$keywords[$i]."%'". " ORDER BY article"; } I believe I need to replace LIKE with AND. When I tried it I couldn't search anyword. Or I need to do something such as = keywords. I am so close and I know it is simple. below is my full code. <?php /************************************************************************************** * Main Search Page - search.php * Author: Your Name <email@something.com> * This file searches the database **************************************************************************************/ //Get variables from config.php to connect to mysql server require 'config.php'; // connect to the mysql database server. mysql_connect ($dbhost, $dbusername, $dbuserpass); //select the database mysql_select_db($dbname) or die('Cannot select database'); //search variable = data in search box or url if(isset($_GET['search'])) { $search = $_GET['search']; } //trim whitespace from variable $search = trim($search); $search = preg_replace('/\s+/', ' ', $search); //seperate multiple keywords into array space delimited $keywords = explode(" ", $search); //Clean empty arrays so they don't get every row as result $keywords = array_diff($keywords, array("")); //Set the MySQL query if ($search == NULL or $search == '%'){ } else { for ($i=0; $i<count($keywords); $i++) { $query = "SELECT * FROM table_name " . "WHERE hotwords LIKE '%".$keywords[$i]."%'". " ORDER BY article"; } //Store the results in a variable or die if query fails $result = mysql_query($query) or die(mysql_error()); } if ($search == NULL or $search == '%'){ } else { //Count the rows retrived $count = mysql_num_rows($result); } //If search variable is null do nothing, else print it. if ($search == NULL) { } else { echo "You searched for <b><FONT COLOR=\"blue\">"; foreach($keywords as $value) { print "$value "; } echo "</font></b>"; } echo "<p> </p><br />"; echo "</center>"; //If users doesn't enter anything into search box tell them to. if ($search == NULL){ echo "<center><b><FONT COLOR=\"red\">Please enter a search parameter to continue.</font></b><br /></center>"; } elseif ($search == '%'){ echo "<center><b><FONT COLOR=\"red\">Please enter a search parameter to continue.</font></b><br /></center>"; //If no results are returned print it } elseif ($count <= 0){ echo "<center><b><FONT COLOR=\"red\">Your query returned no results from the database.</font></b><br /></center>"; //ELSE print the data in a table } else { //Table header echo "<center><table id=\"search\" bgcolor=\"#AAAAAA\">"; echo "<tr>"; echo "<td><b>Results:</b></td>"; echo "<tr>"; echo "</table></center>"; //While there are rows, print it. while($row = mysql_fetch_array($result)) { //table background color = row_color variable echo "<table bgcolor=".$row_color.">"; echo "<tr>"; echo "<td>".$row['article']."</td>"; echo "</tr>"; echo "<tr>"; echo "<td>"."</td>"; echo "</tr>"; echo "<tr>"; echo "<td>".$row['content']."</td>"; echo "</tr>"; echo "<tr>"; echo "<td>"."</td>"; echo "</tr>"; echo "<tr>"; echo "<td>"."</td>"; echo "</tr>"; echo "</table>"; $row_count++; //end while } //end if } echo "</body>"; echo "</html>"; if ($search == NULL or $search == '%') { } else { //clear memory mysql_free_result($result); } ?>
  4. That's exactly what I want to do. I just can't figure out how to write the code and put in my search.php
  5. No it doesn't matter if it is the first word in the article. For the rabbit article I only want that article to appear if you type in rabbits or if you type in tips or if you type in breeding. I do not want this article to pop up if you type in ra. Because I am only choosing important keywords in my database for each article. For this example ra is not a keyword I have chosen. I am looking for php code that will query for an exact match of a word typed in the search box. I hope that clears this up.
  6. Hi HartleySan Thanks but imdont think this is what I need. If you go to my search engine please see link in my first post and you type in the search box the two letters ra and hit the search button then unfortunately my article on rabbits appears. I want my rabbit article only to show up in the result only if the user types in the word rabbit. My search currently is to greedy. What I am asking help with is what type of php code to place in my current code. I believe it is just a basic line of preg match or something.
  7. I built a basic working search engine that will pull articles from my MYSQL tables. I want to make the search is stricter for getting my results. I want to add a preg_match line in my php code to make sure that the match has to be found at the start of the string. So in my search engine when the letter a is typed in the search box I dont want that to pull results. I want the search to be no less then three characters and it must match the word. So you have to type in cat exactly to retrieve my cat article. Here is link to my webpage. I thought adding this code $search = preg_match('/^ /', $search); would work but I have feeling I am approaching this wrong, http://www.trueacewebdesign.com/searchsite/main.html link to the files http://www.trueacewebdesign.com/searchsite.zip Here is my current PHP code. <?php /************************************************************************************** * Main Search Page - search.php **************************************************************************************/ //Get variables from config.php to connect to mysql server require 'config.php'; // connect to the mysql database server. mysql_connect ($dbhost, $dbusername, $dbuserpass); //select the database mysql_select_db($dbname) or die('Cannot select database'); //search variable = data in search box or url if(isset($_GET['search'])) { $search = $_GET['search']; } //trim whitespace from variable $search = trim($search); $search = preg_replace('/\s+/', ' ', $search); //seperate multiple keywords into array space delimited $keywords = explode(" ", $search); //Clean empty arrays so they don't get every row as result $keywords = array_diff($keywords, array("")); //Set the MySQL query if ($search == NULL or $search == '%'){ } else { for ($i=0; $i<count($keywords); $i++) { $query = "SELECT * FROM table_name " . "WHERE column3 LIKE '%".$keywords[$i]."%'". " ORDER BY column1"; } //Store the results in a variable or die if query fails $result = mysql_query($query) or die(mysql_error()); } if ($search == NULL or $search == '%'){ } else { //Count the rows retrived $count = mysql_num_rows($result); } //If search variable is null do nothing, else print it. if ($search == NULL) { } else { echo "You searched for <b><FONT COLOR=\"blue\">"; foreach($keywords as $value) { print "$value "; } echo "</font></b>"; } echo "<p> </p><br />"; echo "</center>"; //If users doesn't enter anything into search box tell them to. if ($search == NULL){ echo "<center><b><FONT COLOR=\"red\">Please enter a search parameter to continue.</font></b><br /></center>"; } elseif ($search == '%'){ echo "<center><b><FONT COLOR=\"red\">Please enter a search parameter to continue.</font></b><br /></center>"; //If no results are returned print it } elseif ($count <= 0){ echo "<center><b><FONT COLOR=\"red\">Your query returned no results from the database.</font></b><br /></center>"; //ELSE print the data in a table } else { //Table header echo "<center><table id=\"search\" bgcolor=\"#AAAAAA\">"; echo "<tr>"; echo "<td><b>Results:</b></td>"; echo "<tr>"; echo "</table></center>"; //While there are rows, print it. while($row = mysql_fetch_array($result)) { //table background color = row_color variable echo "<table bgcolor=".$row_color.">"; echo "<tr>"; echo "<td>".$row['column1']."</td>"; echo "</tr>"; echo "<tr>"; echo "<td>"."</td>"; echo "</tr>"; echo "<tr>"; echo "<td>".$row['column2']."</td>"; echo "</tr>"; echo "<tr>"; echo "<td>"."</td>"; echo "</tr>"; echo "<tr>"; echo "<td>"."</td>"; echo "</tr>"; echo "</table>"; $row_count++; //end while } //end if } echo "</body>"; echo "</html>"; if ($search == NULL or $search == '%') { } else { //clear memory mysql_free_result($result); } ?>
  8. Yes I am on a linux system as I am using Fatcow hosting. www.fatcow.com apache server. I updated the header with your instuctions but it didnt work. Randy Can you email me a link to your version of it live on your testing server. I would love to see how the launaguge drop down working on your end. Thank you JP
  9. Hello, I am on Chapter 17 Example Message Board. I went through this chapter and I am confuse and feel that I am missing something because from the screenshots in the book I do not have the following after I completed the chapter. The Language does not change when I try to switch languages from the drop down menu. The main question is what do I need to do to see that like in the book? Please note I am not receiving any error messages and I can post to a thread or create a new one. Please see the link to forum I created with the book files. http://www.trueacewebdesign.com/larry-php/daforum/index.php I also have zipped up the files and folders if you want to check the code. http://www.trueacewebdesign.com/daforum.zip I really appreciate the help. JP
  10. Solved!!!! I had to add a savepath before my session. I added the following code to the login.php and loggedin.php scripts session_save_path("/home/users/web/b2529/moo.trueace/cgi-bin/tmp"); session_start(); // Start the session. So the bottom line is make sure to check your hosting config if you have session issues. JP
  11. Hi Larry, I thought I read in your book that for sessions I may need to state the path to the folder session.save_path it looks like I have /var/phpsessions as a savepath. If you look at my php infor screenshot. Just to confirm I dont need to add anything about that to the book php files provided for session scripting?
  12. I have narrowed the issue down to the webhost. I have uploaded all the files to Godaddy Hosting and the Login form works now. So the issue has to do with Fatcow hosting. I will update this post after I find out what needs to be done on the Fatcow hosting side. In the meantime I have checked the sessions section in the php info file on fatcow. here is what i got. session
  13. Im using // Redirect: redirect_user('loggedin.php'); If you download my zip file you can see all that I am running.
  14. No error messages. I am not being redirected to loggedin.php. Check out my links above.
×
×
  • Create New...