无法从Codeigniter URL访问参数

时间:2014-07-23 09:49:51

标签: php codeigniter url url-parameters

如何从Codeigniter网址获取第3个参数,例如http://domain.org/project/controller/view#tab-3/2。我需要访问第3个参数值,即此处,来自URL的2

我尝试使用$this->uri->segment(3);并且没有返回任何值。 $this->uri->segment(2);返回值view而不是view#tab-3

我想做的是CI中的分页。在我的页面中,我正在使用标签。在单击tab3时,它将调用ajax函数,如下所示:

function receivedtickets(userid,baseurl)
{
    var pagenum=$('#pagenum').val();

    if(pagenum!="" && pagenum!=undefined)
    { 
        data='userid='+userid+'&pagenum='+pagenum;
    }
    else
    {
        data='userid='+userid;
    }
    $.ajax({
        type:'post',
        url:baseurl+'video/receivedtickets/'+pagenum,
        datatype:'json',
        data:data,
        success:function(response)
        { 
            $('#tab-3').html(response.results);
            $('#recpagination').html(response.pagination);
        }
    });
}

pagenum是我的视图页面中包含标签的隐藏字段。

if($this->uri->segment(3))
        {
            $data['page_num']=$this->uri->segment(3);
        }
        else 
        {
            $data['page_num']=0;
        }
<input type="hidden" id="pagenum" value="<?php echo $data['page_num'];?>">

分页控制器代码:

function receivedtickets()
    {
        if (!$this->tank_auth->is_logged_in()) 
        {       // not logged in
            redirect(base_url().'auth/login');
        }
        $this->load->library('pagination');         // library for pagination

        $userid=$_POST['userid'];
        $per_pg=1;

        if($this->uri->segment(3))
        { 
            $page_num= $this->uri->segment(3);
            $offset=($page_num - 1) * $per_pg;   
        }
        else 
        {
            $offset=0;
        }

        $config['base_url'] = base_url().'video/tickets#tab-3'; 
        $config['total_rows'] = 50; 
        $config['div'] = '#pagination'; // div for displaying ajax
        $config['per_page'] = $per_pg; 
        $config['uri_segment'] = 3;
        $config['page_query_string'] = FALSE;
        $config['use_page_numbers'] = TRUE;
        $this->pagination->initialize($config); 
        $pagination=$this->pagination->create_links();

        $string="";
        $query=$this->db->query("SELECT 
  s.send_on,
  s.sendby_id,
  t.ticket_key,
  t.video_id,
  a.first_name,
  a.last_name,
  v.title,
  v.videothumbnail ,
  s.sendto_id
FROM
  sendticket AS s 
  LEFT JOIN ticket AS t 
    ON s.ticketid = t.id 
  LEFT JOIN auth_user_profiles AS a 
    ON a.user_id = s.sendby_id 
  LEFT JOIN video AS v 
    ON t.video_id = v.videoid 
    WHERE (s.sendto_id = $userid or s.sendto_id ='gfhgfh@kgjgj.in')
ORDER BY s.send_on DESC limit $offset,$per_pg");

        $string.='<div class="tableoutertb">
                <table class="myticketsdivv">
                    <tr class="mytickets_row">
                        <td class="mytickets_colmn ">'.lang('video').'</td>
                        <td class="mytickets_colmn ">'.lang('title').'</td>
                    </tr>';

                    if($query->num_rows > 0)
                    {
                        foreach($query->result() as $row)
                        {
                          $string.='<tr class="mytickets_row">
                            <td class="mytickets_colmn">
                                <span class="mob_title">'.lang('video').'</span>
                                <a target="_blank" href="'.base_url().'video/playvideo/'.$row->video_id.'">';
                                    $string.='<img src="'.base_url().'images/No_image.png" style="max-width: 140px;" alt="Teshot featured video preview">';
                                $string.='</a>
                            </td>
                            <td class="mytickets_colmn">
                                <span class="mob_title">'.lang('title').'</span>
                                <a target="_blank" style="color: #3b5998;" href="'.base_url().'video/playvideo/'.$row->video_id.'">'.$row->title.'</a>
                            </td>
                            </tr>';
                        }
                        $string.='<tr class="mytickets_row"><td class="mytickets_colmn" colspan="5"><div class="pagination" style="position: initial;margin-top:0px;float:right;" id="recpagination"></div></td></tr>';
                    }
                $string.='</table>
                </div>';
                header('Content-type: application/json');
                $ret = array();
                $ret['results']=$string;
                $ret['pagination']= $pagination;
                echo json_encode($ret);
        exit;
    }

任何人都可以帮我解决这个问题吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

散列(#)之后的值永远不会发送到服务器 - 它会在浏览器中重新发送,因此php(codeignitor)永远不会知道它。

然而,似乎您只是访问它然后将其嵌入到页面中,以供js访问。

相反,您可以直接使用js:

从网址中获取值
function receivedtickets(userid,baseurl)
{
    var hash = location.hash;
    var pagenum = hash.substr(hash.lastIndexOf("/")+1);

    if(pagenum!="" && pagenum!=undefined)
    {
        data='userid='+userid+'&pagenum='+pagenum;
    }
    else
    {
        data='userid='+userid;
    }
    $.ajax({
        type:'post',
        url:baseurl+'video/receivedtickets/'+pagenum,
        datatype:'json',
        data:data,
        success:function(response)
        {
            $('#tab-3').html(response.results);
            $('#recpagination').html(response.pagination);
        }
    });
}