使用codeigniter计算短url的点击次数
我正在为创建一个短url创建一个网站。我在计算短url的点击次数时遇到了问题。我可以从数据库中获取短url并重定向到该特定网站。一旦它重定向到特定网站,短url点击量将增加+1。但它不是递增的。最后一天,我在努力,但没有结果 这是我的控制器编码使用codeigniter计算短url的点击次数,codeigniter,Codeigniter,我正在为创建一个短url创建一个网站。我在计算短url的点击次数时遇到了问题。我可以从数据库中获取短url并重定向到该特定网站。一旦它重定向到特定网站,短url点击量将增加+1。但它不是递增的。最后一天,我在努力,但没有结果 这是我的控制器编码 class Go extends CI_Controller { public function __construct(){ parent::__construct(); $this->load->
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();
}
你的错误是
$query = $this->Select_model->selectShortUrl($url_code);
// update clicks for short url
$update = $this->Select_model->updateShortUrl($url_code);
在递增数字后重定向url,但该方法无法调用
所以,在控制器和模型响应中更改以下代码
你的控制器
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();
}
我想当你重定向url的时候,更新的次数是多少。您的问题是在更新计数后重定向url,所以计数不会被更新。@HothiJimit感谢您的评论。。。你知道如何解决这个问题。根据你的评论,短url只重定向到长url,但点击没有更新。你的表名检查你的数据库不正确哪个名称使用了“url i”ya“url”???两个名字都不一样???它是正确的只有我它像一个物体我们可以放任何这样的字母…回答编辑请检查它在我的系统中工作