2017-04-18 66 views
0

我正在创建一个网站来创建一个简短的网址。我在计算短网址的点击数时遇到了问题。我可以从数据库获取简短的URL并重定向到特定的网站。一旦其重定向特定网站的短网址点击次数将增加+1。但它并没有增加。最后一天,我的自我尝试,但我不能得到结果。使用codeigniter来计算短网址的点击次数

这是我的控制器编码..

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(); 
} 
+0

当你重定向URL那段时间更新计数,我认为。你的问题是更新计数后重定向URL,所以计数不会更新。 –

+0

@HothiJimit感谢您的评论...你知道如何解决这个问题。 – pixel

回答

1

你的错误是后increate电话号码,但是方法

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

重定向URL不能打电话。

所以你改变下面的代码在控制器和模型resp。

控制器

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(); 
} 
+0

根据你的评论短url只重定向到长URL但点击不更新.. – pixel

+0

你的表名检查你的数据库不正确哪个名称使用“url我”ya“url”???两个名字不同???。 –

+0

其正确的只有我的它像一个对象,我们可以把这样的任何字母... – pixel