An example of using the template for output buffering + database extraction and display

Source: Internet
Author: User
? Php // This is a simple cache example: // on this page, you need to extract N records from several tables to display them. Because database operations are frequent, // The cache method is used, the value remains unchanged within 24 hours. if the value is exceeded, it is regenerated. // This example also uses the template function to generate pages. // There are two methods to generate records immediately in this article.
// This is a simple cache example:

// On this page, N records need to be extracted from several tables and displayed immediately, because database operations are frequent,

// Therefore, the cache method is used. if the value is exceeded within 24 hours, it is regenerated.

// This example also uses the template function to generate pages.

// There are two methods to generate records immediately in this article to adapt to the continuous and discontinuous index IDs, respectively,

// It is for reference only. For more information, see.

// Steeven@kali.com.cn



// Cache File name:

$ Cache_file = 'index. Inc ';



If (time ()-filemtime ($ cache_file) <3600*24 ){

// If the modification time of the cached file does not exceed 24 hours

Include ($ cache_file );

Exit;

}



// Home page of learn

Require "../lib/template. php ";

Require "../lib/common. php ";

Require "../lib/config. php ";

// Require "common. php ";

// Require "config. php ";



$ IllnessCount = 5;

$ AssayCount = 5;

$ RadiologyCount = 5;



$ IllnessList = & get_rand_field ('illness ', 'name', $ IllnessCount );

$ AssayList = & get_rand_field ('asser', 'name', $ AssayCount );

$ RadiologyList = & get_rand_field ('radiology ', 'name', $ RadiologyCount );





Function & get_rand_field ($ table, $ field, $ n ){

// $ Table is identified by Id

$ Q = "select count (*) from $ table ";

$ Total = mysql_result (mysql_query ($ q), 0, 0 );

$ N = min ($ total, $ n );



// If the primary index of the data is consecutive by Id:

$ Id_list = array ();

While (sizeof ($ id_list) <$ n ){

Mt_srand (double) microtime () * 1000000 );

$ Rand = mt_rand (0, $ total-1 );

If (in_array ($ rand, $ id_list ))

Continue;

$ Id_list [] = $ rand;

}

$ Q = "select Id, $ field from $ table where Id in (". implode ($ id_list ,',').")";

$ R = mysql_query ($ q) or error_die ("query error ");

Return $ r;



/* If the primary Index Id is not consecutive: (intermediate records may have been deleted)

For ($ I = 0; $ I <$ n; $ I ++ ){

$ Id_lists = implode ($ id_list ,',');

Mt_srand (double) microtime () * 1000000 );

$ Rand = mt_rand (0, $ total-1 );

$ Q = "select Id, $ field from $ table ";

If ($ id_lists! = ')

$ Q. = "where Id not in ($ id_lists )";

$ Q. = "limit $ rand, 1 ";

$ R = mysql_query ($ q) or error_die ("query error ");

$ Id_list [] = mysql_result ($ r, 0, 0 );

Mysql_data_seek ($ r, 0 );

$ R_list [] = mysql_fetch_object ($ r );

$ Total --;

}

Return $ r_list;

*/

}



// Output to html

$ T = new Template (".", "keep ");

$ T-> set_file (array ("page" => "index. ihtml "));



$ T-> set_var (array (

"EmptyBlock" => ""

));



$ T-> set_block ("page", "Loop_Illness", "");

Foreach ($ IllnessList as $ row ){

$ T-> set_var (array (

"RandIllness" => html_echo ($ row-> Name ),

"IllnessId" => $ row-> Id,

));

$ T-> parse ("a", "Loop_Illness", true );

}

If (sizeof ($ IllnessList) = 0)

$ T-> parse ("a", "EmptyBlock", true );



$ T-> set_block ("page", "Loop_Assay", "loop_assay ");

Foreach ($ AssayList as $ row ){

$ T-> set_var (array (

"RandAssay" => html_echo ($ row-> Name ),

"AssayId" => $ row-> Id,

));

$ T-> parse ("loop_assay", "Loop_Assay", true );

}

If (sizeof ($ AssayList) = 0)

$ T-> parse ("loop_assay", "EmptyBlock", true );



$ T-> set_block ("page", "Loop_Radiology", "loop_radiology ");

Foreach ($ RadiologyList as $ row ){

$ T-> set_var (array (

"RandRadiology" => html_echo ($ row-> Name ),

"RadiologyId" => $ row-> Id,

));

$ T-> parse ("loop_radiology", "Loop_Radiology", true );

}

If (sizeof ($ RadiologyList) = 0)

$ T-> parse ("loop_radiology", "EmptyBlock", true );



// Output the new page

$ T-> pparse ("out", array ("page "));



// Save the buffer page

$ Fp = fopen ($ cache_file, 'w ');

Fputs ($ fp, $ t-> subst ("out "));

Fclose ($ fp );

? &
Related Article

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.