Tutorial for zblogphp random display of articles

 talklee

Tips: This article has exceeded two thousand three hundred and eighty-one No update in days, please note whether relevant content is still available!

Recently, when launching new themes, they were rejected because zblog Blogs have been completely prohibited from profiteering“ rand() ”Function, do not let the“ rand() ”The reason is:“ rand() ”Databases other than MySQL are not supported, and the speed will become very slow when there is a lot of database data.

 Zblogphp Random Display Article Tutorial Page 1

Quote official website explanation

1. Databases other than MySQL are not supported;

2. The data volume is tens of thousands, which is extremely slow.

Therefore, unless special circumstances exist, zblog blog will no longer allow applications with code containing rand() directly in the database to pass the audit in the future. Of course, it does not include rand() or mt_rand() functions in PHP, nor Math.random().

Why is it slow? I'm trying to use non computer terms to explain.

We compare a book to a database. General database queries, such as looking up a record with ID 1, usually get results directly based on the index. The use of an index is equivalent to the content of a book. If you look up the directory directly to get results, it will naturally be faster than turning from page to page. However, what about ORDER BY RAND()? It will first write a random number for each page of the book, which will turn over each page. Then, compare the order with the size, and sort the numbers on each page, which will turn the book over again. Then take the records with the lowest number. So that's why it's slow. (Of course, I haven't seen the internal implementation of MySQL. These two tasks are not necessarily independent of each other.) For professional explanations, please refer to the official documentation of MySQL:

You cannot use a column with RAND() values in an ORDER BY clause, because ORDER BY would evaluate the column multiple times. However, you can retrieve rows in random order like this:

mysql> SELECT * FROM tbl_name ORDER BY RAND(); ORDER BY RAND() combined with LIMIT is useful for selecting a random sample from a set of rows:

mysql> SELECT * FROM table1, table2 WHERE a=b AND c<d -> ORDER BY RAND() LIMIT 1000; RAND() is not meant to be a perfect random generator. It is a fast way to generate random numbers on demand that is portable between platforms for the same MySQL version.

The above is the explanation. I don't understand the computer terminology or the mysql official website documents, but it doesn't matter whether you understand them or not. The important thing is that you must modify them, otherwise you can't pass the review. So, the random display of birds came into being. The main idea of randomness is:

Bird random display code

1. First, check the maximum and minimum values of log_ID in the post table.

2. Get a random number between the maximum and minimum values.

3. Use this random number to look up the table.

The code is as follows

 Function Template ID_Theme_Get_Rand(){     global $zbp,$str,$order;     $str='';     $sql = $zbp->db->sql->Select(         $zbp->table['Post'],         array("MIN(log_ID)","MAX(log_ID)"),         array(             array('=', 'log_Type', '0'),             array('=', 'log_Status', '0'),         ),         array('log_PostTime' => 'ASC'),         null,         null     );     $array = $zbp->db->Query($sql);     $i=mt_rand($array[0]["MIN(log_ID)"],$array[0]["MAX(log_ID)"]);     $order = '';     $where = array(           array('=','log_Status','0'),           array('>','log_ID',$i)           );     $array = $zbp->GetArticleList(array('*'),$where,$order,array(5),'');           foreach ($array as $key=>$article) {                     if($key>5){                 break;             }             $str .='< li><a href="'.$article->Url.'">'.$ article->Title.'</ a></li>';         }                  return $str; }

It can be said that it has solved the urgent problem, but (this is the right one, which is not perfect) when your article ID is broken, there may be a few articles, which can be solved. After all, the zblog application center is a plug-in, which can modify the continuous ID without affecting the existing ID, which is perfect.

The problem can be solved, but not everyone is willing to use plug-ins. So I continued to search. Occasionally, I saw that the Flying Bird blog provided a method:

Random principle of flying birds

1. First, some data is extracted from the database and added to the array;

2. Randomly select the data in the array and display it.

The code is as follows

 Function template ID_rand ($num){     global $zbp,$str,$order;$ i;     $i = $num;     $str = '';     $arr = array();     $arand = array();     $order = array('log_ViewNums'=>'ASC');     $where = array(array('=','log_Status','0'));     $array = $zbp->GetArticleList(array('*'),$where,$order,array(20),'');$ arr = array_rand($array,$i); for($j=0;$j<$i;$j++){$arand[]=$array[$arr[$j]];}     foreach ($arand as $related) {         $str .=  "<li><a href=\"{$related->Url}\" title=\"{$related->Title}\" target=\"_blank\">{$related->Title}</a></li>";     }     return $str; }

In the code, the array_rand() function in PHP is used to obtain the key name of the array at random, not data. As for the code, it depends on the effect of later use. At present, it is OK. If there is a problem, I will give timely feedback... Sayunara!

Article copyright notice: unless otherwise noted Li Yang's Personal Blog For original articles, reprints or copies, please use hyperlinks and indicate the source.

Comment

Quick reply: expression:
Comment List (Yes three Comments, ten thousand three hundred and ninety People around)
 Netizen nickname: Mumu
Bathing V tourist  Google Chrome 55.0.2883.87  Windows 10 x64 sofa
2017-10-31 From Jiangsu reply
The blog is very beautiful. It has been added as a favorite. I will come here often in the future
Dare to ask God how to create a random article template page. You can visit a custom link like abc.com/random to randomly display an article. When using WP, many topics have this function, but it is not found on ZB
 Netizen nickname: talklee
talklee V Blogger  Sogou Explorer  Windows 7 x64
2017-11-01 From Liaoning reply
@Bathing I am also studying. I have seen the wp and will release the results when they are available.

Contents [+]