PHP WeChat Public Development Notes (7)
PHP WeChat public development notes series
Date: 2014.9.6
This diary is to make up for yesterday, I didn’t have time to write yesterday;
Main functions implemented: fuzzy search, question and answer system, help system
Fixed: _SESSION id
Adjusted the verification mechanism
Things to note: mysql's syntax issues, mysql's fuzzy matching, and PHP's associative arrays seem to be twice as large as the original data if calculated using count (that is, there are only two data in the array, and count will be 4. This requires Verified again, it seems that this problem occurred when using foreach)
Learned: Array operations in PHP
2014.9.10 Organizing notes:
When I wrote this note, I was working on a feature, so I simply jotted down a few notes about my work at that time. Now that the feature is almost developed, I’m going to sort out the previous notes. At that time, I simply recorded the above sentences.
I remember that on the way to work that day, my classmate told me that the desired function was to enter the name of a certain city and get information about classmates in that city. In fact, this function is what I originally wanted to achieve with this WeChat public account, so that information retrieval can be easily carried out. Although at this stage, we may not have many people walking around and do not have that need. But chance cannot be ruled out, so I came up with this idea.
Fuzzy search:
For fuzzy search, it is basically based on database search. There should be a keyword here so that the service desk program can capture this keyword. I set it to view + XX to view information. For example, if I want to view information about Guangzhou, Then just reply "View Guangzhou".
Specific implementation:
The first is to capture keywords. This is similar to what was mentioned in Note 6. It is nothing more than using two string processing functions: strstr and str_replace to process the information entered by the user; the key is how to retrieve information from the database. At the beginning My idea is whether I can do fuzzy search in the database. If it doesn’t work, I can first get all the information and then compare it;
Option 1, fuzzy matching of database:
The information I found online shows that fuzzy search is supported, but I tried for several hours, but it didn’t work. I don’t know if my understanding of this fuzzy search is wrong. The syntax I found online is:
1. select from xxx (data table name) where xxx (field) like xxx (what we want to retrieve). The introduction given by netizens is that this syntax can retrieve the corresponding data from a given data table. The field value is the information we are looking for. But what we need to do here is multi-field viewing, that is, there are field information such as provinces and cities in the data table. I need to do fuzzy matching on these fields, so I started to check the information again;
2. select from xxx where concat('field_a','field_b','field_c') like xxx , here concat is equivalent to combining multiple fields and then doing fuzzy matching. I followed this , but still no success;
3. select from xxx where xxx.colum_name like xxx , this is also the information found. The change is that the field name is changed to: data table.colum_name. This is used to match, but it is still useless;
Specifically, it is useless. According to the information I printed out, the information was retrieved using mysql_query, but after processing the result with mysql_fetch_assoc or mysql_fetch_array, the corresponding array was not obtained. I don’t know what the situation is; what should also be noted here is: like According to the opinions provided by netizens, the latter fuzzy matching information needs to be written in the format of %retrieval% if it is Chinese characters, that is, there must be percent signs on both sides.
Although it does not meet my needs here, I still feel that if you want to operate the database well, you need to read the MySQL operation manual carefully.
Option 2: Get the information and then match and compare
This solution should be said to be very simple and easy to implement, because this is how I handled it in my mobile game project. In fact, when I first had the need for fuzzy retrieval, I also thought of this method. But I considered that firstly, getting information from the database requires accessing the database for retrieval, and then doing the comparison takes time. This may affect the performance, so I thought of searching online to see if there is a solution. But things went against expectations and did not meet expectations. This will be left to the future.
This solution is very simple, just get all the information first, and then compare it. An operation function of PHP array is used here to make the comparison very simple: in_array(); determine whether a value is in the array. Because the information we get from the database is processed into arrays in PHP. When I search for the word Guangzhou, I only need to determine whether the word Guangzhou appears in these arrays, so it is relatively simple. Specifically, this is how I implemented it:
vcv509C1xNDFz6KjrMi7uvPRrbu31eK49rb+zqzK/dfpo6zDv7TO0a27t7XDtb3Su7j2yv3X6dDFz6KjrMi7uvPIpbj6ztLDx9KqvOzL97XEJiMyMDU0MDvIpbbUscijrMalxeS1vcHLvs29q9XiuPbK/dfpt8W1vc7Sw8e1xL3hufvK/dfp1tDIpaOsyLu689TauvPD5rXEstnX99bQ1/a94bn7tKbA7b7Nv8nS1MHLoaM8YnI+CtLytMu52NPaxKO6/bzsy/fV4rj2uabE3LXjvs3V4tH5yrXP1sHLoaM8YnI+Cjxicj4KPHN0cm9uZz7OyrTwz7XNs6O6PC9zdHJvbmc+PGJyPgrU2rm5y7zEo7r9vOzL97XEyrG68qOsztLSss/ro6zKx7fxxNzFqrj2uabE3MC0sO/W+s2s0ae72NLku9jS5LTz0ae1xLarzveho9PQwcvV4rj2z+u3qNauuvOjrM7SubnLvMHL0ru49r3Q1/bOyrTwtcS5psTco6y8tNPDu6e72Li0zsq08KO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http://www.2cto.com/uploadfile/Collfiles/20140912/2014091209043527.png" alt="">
By passing an array and then processing it, you can easily reply to the image and text link.
Let’s talk about the link that jumps after clicking:
Because I didn't understand how to get that link at all (I had no idea at all), and then I thought it should be a file, and this link was the storage address of the file. Then I just wrote a file in PHP, put it in SAE Storage, put this link here, and tested it, and the effect was like this.
When I often browse the web, I see that most of the web pages are html, so I think about how to generate html files, how to generate html files after searching online, and how to generate web pages online, and save them as html in word, and then go home. Again using dreamweaver. Later, the html file was generated, but it did not meet the needs of mobile phone browsing very well. It should be said that it did not look like a web page at all. Maybe I should be able to have a little basic knowledge of web design. There are a lot of template materials on the Internet.
Later, I accidentally saw some documents made by my classmates in the material library of the public platform, and I thought, could I directly use this material library to process the help document I wanted to use, and directly create a new material with the content in it? The content is the information of the help document. As soon as I said it, I added the directly written help document to my newly created material, then saved it and tested the effect. I am very satisfied with the result. It is the effect I want. It looks like a web page opened on a mobile phone. Because I have a bit of obsessive-compulsive disorder, I always have to adjust the format, so I was busy until almost 12 o'clock that day before I tidied it up. That day was Friday, and it was almost 8 o'clock when I returned to the rented house. I tidied it up a little after I came back. I was sorting out the public platform, and then I was busy until about twelve o'clock. Although tired, but very happy.
Note on PHP:
1. I encountered a problem during development today. There was a problem with $_SESSION: the saved information is accessible to every user. At that time, when searching for information on the Internet, in order to make the information obtained by each user inconsistent, it was necessary to set the ID, that is, using a unique user information to set the ID, so as to ensure that the information obtained by each user was inconsistent:
session_id() Use this function to set the id
Because the received information obtained by the WeChat official account contains a message that represents the user who sent the message, and this can be used as the ID to make the value unique, so I set the ID based on the user's information, but something went wrong later. At first I thought it was a problem with this mechanism and that I was using it improperly. So the calling order of the functions was adjusted:
It was originally
session_id();
session_start();
With this calling sequence, I wondered if the order was wrong, so I changed it, but the result was still wrong. Later, I carefully looked at the ID I set, and found out that I had written the user information wrong, and I didn’t distinguish between uppercase and lowercase letters. It was really careless that killed people.
The user information obtained from the WeChat server is as follows:
$postObj->FromUserName
And I use it like this $postObj->fromUserName
But I never found it. . . Eh. . .
2. PHP array:
When using an associative array, if you use foreach to loop, the number of loops will be twice the count size. I encountered this problem when I was doing fuzzy search. At that time, I did not use in_array for matching, but used a loop array for matching. The information found on the Internet said that associative arrays use foreach for matching, so that the key value can also be obtained:
foreach ($array as $key => $value)
{
if($array[$key] == xxx)
$res_array[] = $array[$key];
}
When I did this at that time , the number of times of this loop is twice the size of the array, I printed the key, and it showed that both the associated key and the numerical key were printed. That is, the first time the numerical key is printed, and the second time the corresponding associated key is printed, the same element will be printed twice. So when I do the matching, I get two identical arrays. I don't know if there is something wrong with my usage.
http://www.bkjia.com/PHPjc/879184.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/879184.htmlTechArticlePHP WeChat Public Development Notes (7) PHP WeChat Public Development Notes Series Date: 2014.9.6 This diary is a supplement Yesterday, I didn’t have time to write yesterday; Main functions implemented: fuzzy search, question...