如何从视图中传递控制器中的函数参数

时间:2017-11-14 08:14:42

标签: php codeigniter-3

我在控制器中有一个参数的函数,现在我想从视图到URL传递值。

courses.php

public function index($id){
   print_r();
   exit;
}

coursesview.php

<a class="jscroll-next" href="<?php echo base_url(); ?>/courses/index?page=<?php echo $nextPage; ?>&id=<?php echo $parentId ?>">next page</a>

怎么可能?

2 个答案:

答案 0 :(得分:0)

来自user guide, 只需将参数放在一个数组中,然后使用load->view()的第二个参数将其传递给视图。

之后,在视图中,变量将作为键使用。 在此示例中:$id$anothervar

<强> courses.php

public function index($id){
  $data = array('id' => $id, 'anothervar' => 'yeahi');
  $this->load->view('coursesview', $data);
  // exit;
}

<强> coursesview.php

echo $id; // the ID
echo $anothervar; // "yeahi"

答案 1 :(得分:0)

您将在URL中使用最小参数,这是我的建议

1.<a class="jscroll-next" href="<?php echo base_url('courses/$nextPage/$parentId/'); ?>">next page</a> //controller followed by arguments -  if you call index method

(OR)

2.<a class="jscroll-next" href="<?php echo base_url('courses/methodname/$nextPage/$parentId/'); ?>">next page</a> //controller,method name and followed by arguments

如何在控制器中获取这些参数 例如:

$nextPage =  $this->uri->segment(3);
$parentId =  $this->uri->segment(4);