【问题标题】:Cannot access parameter from Codeigniter URL无法从 Codeigniter URL 访问参数
【发布时间】:2014-09-14 10:40:04
【问题描述】:

如何从 Codeigniter url 中获取第三个参数,例如 http://domain.org/project/controller/view#tab-3/2。我需要从 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;
    }

谁能帮我解决这个问题?提前致谢。

【问题讨论】:

    标签: php codeigniter url url-parameters


    【解决方案1】:

    哈希 (#) 之后的值永远不会发送到服务器 - 它在浏览器中被解析,因此 php (codeignitor) 永远不会知道它。

    但是似乎你只是访问它然后将它嵌入到页面中,供 js 访问。

    相反,您可以直接使用 js 从 url 中获取值:

    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);
            }
        });
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-28
    • 2016-11-19
    • 2021-12-08
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多