使用codeigniter计算短网址的点击次数

时间:2017-04-18 09:12:22

标签: codeigniter

我正在创建一个用于创建简短网址的网站。我在计算短网址的点击时遇到问题。我可以从db获取短网址并重定向到该特定网站。一旦其重定向特定网站短网址点击将增加+1。但它没有发展。最后一天,我正在努力,但我无法得到结果。

这是我的控制器编码..

class Go extends CI_Controller {
     public function __construct(){
        parent::__construct();
         $this->load->model('Select_model');
        }
    public function index(){
            $url_code = $this->uri->segment(1);
            // redirect the short url 
            $query = $this->Select_model->selectShortUrl($url_code);
            // update clicks for short url
            $update = $this->Select_model->updateShortUrl($url_code);

   }  
}

这是我的模型编码..

 // redirect the short url 
public function selectShortUrl($shorturl){
      $cx=$this->db->select('*')
                   ->from('url i')
                   ->where('i.shorturl',$shorturl)
                   ->get();
           if ($cx->num_rows() == 1) {
               foreach ($cx->result() as $row) {
                    $url_address = $row->fullurl;
           }
           redirect (prep_url($url_address));
         }
        else{
            redirect(base_url());
        } 

}

// update clicks for short url
public function updateShortUrl($shorturl){
        $cx=$this->db->set('clicx','`clicx`+1')
                   ->where('shorturl',$shorturl)
                   ->update('url');
        return $cx->result();
}

1 个答案:

答案 0 :(得分:1)

你的错误是

 $query = $this->Select_model->selectShortUrl($url_code);
            // update clicks for short url
            $update = $this->Select_model->updateShortUrl($url_code);

在递增数字后重定向网址,但该方法无法调用。

因此您可以在控制器和型号中更改以下代码。

你的控制器

class Go extends CI_Controller {
     public function __construct(){
        parent::__construct();
         $this->load->model('Select_model');
        }
    public function index(){
            $url_code = $this->uri->segment(1);
            // redirect the short url 
            $query = $this->Select_model->selectShortUrl($url_code);
            // update clicks for short url


   }  
}   

你的模特

// redirect the short url 
public function selectShortUrl($shorturl){
      $cx=$this->db->select('*')
                   ->from('url i')
                   ->where('i.shorturl',$shorturl)
                   ->get();
           if ($cx->num_rows() == 1) {
               foreach ($cx->result() as $row) {
                    $url_address = $row->fullurl;
           }

           $this->updateShortUrl($shorturl);


           redirect (prep_url($url_address));
         }
        else{
            redirect(base_url());
        } 

}

// update clicks for short url
public function updateShortUrl($shorturl){
        $cx=$this->db->set('clicx','`clicx`+1',FALSE)
                   ->where('shorturl',$shorturl)
                   ->update('url');
        return $cx->result();
}