麻辣家族討論版版's Archiver

小誌 發表於 2011-4-19 18:55

6-4-1 圖形計數器

[color=DarkRed][size=4][b]6-4-1   圖形計數器[/b][/size][/color]
文字型計數器看起來好像單調了一點,我們何不將它改成圖片式的圖形計數器,日後如果想換換口味的話,直接將圖檔換掉就行了!
[attach]5579[/attach]
圖6-28  圖片式計數器。

在撰寫程式之前,請先準備好代表0~9的數字圖片,檔名跟圖片的內容數字必須相符:
[attach]5580[/attach]
為了將計數檔案內的資料取出來並轉換為圖片,我們必須將數字的每一個「字」拆解出來,並以圖片檔名替代:[code]<?
function picout($num)
{
$strlen=strlen($num);
for ($x=0;$x<$strlen;$x++)
  {
   $picnum=substr($num,$x,1);
    echo "<img src=$picnum.gif>";
  }
}
?>[/code][list]
[*]程式碼第4行:先利用strlen()函數計算字串的長度(計數資料的位數)。
[*]程式碼第5~9行:使用for迴圈將各個圖片檔的HTML標籤敘述輸出。
[*]利用substr()函數拆解字串,每次取出一個字元。
[/list]

小誌 發表於 2011-4-19 18:56

為了不變動原始的文字計數器功能,所以小誌把文字轉換成圖片HTML標籤敘述的程式區塊用函數包起來,所以我們只要將本來輸出文字資料的地方改成呼叫副程式即可。
piccount.php[code]<?
$countfile="piccount.txt";
global $num;
if (file_exists($countfile))
   {
    $file=fopen($countfile,"r");
    $num=fgets($file,filesize($countfile)+1);
    $num++;
    fclose($file);
   }
else
   {
    $num="1";
   }
$file=fopen($countfile,"w");
fwrite($file,$num);
fclose($file);
?>

<html>
<head>
<meta http-equiv="Content-Language" content="zh-tw">
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title>實用的圖片計數器</title>
</head>
<body>
<p align="center"><font size="5">實用的圖片字計數器</font></p>
<hr>
<p align="center"><b><font color="#FF0000">
參觀人次:<?=picout($num)?></font></b></p>
</body>
</html>

<?
function picout($num)
{
$strlen=strlen($num);
for ($x=0;$x<$strlen;$x++)
  {
   $picnum=substr($num,$x,1);
    echo "<img src=$picnum.gif>";
  }
}
?>[/code]

小誌 發表於 2011-4-19 18:56

另解:
Piccount_2.php[code]<?
$countfile="piccount.txt";
if (file_exists($countfile))
   {
    $file=fopen($countfile,"r");
    $num=fread($file,filesize($countfile));
    $num++;
    fclose($file);
    $file=fopen($countfile,"w");
    fwrite($file,$num);
    fclose($file);
   }
else
   {
    $num=1;
    $file=fopen($countfile,"w");
    fwrite($file,$num);
    fclose($file);
   }
?>

<html>
<head>
<meta http-equiv="Content-Language" content="zh-tw">
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title>實用的圖片計數器</title>
</head>
<body>
<p align="center"><font size="5">實用的圖片字計數器</font></p>
<hr>
<p align="center"><b><font color="#FF0000">
參觀人次:<?=picout()?></font></b></p>
</body>
</html>

<?
function picout()
{
global $countfile;
$file=fopen($countfile,"r");
while (false != ($char = fgetc($file)))
  {
     echo "<img src=" . $char . ".gif>";
  }
fclose($file);
}
?>[/code]

頁: [1]

麻辣家族討論版版為 麻辣學園 網站成員  由 昱得資訊工作室 © Since 1993 所提供