doupu7651 2012-04-21 21:53
浏览 67
已采纳

使用View中的参数链接到Controller函数(CodeIgniter)

Using CodeIgniter I am trying to create a link that the user can click within a view to show them the details of a record in my database.

In ASP.NET MVC3 with C# I would do it with @Html.ActionLink("Controller", "Action", new { id = item.Id})

This is my session controllers index() function

public function index()
{
        $data['sessions'] = $this->session_model->get_sessions();

        $this->load->view('_header');
        $this->load->view('session/index', $data);
        $this->load->view('_footer');
}

This is the index view it loads where I want to be able to click the link to go to enter() function

<table>

<th>Session Name</th>
<th>Description</th>
<th>Host</th>
<th></th>

<?php foreach ($sessions as $session_item): ?>
    <tr>
        <td> <?php echo $session_item['sessionName'] ?> </td>
        <td> <?php echo $session_item['sessionDesc'] ?> </td>
        <td> <?php echo $session_item['adminName'] ?> </td>
        <td> <a id="enterSession" href=<?php echo site_url("session/enter" + $session_item['id']) ?> >Enter</a></td>

    </tr>
<?php endforeach ?>

The enter session points me to the url "http://192.168.1.36/index.php/1" (if the id of my item is 1) whereas I expect "http://192.168.1.36/index.php/session/enter/1"

Any ideas on how I can make it call the enter() function also in the session controller (shown below)

 public function enter($id) {

        $this->load->view('_header');
        $this->load->view('session/enter');
        $this->load->view('_footer');
    }
  • 写回答

1条回答 默认 最新

  • douzhan1963 2012-04-21 22:15
    关注

    There seems to be a typo in the string concatenation in your PHP code. Perhaps it will work to use:

    site_url("session/enter" . $session_item['id'])
    

    ... rather than a + sign between the two strings.

    Regarding the second question - it looks correct as-is. Is it failing to call the session controller's enter() function passing the $id as an argument (assuming the URL is correct)?.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码