標籤:php經典項目 程式設計語言 php
本篇給出首頁左側導覽列及右部公告區的實現。
六、左側導覽列:
1、日曆:
這裡單獨一個php檔案,在顯示日曆的那個地方直接引用該檔案即可:
cale.php
<?php class calendar{ private $year,$month,$day; private $week=array("Sun","Mon","Tue","Wed","Thu","Fri","Sat"); private $_month=array( "01"=>"一月", "02"=>"二月", "03"=>"三月", "04"=>"四月", "05"=>"五月", "06"=>"六月", "07"=>"七月", "08"=>"八月", "09"=>"九月", "10"=>"十月", "11"=>"十一月", "12"=>"十二月" ); function setyear($year){ //設定年份 $this->year=$year; } function getyear(){ //獲得年份 return $this->year; } function setmonth($month){ //設定月份 $this->month=$month; } function getmonth(){ //獲得月份 return $this->month; } function setday($day){ //設定日期 $this->day=$day; } function getday(){ //獲得日期 return $this->day; } function OUT(){ //輸出日曆 $this->_env(); //設定顯示的日期 $week=$this->getweek($this->year,$this->month,$this->day); //獲得日期為星期幾 $fweek=$this->getweek($this->year,$this->month,1); //獲得此月第一天為星期幾 echo "<div style=width:255;font:9pt> <form action=$_SERVER[PHP_SELF] method='post' style='margin:0'> <select name='month' onchange='this.form.submit();'>"; for($ttmpa=1;$ttmpa<13;$ttmpa++){ //輸出12個月 $ttmpb=sprintf("%02d",$ttmpa); if(strcmp($ttmpb,$this->month)==0){ $select="selected style='background-color:#FAFDE2'"; }else{ $select=""; } echo "<option value='$ttmpb' $select>".$this->_month[$ttmpb]."</option>"; } echo " </select> <select name='year' onchange='this.form.submit();'>"; //輸出年份,前後10年 for($ctmpa=$this->year-10;$ctmpa<$this->year+10;$ctmpa++){ if($ctmpa>2050){ break; } if($ctmpa<1980){ continue; } if(strcmp($ctmpa,$this->year)==0){ $select="selected style='background-color:#FAFDE2'"; }else{ $select=""; } echo "<option value='$ctmpa' $select>$ctmpa</option>"; } echo "</select> </form><br/> <table border=0 align=center>"; for($Tmpa=0;$Tmpa<count($this->week);$Tmpa++){ //輸出星期的標題 echo "<td>".$this->week[$Tmpa]."</td>"; } for($tmpb=1;$tmpb<=date("t",mktime(0,0,0,$this->month,$this->day,$this->year));$tmpb++){ //輸出所有日期 if(strcmp($tmpb,$this->day)==0){ //獲得當前日期,並採用特色顏色做為標記 $flag=" bgcolor='#FF3366'"; }else{ $flag=' bgcolor=#FAFDE2'; } if($tmpb==1){ echo "<tr>"; for($tmpc=0;$tmpc<$fweek;$tmpc++){ echo "<td></td>"; } } if(strcmp($this->getweek($this->year,$this->month,$tmpb),0)==0){ //如果是周日 echo "<tr><td align='center' $flag>$tmpb</td>"; }else{ echo "<td align='center' $flag>$tmpb</td>"; } } echo "</table></div>"; } //獲得方法內指定的日期的星期數 function getweek($year,$month,$day){ $week=date("w",mktime(0,0,0,$month,$day,$year)); //獲得星期 return $week; //獲得星期 } function _env(){ if(isset($_POST["month"])){ $month=$_POST["month"]; }else{ $month=date("m"); //預設為本月 } if(isset($_POST["year"])){ $year=$_POST["year"]; }else{ $year=date("Y"); //預設為本年 } $this->setyear($year); $this->setmonth($month); $date=sprintf('%1d',date('d')); $this->setday($date); }} $D=new calendar; $D->OUT(); ?>
在index.php裡面直接引用該檔案
<!-- 日曆顯示 --> <tr> <span style="white-space:pre"></span><td height="155" align="center" valign="top"><?php include 'cale.php';?></td> </tr>
2、最新文章顯示:
<!-- 最新文章顯示 --><tr><span style="white-space:pre"></span><td height="125" align="center" valign="top" > <table width="200" border="0" cellpadding="0" cellspacing="0"> <tr> <span style="white-space:pre"></span><td> <table width="201" border="0" cellpadding="0" cellspacing="0" style="margin-top:25px" valign="top"> </table> </td> </tr> <?php $sql="select id,title from tb_article order by id desc limit 5"; $res = $sqlHelper->execute_dql($sql); $i=1; while($info=$res->fetch_assoc()){ ?> <tr> <span style="white-space:pre"></span><td width="201" align="left" valign="top"> <a href="article.php?file_id=<?php echo $info['id'];?>" target="_blank"><font size="2"><?php echo $i."、".substr($info['title'],0,27);?></font></a> </td> </tr> <?php $i=$i+1; } ?> <tr> <td height="10" align="right"><a href="file_more.php"><img src=" images/more.gif" width="27" height="9" border="0"> </a></td> </tr> </table> </td></tr>這裡我去查詢資料庫的時候使用了自己的工具類sqlHelper.class.php
這裡給出上面用到的方法實現代碼:
sqlHelper.class.php部分代碼:
class SqlHelper{ public $mysqli; public $dbname="db_tmlog"; public $username="root"; public $password="root"; public $host="localhost"; public function __construct(){ $this->mysqli = new mysqli($this->host, $this->username, $this->password, $this->dbname); if($this->mysqli->connect_error){ die("串連失敗".$this->mysqli->connect_error); } $this->mysqli->query("set names utf8"); } //執行dql語句 public function execute_dql($sql){ $res = $this->mysqli->query($sql) or die($this->mysqli->error); //這裡返回的是一個結果集,當調用$row = $res->fetch_assoc()時是一條一條的向下走,應該使用while迴圈 return $res; } <span style="font-family: Arial, Helvetica, sans-serif;">}</span>
dql語句就是簡單的查詢語句。
在使用資料庫查詢之前,先把這個檔案包進去,然後new一個工具類對象,然後使用對象調用裡面的函數。
3、最新圖片顯示
<!-- 最新圖片顯示 --><tr><span style="white-space:pre"></span><td height="201" align="center" valign="top"><br/> <table width="145" border="0" cellspacing="0" cellpadding="0"> <tr> <span style="white-space:pre"></span><td> <table width="201" border="0" cellspacing="0" cellpadding="0" valign="top" style="margin-top:5px;"> <?php<span style="white-space:pre"></span>$sql="select id,tpmc,file from tb_tpsc order by id desc limit 2";$res2 = $sqlHelper->execute_dql($sql);while($info=$res2->fetch_assoc()){<span style="white-space:pre"></span>$query="select * from tb_tpsc where id=".$info['id']; <span style="white-space:pre"></span>$result=$sqlHelper->execute_dql($query); <span style="white-space:pre"></span>if($row = $result->fetch_assoc()){ <span style="white-space:pre"></span>$data = $row['file']; <span style="white-space:pre"></span>}?><tr> <span style="white-space:pre"></span><td width="9" rowspan="2" align="center"> </td> <td width="147" align="center"> <a href="image.php?recid=<?php echo $info['id']; ?>" target="_blank"> <img src="<?php echo $data;?>" width="120" height="80" border="0"> </a> </td> <td width="10" rowspan="2" align="center"> </td> </tr> <tr> <td align="center">圖片名稱:<?php echo $info['tpmc'];?></td></tr><?php }?><tr><span style="white-space:pre"></span><td colspan="3" height="10" align="right"><a href="pic_more.php"><img src=" images/more.gif" width="27" height="9" border="0"> </a></td></tr> </table> </td> </tr> </table> </td></tr>
同樣使用了資料庫查詢。
4、公告區實現
在公告區使用了我以前沒有見過的一個標籤<marquee>
它裡面設定了一些屬性,就是當滑鼠停留在上面的時候它就停止滾動,離開的時候就開始滾動。
<?php<span style="white-space:pre"></span>$p_sql = "select * from tb_public order by id desc"; $p_rst = $sqlHelper->execute_dql($p_sql);?><marquee onMouseOver="this.stop()" style="width:426px; height:280px" onMouseOut="this.start()" scrollamount="2" scrolldelay="7" direction="up" align=""><span style="FONT-SIZE: 9pt"><center><?php<span style="white-space:pre"></span>while($p_row = $p_rst->fetch_row()){?><a href="#" onClick="wopen=open('show_pub.php?id=<?php echo $p_row[0]; ?>','','height=200,width=1000,scollbars=no')"><?php echo $p_row[1]; ?></a><br> <?php }?></center></span></marquee>這個標籤是HTML5新增的,還有center標籤。那麼在使用的時候就會出現下面畫黃色波浪線的情況,我沒有去管他。
在<a>這個超連結標籤裡,它設定了onclick這個屬性,onclick這個屬性後面跟的一定是js檔案裡面的函數,這個是開啟一個自訂寬高的視窗。href="#"表示這個超連結不串連到其他頁面,這裡超連結的響應使用onclick來設定了。
到這裡我們的首頁基本就實現了。這是index.php 的完整代碼:index.php提取碼:iu09
PHP經典項目案例-(一)部落格管理系統3