05. สร้างเนื้อให้บล๊อก (Generate the block content)
Submitted by wd on Wed, 2008-01-16 12:16
มอดูลนี้ เราจะสร้างรายการของเนื้อหา (nodes) ของวันนี้ในสัปดาห์ก่อน เวลาเราจะดึงรายการมา เราดูจากเวลาที่เนื้อหาถูกสร้าง โดยเราทำในรูปวินาที (ดู php เรื่องเวลา)
<?php
/**
* Generate HTML for the onthisdate block
* @param op the operation from the URL
* @param delta offset
* @returns block HTML
*/
function onthisdate_block($op='list', $delta=0) {
// listing of blocks, such as on the admin/block page
if ($op == "list") {
$block[0]["info"] = t('On This Date');
return $block;
} else if ($op == 'view') {
// our block content
// Get today's date
$today = getdate();
// calculate midnight one week ago
$start_time = mktime(0, 0, 0,
$today['mon'], ($today['mday'] - 7), $today['year']);
// we want items that occur only on the day in question, so
// calculate 1 day
$end_time = $start_time + 86400;
// 60 * 60 * 24 = 86400 seconds in a day
}
//We'll use db_query() to get the records (i.e. the database rows) with our SQL query
$result = db_query("SELECT nid, title, created FROM {node} WHERE created >= '%s' AND created <= '%s'", $start_time, $end_time);
// content variable that will be returned for display
$block_content = '';
while ($links = db_fetch_object($result)) {
$block_content .= l($links->title, 'node/'. $links->nid) .'<br />';
}
// check to see if there was any content before setting up
// the block
if ($block_content == '') {
/* No content from a week ago. If we return nothing, the block
* doesn't show, which is what we want. */
return;
}
// set up the block
$block['subject'] = 'On This Date';
$block['content'] = $block_content;
return $block;
}
?>- สร้าง query ใช้ฟังก์ชั่น
db_query()โดยให้ชื่อตารางในฐานข้อมูลอยู่ในรูป{node}ดูรายละเอียดจาก Table Prefix (and sharing tables across instances) - เวลาดึงจริง ใช้ฟังก์ชั่น
db_fetch_object() - พอดึงมาปุ๊ป ก็สร้างรายการเป็นลิงก์ ด้วยฟังก์ชั่น
l()ให้อยู่ในรูปของ<li><a href="node/nid">title</li>
- Printer-friendly version
- Log in or register to post comments
- 5320 reads







Recent comments