The realization of classification sort of PHP text database

Source: Internet
Author: User
Tags explode php file php programming sort server memory

In PHP programming, if you use a text database, you may have a headache for sorting problems. The following sunny little describes how to use PHP to combine JavaScript scripts to implement this functionality: The following figure, when the user clicks on the corresponding heading category, if the category is different from the original category, according to the corresponding category of the same ascending and descending order; When the click category is the same as the original category, it is arranged in the reverse order of the category. (That is, if you put the mouse in the "singer" title, you will be prompted "in [singer] [ascending order]". )

The format of the text database in the example is as follows: Each line of information is saved in one file, the file content format is "song name | singer | size". For example, the file "find a word instead of. txt" content is "find a word instead of | Timi Zhuo |48554", each file has only one row, and saved in the "Data" directory.

Sorting the idea is: will be sorted in the category of information read to the $sort_array[] array, the $sort_array[] to arrange, get the new array of subscript $key_array[], and then click the array of the subscript to read the file.

Here we write this PHP file.

?
Sort categories to default values:
if (empty ($sorted)) $sorted = "Musicname"; Sort Categories
if (empty ($dir)) $dir = 1; Lifting sequence
?>
<HTML><HEAD>
<meta http-equiv= "Content-type" content= "text/html;charset=gb2312" >
<TITLE> Songs List </TITLE>
<style type= "Text/css" >
<!--
TD{FONT-SIZE:9PT}
-->
</style>
</HEAD>
<BODY>
<!--Here are JavaScript scripts that combine PHP into-->
<script language= "JavaScript" >
Sorted= "<?echo $sorted;? > "; Reading category information for the address bar
Dir=parseint ("<?echo $dir;? > "); Read the Ascending and descending order information of the address bar
function SortBy (obj) {//is used to specify the link address of the clicked title bar, passing sorting information
Sorted==obj.ename? Mydir=-dir:mydir=dir; If the sort category is the category you clicked on, change the lift order
Obj.href=location.pathname+ "sorted=" +obj.ename+ "&dir=" +MYDIR; Specify a link address
}
Functions Tips (obj) {//This function defines the links and the status bar hints
Sorted==obj.ename? Mydir=-dir:mydir=dir;
obj.href= "SortBy: Press [" +obj.chname+ "] [" + "] + (mydir>0?) Descending ": Ascending") + "] arrange";
Obj.title= "Press [" +obj.chname+ "] [" + (mydir>0?) Descending ": Ascending") + "] arrange";
/* Note the two-style format. First, the first line just specifies that the link's path is "SortBy: Press ... arrangement, but the word "http://" does not appear in the status bar.
In fact, as long as the link with the English word string without spaces and special characters plus ":", the browser that this is not a relative path, the status bar will be displayed on the link path (even if it is illegal).
Second, on the right side of the upper two, a ternary operator ("?:") is inserted, and it appears that the statements in parentheses are not in JavaScript syntax. This sentence is relatively rare.
Of course you can also write If...else. 's format.
The following PHP program is used in a similar format for readers ' reference.
*/
}
</script>
<table align=center bgcolor= "#0099CC" border=0 cellpadding=0 cellspacing=0 width=500>
<TBODY>
<TR>
&LT;TD height= align= "center" bgcolor= #799ADD ><font color= "#FFFFFF" > Songs list </font></TD>
</TR>
<TR>
<TD> <table border=0 cellpadding=1 cellspacing=1 width= "100%" >
<TBODY>
<tr bgcolor= #becfed >
?
$SORTDOWNSRC = "Img/sortdown.gif"; A picture of the down arrow, displayed next to the heading in descending order
$SORTUPSRC = "Img/sortup.gif"; A picture of the up arrow, displayed next to the heading in ascending order
function PrintTitle () {//The functions are used to output caption links and Arrow pictures
Global $ename, $sorted, $dir, $sortdownsrc, $SORTUPSRC, $chname;
echo "<a href= ' onmouseover= ' tips (this) ' onmousedown= ' SortBy (this) ' Chname= ' $chname ' ename= ' $ename ' ><font Color= ' #0000FF ' > $chname </font></a>;
/* The top output defines two attributes "Chname" and "ename" for the <a> label itself. "Chname" is the Chinese name of the title, used for display; "Ename" is the English name of the title, used to classify
They are used directly in the SortBy () function in the JavaScript code above. This method may be worth the reader's reference.
*/
if ($sorted = = $ename) {//If the heading category is the sorted category, display the arrow picture
echo "0 $sortdownsrc: $sortupsrc). "' > ";
The format of the insert ternary operator on the previous line is described earlier.
}
}
?> <!--output title bar table-->
? $ename = "Musicname"; $chname = "song name";?> <td align=center>. PrintTitle ();?></td>
? $ename = "Singer"; $chname = "singer";?> <td align=center>. PrintTitle ();?></td>
? $ename = "Musicsize"; $chname = "Size";?> <td align=center>. PrintTitle ();?></td>
?
$i = 1;
The following reads the file data in the database folder.
$open =opendir ("data/");
First time Read
while ($filename =readdir ($open)) {
if (FileSize ("data/". $filename) >0) {
$filecontent =file ("data/". $filename);
Divide the read data into variables:
$msgs _info=explode ("|", $filecontent [0]);
$musicname = $msgs _info[0];
$singer = $msgs _info[1];
$musicsize = $msgs _info[2];
/* The value of the class to be sorted is read to $sort_array[] to sort, with a double variable, that is, the category value passed from the address bar as a variable.
Therefore, the above three-variable names must correspond to the "ename" of each link title:
*/
$sort _array[$i]=${$sorted};
Read the corresponding filename to $filename_array[] to correspond to the sorted subscript.
$filename _array[$i]= $filename;
$i + +;
}
}
$totalfile =count ($sort _array); Total Files
if ($totalfile!=0) {
The following functions rank $sort_array[] in ascending or descending order:
$dir >0? Asort ($sort _array): Arsort ($sort _array);
/* Read the New Order after the arrangement:
Note that the subscript for $sort_array[] starts at 1 because the function cannot handle 0 as subscript, and 0 in PHP is false and interrupts the loop in the condition of for ().
*/
$i = 1;
for (Reset ($sort _array); $key = key ($sort _array); next ($sort _array)) {
Order $key _array[] to subscript an array of rows
$key _array[$i] = $key;
$i + +;
}
for ($i =1; $i <= $totalfile; $i + +) {
Because $key_array[] is a row of array subscript, apply it to $filename_array[] in the line:
Read the file for the second time, read it in rows, and output:
$filecontent =file ("data/". $filename _array[$key _array[$i]));
$msgs _info=explode ("|", $filecontent [0]);
$musicname = $msgs _info[0];
$singer = $msgs _info[1];
$musicsize = $msgs _info[2];
echo "<tr bgcolor= #ebf1f7 > \ n";
echo "<td align=left>&nbsp; $musicname </td> \ n";
echo "<td align=left>&nbsp; $singer </td> \ n";
echo "<td align=left>&nbsp; $musicsize kb</td> \ n";
echo "</TR> \ n";
}
}
Closedir ($open);
?> </TR>
</TBODY>
</TABLE></TD>
</TR>
</TBODY>
</TABLE>
</BODY>
</HTML>

OK, our program has been written. The program read two times the file, which may be slower, of course, you can change the contents of the file into an array in the first reading, so that the second reading, but this need to occupy more server memory, for the sake of security to sacrifice a little speed.

Today, Xiao Yang not only introduces the sorting method of text database, but also introduces the programming method of PHP and JavaScript in detail, the special application of ternary operator, the display rule of status bar and the application of HTML element custom attribute. OK, run the PHP file on the server just now to see. The attentive reader will soon find out what is missing. Yes, it's a paging display! Unfortunately, limited to space, here is not introduced, you go to perfect it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.