2017-09-21 53 views
0

我在php中使用網站計數器,我只需要顯示計數器從0000000的值開始。如何爲網站訪問者打印0000000值

下面是我的代碼:

<?php 
//opens countlog.txt to read the number of hits 

    $datei = fopen("countlog.txt","r"); 
    $count = fgets($datei,1000); 
    fclose($datei); 
    $count=$count + 1 ; 
    // get the total number of digits in the counter 
    $number_length = strlen($count); 
    $main_image = ""; 
    for($i=0;$i<$number_length;$i++){ 
     $digit = substr($count, $i, 1); 
     if($digit == 1){ 
      $main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/1.png'>"; 
     } 
     if($digit == 2){ 
      $main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/2.png'>"; 
     } 
     if($digit == 3){ 
      $main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/3.png'>"; 
     } 
     if($digit == 4){ 
      $main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/4.png'>"; 
     } 
     if($digit == 5){ 
      $main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/5.png'>"; 
     } 
     if($digit == 6){ 
      $main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/6.png'>"; 
     } 
     if($digit == 7){ 
      $main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/7.png'>"; 
     } 
     if($digit == 8){ 
      $main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/8.png'>"; 
     } 
     if($digit == 9){ 
      $main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/9.png'>"; 
     }`enter code here` 
     if($digit == 0){ 
      $main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/0.png'>"; 
     } 
    } 
    echo $main_image; 
    // opens countlog.txt to change new hit number 
    $datei = fopen("countlog.txt","w"); 
    fwrite($datei, $count); 
    fclose($datei); 

?> 
+2

使用strpad http://php.net/manual/en/function.str-pad.php – MacBooc

+0

非常感謝你..它爲我工作.. –

回答

3

嘗試str_pad功能:

$main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/".$digit.".png'>"; 
+0

謝謝....它對我的幫助 –

0

您還可以創建:

$count=$count + 1 ; 
$count = str_pad($count, 7, '0', STR_PAD_LEFT); 

你也可以用一條線替換你的大if塊圖像生成器並將其鏈接到您的html。

使用

<img src="//yourdomain.com/urltoscript.php" alt="Counter"> 

圖像生成

<?php 
/** 
* Get next incremented counter 
* @return int 
*/ 
function get_next_counter() 
{ 

    $counter_file = 'counter.txt'; 
    $count = 0; 

    if(file_exists($counter_file)) { 
     $count = file_get_contents($counter_file); 
    } 

    $count++; 
    file_put_contents($counter_file, $count); 

    return $count; 
} 

/** 
* Send to browser image with counter. Length 6 digits 
*/ 
function display_image_counter($counter) 
{ 
    $im = imagecreate(100, 30); 

    // White background 
    $bg = imagecolorallocate($im, 255, 255, 255); 

    // dark text 
    $textcolor = imagecolorallocate($im, 0, 0, 0); 

    // Text in left top corner 
    imagestring($im, 5, 0, 0, sprintf("%'.06d", $counter), $textcolor); 

    // Display image to browser 
    header('Content-type: image/png'); 
    imagepng($im); 
    imagedestroy($im); 
} 

$counter = get_next_counter(); 
display_image_counter($counter); 

?> 
相關問題