【问题标题】:Create link inside html table在 html 表中创建链接
【发布时间】:2023-03-21 03:51:01
【问题描述】:

我有下面的代码来生成表格

    $query = $this->search_employee->getAll($config['per_page'], $this->uri->segment(3));

    $this->table->set_heading('Name', 'Department');
    $tmp = array ( 'table_open'  => '<table border="0" cellpadding="2" cellspacing="1" width="70%">' );
    $this->table->set_template($tmp);

    $data['main_content'] = 'display_professors';
    $data['table'] = $this->table->generate($query);

我的问题是如何在 Name 列下的每一行下创建一个链接,链接到 view_employee/ID

我正在使用 codeigniter 的 table class

【问题讨论】:

    标签: php codeigniter html-table


    【解决方案1】:

    可能有更好的方法来做到这一点,但我只是遍历查询行来创建表行:

    if ($query->num_rows() > 0)
    {
        foreach ($query->result() as $row)
        {
            $this->table->add_row(anchor('view_employee/ID/' . $row->employee_id, $row->employee_name), $row->employee_department);
        }
    }
    
    $data['table'] = $this->table->generate();
    

    等等……

    更新:

    根据您对此答案的评论,与您自己的示例相比,我看不到此方法如何将 html 放入模型中。毕竟,无论哪种方式,您在视图中所做的都是&lt;?= $table; ?&gt;,或类似的。

    如果你绝对想将两者分开,你可以这样做:

    $data['rows'] = array();    
    
    if ($query->num_rows() > 0)
    {
        foreach ($query->result() as $row)
        {
            $this_row = array();
            $this_row['link'] = site_url('view_employee/ID/' . $row->employee_id);
            $this_row['employee_name'] = $row->employee_name;
            $this_row['employee_department'] = $row->employee_department;
    
            $data['rows'][] = $this_row;
        }
    }
    

    然后在视图中:

    <table border="0" cellpadding="2" cellspacing="1" width="70%">
      <tr>
        <th>Name</th>
        <th>Department</th>
      </tr>
    <? foreach($rows as $row): ?>
      <tr>
        <td><a href="<?= $row['link']; ?>"><?= $row['employee_name']; ?></a></td>
        <td><?= $row['employee_department']; ?></td>
      </tr>
    <? endforeach; ?>
    </table>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-02
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-09
      相关资源
      最近更新 更多