In PHP programming, if you use a text database, you may have a headache with its classification and sorting problem. Next, Xiaoyang will introduce how to use PHP combined with JavaScript scripts to implement this function: As shown below, when the user clicks on the corresponding title category, if the category is different from the original category, it will be arranged in the same ascending and descending order as the corresponding category; when the clicked category is the same as the original category, If the categories are the same, the categories will be arranged in the reverse order of the original ones. (That is, if you put the mouse on the "Singer" title in the picture, you will be prompted to "Arrange by [Singer] [Ascending Order]".)
In the example, the format of the text database is: the information in each row is Each is saved in one file, and the file content format is "song name|singer|size". For example, the content of the file "Find a word to replace .txt" is "Find a word to replace | Zhuo Yiting | 48554". Each file has only one line and is saved in the "data" directory.
The idea of classification sorting is: read the information of the category to be sorted into the $sort_array[] array, arrange the $sort_array[] to get the newly arranged subscript array $key_array[], and then press the subscript The array can read the files in sequence.
Next we write this PHP file.
OK, our program has been written. The file is read twice in the program, which may be slower; of course, the content of the file can be stored in an array when it is read for the first time, thus eliminating the need for the second read, but this requires occupying With more server memory, you have to sacrifice a little speed for safety reasons.
Today Xiaoyang not only introduced to you the classification and sorting method of text database, but also introduced in detail the programming method combining PHP and JavaScript, the special application of the ternary operator, the display rules of the status bar and HTML elements Application of custom attributes, etc. Okay, let’s run the PHP file we just compiled on the server and take a look. The attentive reader will quickly discover what is missing. By the way, it’s displayed in pages! Unfortunately, due to space limitations, I won’t introduce it here. You can improve it yourself.