【问题标题】:Redirecting user to the respective table cell clicked将用户重定向到单击的相应表格单元格
【发布时间】:2021-12-18 10:18:14
【问题描述】:

目前,我的视图类中有一个表,它使用 foreach 循环从后端填充其数据。我对表格的每一行都有一个编辑单元格,当单击 id 为 1 的单元格时,我想用它来将用户重定向到像 contacts/edit/1 这样的页面,并且该页面应该显示用户的所有数据 id 1.我正在使用以下代码来实现这一点:

查看类(index.php):

<?php
            foreach($records as $row ){                            
                ?>
            <tr>            
              <td><?= $row->refno ?></td>
              <td><?= $row->display_name ?></td>                  
              <td><a href="contacts/edit/'.$row->id.'">
              <span class="sr-only">edit</span></a>
              </td>
              <td></td>
            </tr>

控制器类:

public function lists($type='')
    {
        $main['records']=$this->contacts_model->get_records();
        $main['page'] = 'crm/contacts/index';
        $this->load->view('crm/index',$main);
    }

    public function edit($slug='')
    {
        $main['page'] = 'crm/contacts/edit';
        $this->load->view('crm/index',$main);
    }

模型类:

function get_records(){
        $this->db->select("*");
        $this->db->from("contacts");
        $this->db->where("status='Y'");
        $query = $this->db->get();
        return $query->result();
    }

// I want another method here that will fetch the details as per the id that was selected

所以这里我有 2 个问题,1 是我的 &lt;a&gt; 标签在我将其设置为 contacts/edit/'.$row-&gt;id.' 后并没有将我带到相应的 id,2 是我如何为使用该标签的人显示详细信息我的联系人/编辑视图类中的 id 1。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    解决您的第一个问题非常简单。你犯了一个简单的语法错误:

    错误:

    <?php
       ...
       <td><a href="contacts/edit/'.$row->id.'">
       ...
    

    正确:

    <?php
       ...
       <td><a href="<?= 'contacts/edit/'. $row->id ?>">
       ...
    

    注意:短格式回显 (&lt;?= "some text or variables" ?&gt;) 仅在 short_open_tags 标志在 php.ini 中启用时有效!! p>

    对于第二个问题,您需要在控制器中创建其他功能,以收集和显示基于 ID 的数据的视图。

    【讨论】:

    • 好的,我怎样才能将 id 值传递给我的控制器?
    • 你应该通过控制器函数的参数获取id值。
    • 您可以使用函数参数(在 $slugedit 函数中访问 URI 段(在本例中为 ID)。我假设您在 config/routes.php 中定义了路由。
    猜你喜欢
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多