Use PHP and coreseek to implement precise recipe search function
Use PHP and coreseek to achieve accurate recipe search function
Overview:
In today's fast-paced life, more and more people are beginning to pay attention to their dietary health. Finding the right recipe becomes a need. This article will introduce how to use PHP and coreseek search engine to implement accurate recipe search function to help users easily find recipes that meet their needs.
Preparation work:
Before we start, we need to prepare some tools:
- PHP environment: Set up a PHP environment locally or on the server to ensure that PHP scripts can be run.
- MySQL database: used to store recipe data.
- Coreseek Search Engine: A full-text search engine based on Sphinx. The latest version can be downloaded from its official website.
Core idea:
- Prepare recipe data and import it into the MySQL database.
- Configure and start the Coreseek search engine.
- Use PHP to write the search function code to realize user input and display of search results.
Step 1: Prepare recipe data and import it into the MySQL database
First, we need to create a database to store recipe data. You can use tools such as phpMyAdmin or directly execute the corresponding SQL statement on the command line to create it.
The SQL statements to create databases and tables are as follows:
CREATE DATABASE IF NOT EXISTS recipe;
USE recipe;
CREATE TABLE IF NOT EXISTS recipes (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) NOT NULL, ingredients TEXT NOT NULL, directions TEXT NOT NULL
);
Next, we can read the local recipe data by writing a PHP script and import it into the database. Assume our recipe data is saved in a text file called "recipes.txt" with the following format:
Title 1
Ingredients 1, Ingredients 2, Ingredients 3
Step 1 , Step 2, Step 3
Title 2
Ingredients 4, Ingredients 5, Ingredients 6
Step 4, Step 5, Step 6
The PHP script code is as follows:
//Open file
$handle = fopen('recipes.txt', 'r');
if ($handle) {
$title = ''; $ingredients = ''; $directions = ''; while (($line = fgets($handle)) !== false) { $line = trim($line); if ($line != '') { if ($title == '') { $title = $line; } elseif ($ingredients == '') { $ingredients = $line; } elseif ($directions == '') { $directions = $line; } if ($title != '' && $ingredients != '' && $directions != '') { // 将食谱数据插入数据库 $sql = "INSERT INTO recipes (title, ingredients, directions) VALUES ('" . $title . "', '" . $ingredients . "', '" . $directions . "')"; // 执行SQL语句 // ... // 重置数据 $title = ''; $ingredients = ''; $directions = ''; } } } fclose($handle);
}
?>
In the above code, we open the file named "recipes.txt" and read the contents line by line. Differentiate the titles, ingredients, and steps of different recipes by using blank lines. After each complete recipe is read, we insert it into the database.
Step 2: Configure and start the Coreseek search engine
First, unzip the downloaded coreseek compressed package to the specified directory. Enter the Coreseek directory, edit the configuration file "sphinx.conf", and configure the following content:
source recipe
{
type = mysql sql_host = localhost sql_user = root sql_pass = your_password sql_db = recipe sql_port = 3306 sql_query = SELECT id, title, ingredients, directions FROM recipes
}
index recipe
{
source = recipe path = /path/to/index/recipe docinfo = extern min_word_len = 1 charset_type = utf-8
}
searchd
{
listen = 9312 log = /path/to/searchd.log query_log = /path/to/query.log max_matches = 1000
}
In the above configuration, we defined a data source named "recipe" , a MySQL database was used to obtain recipe data. Then, an index named "recipe" was created and the related paths and configurations were set.
After saving and exiting the configuration file, use the following command to start the Coreseek search engine:
/path/to/coreseek/bin/searchd --config /path/to/coreseek/etc/ sphinx.conf
Step 3: Use PHP to write the code for the search function
Now, we can use PHP to write the code for the search function. Suppose we have a search form on our web page where users can enter keywords to search.
The HTML code is as follows:
The PHP script code is as follows (search.php):
// Get the keywords entered by the user
$keyword = $_GET['keyword'];
//Query database
$sql = "SELECT * FROM recipes WHERE MATCH('".$keyword."')";
//Execute SQL statement
// .. .
// Parse query results
// ...
// Display search results
// ...
?>
In the above code, we first get the keyword entered by the user and store it in the variable $keyword. Then, we use the MATCH keyword to query the database and obtain recipe data that matches the keyword. The specific database query and result parsing code is omitted here, you can implement it according to your own needs.
Finally, in the search results page, we can display the search results according to specific needs, such as using loop statements to traverse the queried data and display it to the user in the form of a list or card.
Summary:
This article introduces how to use PHP and coreseek search engine to achieve accurate recipe search function. By preparing recipe data, configuring and starting the Coreseek search engine, and writing relevant PHP code, we can easily implement a powerful recipe search function to help users quickly find recipes that meet their needs. At the same time, you can also expand and optimize the code according to your own needs to achieve a better user experience.
The above is the detailed content of Use PHP and coreseek to implement precise recipe search function. For more information, please follow other related articles on the PHP Chinese website!

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment
