Codeigniter:获取页面的URI段并将其添加到其重定向页面的URL中

Codeigniter:获取页面的URI段并将其添加到其重定向页面的URL中

问题描述:

So I have these 2 pages IF the URL of page1 is: http://localhost/project/page1/page1/5

And there's a link in that page that redirects to page2 with the URL:

http://localhost/project/page2/page2/2
(its 3rd segment - 2 is from dropdown value selected)

Is it possible to make the redirected page - page2 with a URL like this: http://localhost/project/page2/page2/5/2

-so that I could do something with page1's value, like querying dynamically in the database.

VIEW in page1:

<div class="form-group form-inline">
  <select id="p2_val">

      <?php 
          foreach($v_p2 as $row) {
      ?> 
      <option 
          value="<?php  echo base_url('page2/page2/').$row->p2_val;?>">
          <?php echo $row->p2_num; ?>
      </option>
      <?php }  ?> 

  </select>

      <input  class="SubmitButton" type="submit" 
      name="SUBMITBUTTON" value="Submit" />
</div>

Controller in page1:

class Page1 extends CI_Controller{

     public function __construct()
        {
            parent::__construct(); 
            $this->load->model('page1_model');

        }

    public function page1(){
        if($this->session->userdata('logged')){

            $this->load->model('page1_model');

            $data["v_p2"] = $this->page1_model->v_p2($this->session->userdata('user_id'));

            $this->load->view('pages/page1', $data);

            $this->load->view('templates/footer');

        }

    }

Controller of page2:

class Page2 extends CI_Controller{

     public function __construct()
        {
            parent::__construct(); 
            $this->load->model('page2_model');

        }

    public function page2(){
        if($this->session->userdata('logged')){

            $this->load->model('page2_model');

            $data["get_p2"] = $this->page2_model->get_p2($this->session->userdata('user_id'));

            $this->load->view('pages/page2', $data);

            $this->load->view('templates/footer');

        }

    }

I changed view of page1 into:

  <option 
      value="<?php  echo base_url('page2/page2/').$row->p1_val.'/'.$row->p2_val;?>">
      <?php echo $row->p2_num; ?>
  </option>

No change in the controllers, just worked around with models by using:

$this->uri->segment();

- Most changes were then in the db queries I made.
- Since I added additional segment in page2, I added " ../ " to the paths of the assets.

sample - model for page2:

   function v_p2($data){
        $p1_val = $this->uri->segment(3);
        $p2_val = $this->uri->segment(4); 

         //DO smth with $p1_val & $p2_val
         //These values were used for querying


}

I'm still not sure I'm understanding you but you can do:

public function page2($var1, $var2){

}

http://localhost/project/page2/page2/5/2

where $var1 is 5, and $var2 is 2 in the above url