【问题标题】:Codeigniter explode to html tableCodeigniter 爆炸成 html 表
【发布时间】:2015-09-30 05:07:25
【问题描述】:

我目前遇到了爆炸到 html 表格的问题。

我的控制器

public function idp_test()
{
    $this->load->model('groups/groups_model');
    $scholar_id = $this->session->userdata('scholar_id');
    $groups = $this->groups_model->retrieve_groups_idp($this->scholar->level_id);

    $content = array(
        'groups'            => $groups,
        'scholar'           => $this->scholar,
        'page_title'        => 'My individual development plan',
        'page_view'         => 'scholar/activities/idp_test',
        'page_scripts_view' => 'scholar/inc/_choose_activities_scripts',
        'sidebar_menu'      => 'idp test'
    );


    $this->load->view("theme/base", $content);
}

我的模型

public function retrieve_groups_idp($level_id)
{

    $this->db->select("groups.*, levels.name as levelname");
    $this->db->join('activities', 'groups.activity_ids = activities.activity_id' );
    $this->db->join('levels', 'levels.level_id = groups.level_id', 'left' );
    $this->db->join('activity_types', 'activities.activity_type_id = activity_types.activity_type_id', 'left' );
    $this->db->where('groups.level_id', $level_id);
    $this->db->group_by('groups_name');
    $this->db->order_by('groups_name');

    $qr = $this->db->get('groups');

    return $qr->result();
}

public function retrieve_activity_desc($activity_ids)
{
        $activity_desc_arr[] = explode(', ', $activity_ids);

        foreach ($activity_desc_arr as $activity_id){

            $this->db->select("activity_id as activityid, CONCAT(activities.name, ' - ', activity_types.name) AS description", FALSE);
            $this->db->join('activity_types', 'activities.activity_type_id = activity_types.activity_type_id');
            $this->db->where_in('activities.activity_id', $activity_id);

            $qr = $this->db->get('activities');

            return $qr->result();
        }
}

我的观点

<?php foreach ($groups as $groups): ?>
                        <table class="table table-vcenter">
                            <thead>
                                <tr class="active">
                                    <th><?php echo $groups->groups_name?></th> <!--Series Header -->
                                    <th class="text-center"><small><strong>Module Type</strong></small></th>
                                    <th class="text-center"><small><strong>Status</strong></small></th>
                                    <th class="text-center"><small><strong>Action</strong></small></th>
                                </tr>
                            </thead>
                            <tbody>
                                    <tr>
                                        <td><?php 
                                                $rows = $this->groups_model->retrieve_activity_desc($groups->activity_ids);
                                                    foreach ($rows as $row){
                                                        echo var_dump($row->description);
                                                    }
                                        ?></td>
                                        <td class="text-center">belom lagi</td>
                                        <td class="text-center">TO ATTEND</td>
                                        <td class="text-center"><a href="javascript:void(0)" class="label label-success">View Session</a></td>
                                    </tr>
                            </tbody>
                        </table>
                        <?php endforeach ?>

我无法发布图片,所以下面是目前看到的一些示例


FS 系列 1 |模块类型 |状态 |行动


系列 1 - FIL 系列 1 - CBL 系列 1 - PEL |贝隆拉吉|参加|


我需要实现的就像下面的示例视图


FS 系列 1 |模块类型 |状态 |行动


系列 1 - FIL |贝隆拉吉|参加|


系列 1 - CBL |贝隆拉吉|参加|


系列 1 - PEL |贝隆拉吉|参加|


我是 php 和 codeigniter 的新手,对不起我的英语。

谢谢

【问题讨论】:

    标签: php html codeigniter


    【解决方案1】:

    想想你的视图代码中有什么:

    <tr>
      <td><?php 
        foreach($rows as $row){
          var_dump($row->description);
        }
      ?>
      </td>
      <td>belom lagi</td>
      <td>to attend</td>    
    </tr>
    

    当然这些都会被打印到一个表格单元格中,它怎么可能产生其他东西,对吧?

    你需要的是这样的:

    <?php
      foreach($rows as $row){
        echo "<tr>"; // you need a new row for every entry
        echo "<td>".$row->description."</td>";
        echo "<td>belom lagi</td>";
        echo "<td>to attend</td>";
        echo "</tr>";
      }
    ?>
    

    【讨论】:

    • 嗨@ROAL,感谢您的评论,是的,它可以工作,但是当我输入这样的内容时出错:echo "查看会话";。你能帮我解决这个问题吗?
    • 您不能在双引号 echo 内使用双引号,因为它会提前结束字符串。您应该转义引号或使用撇号,例如:echo '&lt;td&gt;&lt;a href="javascript:void()" class="label label-success"&gt;View Session&lt;/a&gt;&lt;/td&gt;'; 或 echo "&lt;td&gt;&lt;a href=\"...\"&gt;...&lt;/a&gt;&lt;/td&gt;";。尝试在谷歌上搜索有关编程语言中“引用嵌套”的内容。
    【解决方案2】:

    经过一番研究,我想通了,只是想与大家分享。

    已编辑 - 控制器

    public function idp_test()
    {
        $this->load->model('groups/groups_model');
        $this->load->model('activities/activity_type_model');
    
        $scholar_id = $this->session->userdata('scholar_id');
        $groups = $this->groups_model->retrieve_groups_idp($this->scholar->level_id);
    
        foreach ($groups as $group) {
            $act_desc = $this->groups_model->retrieve_activity_desc($group->activity_ids);
            $group->activity_ids = $act_desc;
        }
    

    已编辑 - 查看

    <!-- START TABLE HEADER -->
                            <?php foreach ($groups as $group): ?>
                            <table class="table table-vcenter">
                                <thead>
                                    <tr class="active">
                                        <th><?php echo $group->groups_name?></th> <!--Series Header -->
                                        <th class="text-center"><small><strong>Module Type</strong></small></th>
                                        <th class="text-center"><small><strong>Status</strong></small></th>
                                        <th class="text-center"><small><strong>Action</strong></small></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($group->activity_ids as $session): ?>
                                        <tr>
                                            <td><?php echo $session->description; ?></td>
                                            <td class="text-center"><?php echo $session->modulename; ?></td>
                                            <td class="text-center">STATUS</td>
                                            <td class="text-center"><a href="javascript:void(0)" class="label label-success">View Session</a></td>
                                        </tr>
                                    <?php endforeach ?>
                                </tbody>
                            </table>
                            <?php endforeach ?>
    
                            <!-- END 1 -->
    

    根据我的需要出来。

    谢谢stackoverflow。

    【讨论】:

      猜你喜欢
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-03
      相关资源
      最近更新 更多