Heim > php教程 > php手册 > Hauptteil

phpcms V9 二次开发------(获取点击数详解)

WBOY
Freigeben: 2016-10-22 00:00:07
Original
1375 Leute haben es durchsucht

关于phpcms V9的点击数的使用应该有不少数是直接调用网上搜索到的代码,但是对于一些想要深入研究开发的人来说,看到网上的代码后更是不解,本人这几天看了看,了解了一些东西,在这里写出来分享一下,首先先了解一下基础知识

pc_base::load_model(‘*<span style="color: #000000;">_model’) 加载数据库模型 
pc_base::load_sys_class(‘classname’) 实例化系统类
pc_base::load_app_class(‘classname’,’admin’) 实例化模块类
pc_base::load_sys_func (‘funcfile’) 调用系统函数库</span>
Nach dem Login kopieren
以上是调用模型和实例化对象的四种方法
  pc_base::load_model(‘*_model’) 对应加载 根目录\phpcms\model 下面的类文件
  pc_base::load_sys_class(‘classname’) 对应加载 根目录\phpcms\libs\classes 下面的文件
  pc_base::load_app_class(‘classname’,’admin’) 对应加载 根目录\phpcms\modules\admin\classes 下面的文件
  pc_base::load_sys_func (‘funcfile’) 对应加载 根目录\phpcms\libs\functions\
下面讲解一个获取点击数的实例 
<span style="color: #008080;">1</span> {pc:content action=<span style="color: #800000;">"</span><span style="color: #800000;">lists</span><span style="color: #800000;">"</span> catid=<span style="color: #800000;">"</span><span style="color: #800000;">$catid</span><span style="color: #800000;">"</span> num=<span style="color: #800000;">"</span><span style="color: #800000;">25</span><span style="color: #800000;">"</span> order=<span style="color: #800000;">"</span><span style="color: #800000;">id DESC</span><span style="color: #800000;">"</span> page=<span style="color: #800000;">"</span><span style="color: #800000;">$page</span><span style="color: #800000;">"</span> moreinfo=<span style="color: #800000;">"</span><span style="color: #800000;">1</span><span style="color: #800000;">"</span><span style="color: #000000;">}
</span><span style="color: #008080;">2</span> <span style="color: #000000;">{loop $data $r}
</span><span style="color: #008080;">3</span> {php $db = pc_base::load_model(<span style="color: #800000;">'</span><span style="color: #800000;">hits_model</span><span style="color: #800000;">'</span>);   $_r = $db->get_one(array(<span style="color: #800000;">'</span><span style="color: #800000;">hitsid</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">c-</span><span style="color: #800000;">'</span>.$modelid.<span style="color: #800000;">'</span><span style="color: #800000;">-</span><span style="color: #800000;">'</span>.$r[id])); $views =<span style="color: #000000;"> $_r[views]; }
</span><span style="color: #008080;">4</span> {php $comment_tag = pc_base::load_app_class(<span style="color: #800000;">"</span><span style="color: #800000;">comment_tag</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">comment</span><span style="color: #800000;">"</span>); $comment_total = $comment_tag->count(array(<span style="color: #800000;">'</span><span style="color: #800000;">commentid</span><span style="color: #800000;">'</span>=><span style="color: #800000;">'</span><span style="color: #800000;">content_</span><span style="color: #800000;">'</span>.$catid.<span style="color: #800000;">'</span><span style="color: #800000;">-</span><span style="color: #800000;">'</span>.$r[id].<span style="color: #800000;">'</span><span style="color: #800000;">-</span><span style="color: #800000;">'</span><span style="color: #000000;">.$modelid));}
</span><span style="color: #008080;">5</span> 
Nach dem Login kopieren
  • class="rt">{date('Y-m-d H:i:s',$r[inputtime])}·"{$r[url]}" target="_blank"{title_style($r[style])}>{$r[title]} 点击:{$views} 评论数:{if $comment_total}{$comment_total}{else}0{/if}
  • {/loop} 6 {$pages} 7 {/pc}

    其中的第3行是获取点击数的方法我们单独着重解说:

    $db = pc_base::load_model('hits_model')  
    
    Nach dem Login kopieren

    实例化对象为 $db,加载实例化类hit_model,该类的位置在 根目录\phpcms\model\hit_model.class.php文件中

    <span style="color: #0000ff;">class</span><span style="color: #000000;"> hits_model extends model {
        </span><span style="color: #0000ff;">public</span> $table_name = <span style="color: #800000;">''</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> function __construct() {
            $</span><span style="color: #0000ff;">this</span>->db_config = pc_base::load_config(<span style="color: #800000;">'</span><span style="color: #800000;">database</span><span style="color: #800000;">'</span><span style="color: #000000;">);
            $</span><span style="color: #0000ff;">this</span>->db_setting = <span style="color: #800000;">'</span><span style="color: #800000;">default</span><span style="color: #800000;">'</span><span style="color: #000000;">;
            $</span><span style="color: #0000ff;">this</span>->table_name = <span style="color: #800000;">'</span><span style="color: #800000;">hits</span><span style="color: #800000;">'</span><span style="color: #000000;">;
            parent::__construct();
        }
    }</span>
    Nach dem Login kopieren
    该类文件加载继承了model类文件并且继承了其内部的方法,所以下面调用get_one()方法
    $_r = $db->get_one(array('hitsid'=>'c-'.$modelid.'-'.$r[id])) 调用$db对象中的get_one方法该方法位于hits_model继承的model类中代码如下
    final <span style="color: #0000ff;">public</span> function get_one($<span style="color: #0000ff;">where</span> = <span style="color: #800000;">''</span>, $data = <span style="color: #800000;">'</span><span style="color: #800000;">*</span><span style="color: #800000;">'</span>, $order = <span style="color: #800000;">''</span>, $group = <span style="color: #800000;">''</span><span style="color: #000000;">) {
            </span><span style="color: #0000ff;">if</span> (is_array($<span style="color: #0000ff;">where</span>)) $<span style="color: #0000ff;">where</span> = $<span style="color: #0000ff;">this</span>->sqls($<span style="color: #0000ff;">where</span><span style="color: #000000;">);
            </span><span style="color: #0000ff;">return</span> $<span style="color: #0000ff;">this</span>->db->get_one($data, $<span style="color: #0000ff;">this</span>->table_name, $<span style="color: #0000ff;">where</span><span style="color: #000000;">, $order, $group);
        }</span>
    Nach dem Login kopieren

    get_one(arr('hitsid'=>'c-'.$modelid.'-'.$r[id]))方法中传递的数组为数据表v9_hits中的字段的值,其 hits 表的结构如下

    此时的$_r为该表中的一条数据 数据表中的字段 views 即为该篇文章的点击次数 所以使用 $_r[views]即可获取点击数啦!!!

     注:hitsid 字段的数据 c-1-2 中 1表示当前模型id 2表示当前文章的id

     

     

     

      

    Quelle:php.cn
    Erklärung dieser Website
    Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
    Beliebte Empfehlungen
    Beliebte Tutorials
    Mehr>
    Neueste Downloads
    Mehr>
    Web-Effekte
    Quellcode der Website
    Website-Materialien
    Frontend-Vorlage
    Über uns Haftungsausschluss Sitemap
    Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!