dream890110 2017-09-21 07:09
浏览 33

如何为网站访问者打印0000000值

I am using a website counter in php,i just need to show counter start from 0000000 value.

Below is my code:

<?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条回答 默认 最新

  • douzong0711 2017-09-21 07:15
    关注

    Try str_pad function:

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

    Also you can replace your big if block with one line:

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

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line