Home  >  Article  >  Backend Development  >  How to use PHP to create a dynamic counter_PHP tutorial

How to use PHP to create a dynamic counter_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:31:14882browse

我们在创建网站的时候,通常都会有在网页中添加计数器的功能,今天在这篇文章中我们将为大家具体介绍PHP制作动态计数器的相关功能。

的功能非常强大,你可以利用它做几乎任何事。不需要复杂的变量和代码,你就可以非常迅速地做出漂亮的计数器来。下面,就让我们一步一步的来做。

其实,PHP制作动态计数器的原理大家都知道,首先,确定一个记录文件,例如counter.txt或counter.log,每一次访问这个页面,就对这个文件加1,然后把结果显示出来。因此,我们的PHP代码开始应该是这样的:

<ol class="dp-xml">
<li class="alt"><span><span class="tag">< ?</span><span class="tag-name">PHP</span><span> </span></span></li><li><span>$</span><span class="attribute">countfile</span><span> = </span><span class="attribute-value">"js/counter.inf"</span><span>;  </span></li><li class="alt"><span>//定义计数器写入的文件是当前目录下JS目录中<br />//counter.inf,然后我们应当测试该文件能否打开  </span></li><li><span>if (($</span><span class="attribute">fp</span><span> = </span><span class="attribute-value">fopen</span><span>($countfile, "r+")) == false) <br />{ //用读写模式打开文件,若不能打开就退出  </span></li><li class="alt"><span>printf ("Open file %s failed!",$countfile);  </span></li><li><span>exit;  </span></li><li class="alt"><span>}  </span></li><li><span>else  </span></li><li class="alt"><span>{  </span></li><li><span>//如果文件能够正常打开,就读入文件中的数据,假设是1  </span></li><li class="alt"><span>$</span><span class="attribute">count</span><span> = </span><span class="attribute-value">fread</span><span> ($fp,10);  </span></li><li><span>//读取10位数据  </span></li><li class="alt"><span>$</span><span class="attribute">count</span><span> = $count + 1;  </span></li><li><span>//count ++  </span></li><li class="alt"><span>fclose ($fp);  </span></li><li><span>//关闭当前文件  </span></li><li class="alt"><span>$</span><span class="attribute">fp</span><span> = </span><span class="attribute-value">fopen</span><span>($countfile, "w+")  </span></li><li><span>//以覆盖模式打开文件  </span></li><li class="alt"><span>fwrite ($fp,$count);  </span></li><li><span>//写入加1后的新数据  </span></li><li class="alt"><span>fclose ($fp);  </span></li><li><span>//并关闭文件  </span></li><li class="alt"><span>} </span></li></ol>

这时,整个PHP制作动态计数器的工作就完成了,如果只是简单的文字计数的话,在这里就可以输出变量$count的值。下面是将$count转换为图片模式的代码:

<ol class="dp-xml"><li class="alt"><span><span>$</span><span class="attribute">fp</span><span> = </span><span class="attribute-value">fopen</span><span> ($countfile, "r"); <br />//以只读模式打开文件  </span></span></li><li><span>$</span><span class="attribute">array_count</span><span> = </span><span class="attribute-value">1</span><span>; //定义一个表<br />示数组元素位置的变量,下面要用  </span></li><li class="alt"><span>while (! feof($fp)) {  </span></li><li><span>$</span><span class="attribute">current_number</span><span> = </span><span class="attribute-value">fgetc</span><span>($fp);  </span></li><li class="alt"><span>$counter_array[$array_count] = $current_number;  </span></li><li><span>$</span><span class="attribute">array_elements</span><span> = </span><span class="attribute-value">count</span><span> ($counter_array);  </span></li><li class="alt"><span>$</span><span class="attribute">array_count</span><span> = $array_count + 1;  </span></li><li><span>}  </span></li></ol>

上面这个while循环的作用是把每一位数分离出来。它从counter.inf中由左到右逐位读取数值,并依次写入一个叫counter_array的数组中,这个数组的索引是从1开始的($array_count)。如果现在counter.inf中的数字是158,那么数组$counter_array[]就像这样:$counter_array[1] = 1、$counter_array[2] = 5、$counter_array[3] = 8。有了上面这些工作,我们就可以方便地显示不同的数字图片了,PHP制作动态计数器显示代码如下:

<ol class="dp-xml"><li class="alt"><span><span>echo ("</span><span class="tag"><</span><span class="tag-name">table</span><span> </span><span class="attribute">border</span><span>=($%$43%^<br />#ASD#2@$#f$%^)0($%$43%^#ASD#2@$#f$%^)<br /> </span><span class="attribute">height</span><span>=($%$43%^#ASD#2@$#f$%^)5($%$43<br />%^#ASD#2@$#f$%^) </span><span class="attribute">align</span><span>=($%$43%^#ASD#2@$<br />#f$%^)center($%$43%^#ASD#2@$#f$%^)</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">tr</span><span class="tag">></span></span></li>
<li class="alt"><span><span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">bgcolor</span><span>=($%$43%^#ASD#2@$#f$%^)#<br />bab389($%$43%^#ASD#2@$#f$%^) </span></span></li><li class="alt"><span><span class="attribute">align</span><span>=($%$43%^#ASD#2@$#f$%^)center($%$43%^#ASD#2@$#f$%^)</span></span></li><li class="alt"><span><span class="tag">></span><span>欢迎您,第");  </span></span></li>
<li>
<span>for ($</span><span class="attribute">array_id</span><span> = </span><span class="attribute-value">1</span><span>;$array_id </span><span class="tag"><</span><span> $array_elements; ++ $array_id) {  </span></li><li class="alt"><span>echo ("</span><span class="tag"><</span><span class="tag-name">img</span><span> </span><span class="attribute">src</span><span>=</span><span class="attribute-value">http</span><span>://www.asp315.com<br />/news/2/($%$43%^#ASD#2@$#f$%^)images<br />//counter//$counter_array[$array_id].gif($%$43%^#ASD#2@$#f$%^)<br /> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">absmiddle</span><span class="tag">></span><span>");  </span>
</li>
<li><span>}  </span></li>
<li class="alt">
<span>echo ("位客人</span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">tr</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">table</span><span class="tag">></span><span>");  </span>
</li>
<li><span>}  </span></li>
</ol>

上面的PHP制作动态计数器代码很简单,就是画一个表格,并依次在表格中显示所需的图片。在imagescounter目录下有0.gif至9.gif十张图片,for循环遍历数组,从高位到低位给出每一位数相应的图片,直到数组的尾部。这样,一个完整的计数器就完成了。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446242.htmlTechArticle我们在创建网站的时候,通常都会有在网页中添加计数器的功能,今天在这篇文章中我们将为大家具体介绍PHP制作动态计数器的相关功能。...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn