【问题标题】:what is the use of $this->uri->segment(3) in codeigniter pagination$this->uri->segment(3) 在 codeigniter 分页中的用途是什么
【发布时间】:2013-10-18 02:27:37
【问题描述】:

听到是我的密码

public function viewdeletedrecords()
{   

    if($this->session->userdata('applicant_firstname') == '')
    {
        redirect('papplicant/login') ;
    }
    $profile = $this->m_applicant->showdeletedrecods('','');                                                         
    $total_rows = count($profile) ;
    $config['base_url'] =  base_url().'index.php/papplicant/viewdeletedrecords/' ;
    $config['per_page'] = '10' ;
    $config['full_tag_open'] = '<div>' ;

    $config['full_tag_close'] = '</div>' ;

    $config['first_link'] = 'First' ;

    $config['last_link'] = 'Last' ;

    $config['use_page_numbers'] = TRUE ;

    $config['prev_link'] = '&lt;' ;

    $config['uri_segment'] = 3 ;

    $config['num_links'] = 10 ;         

    $config['cur_tag_open'] = '<b>' ;

    $config['cur_tag_close'] = '</b>' ;

    $config['total_rows'] = $total_rows ;       

    $invoicepaginate = $this->m_applicant->showdeletedrecods( $config['per_page'], $this->uri->segment(3)) ;    

    $this->pagination->initialize($config);     

    $data4 = array(                             

    'data' => $invoicepaginate                                                                                       

    ) ;

    $this->load->view('applicant', $data4);

}

$this-&gt;uri-&gt;segment(3)在codeigniter中有什么用

当我输入 $this-&gt;uri-&gt;segment(3); 时,它按预期工作,但是当我输入 $this-&gt;uri-&gt;segment(4); 时,它停止工作

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    这使您可以从 URI 字符串中检索信息

    $this->uri->segment(n); // n=1 for controller, n=2 for method, etc
    

    考虑这个例子:

    http://example.com/index.php/controller/action/1stsegment/2ndsegment

    它会返回

    $this->uri->segment(1); // controller
    $this->uri->segment(2); // action
    $this->uri->segment(3); // 1stsegment
    $this->uri->segment(4); // 2ndsegment
    

    【讨论】:

      【解决方案2】:

      默认情况下,如果段不存在,该函数将返回 FALSE(布尔值)。如果段丢失,还有一个可选的第二个参数允许您设置自己的默认值。例如,这将告诉函数在失败时返回数字零: $product_id = $this->uri->segment(3, 0);

      它有助于避免编写这样的代码:

      [if ($this->uri->segment(3) === FALSE)
      {
          $product_id = 0;
      }
      else
      {
          $product_id = $this->uri->segment(3);
      }]
      

      【讨论】:

        【解决方案3】:

        假设你有一个这样的网址 http://www.example.com/controller/action/arg1/arg2

        如果你想知道这个 url 中传递的参数是什么

        $param_offset=0;
        $params = array_slice($this->uri->rsegment_array(), $param_offset);
        var_dump($params);
        

        输出将是:

        array (size=2)
          0 => string 'arg1'
          1 => string 'arg2'
        

        【讨论】:

          【解决方案4】:

          CodeIgniter User Guide 说:

          $this->uri->segment(n)

          允许您检索特定段。其中 n 是段 您要检索的号码。段从左到右编号。 例如,如果您的完整 URL 是这样的: http://example.com/index.php/news/local/metro/crime_is_up

          段号是这样的:

          1. news
          2. local
          3. metro
          4. crime_is_up
          

          所以segment 指的是你的url 结构段。通过上面的示例,$this-&gt;uri-&gt;segment(3) 将是 'metro',而 $this-&gt;uri-&gt;segment(4) 将是 'crime_is_up'

          【讨论】:

            【解决方案5】:

            在您的代码中$this-&gt;uri-&gt;segment(3) 指的是您在查询中使用的分页offset。根据您的$config['base_url'] = base_url().'index.php/papplicant/viewdeletedrecords/' ;$this-&gt;uri-&gt;segment(3) 即段 3 指的是偏移量。第一段是controller,第二段是method,然后是parameters作为segments发送给控制器。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2014-12-18
              • 2016-07-07
              • 1970-01-01
              • 2023-03-22
              • 2013-08-08
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多