mysql - 如何以行格式显示从数据库到 codeigniter View 的响应

标签 mysql codeigniter

我正在以行格式向 codeigniter View 显示数据库中的数据,但现在的问题是,如果我显示两个不同的 id 数据,那么它会组合起来并在识别每个 id 数据时造成困惑。

这是当前的显示方式:

enter image description here

但我想这样显示:

id:
1

job title
Business Analyst

Experience Required
3 years 

id:
2 

job title
Business Analyst

Experience Required
3 years 

这是codeigniter查看表代码:

<table class="table table-bordered" style="margin-top:20px;" id="dataTable">
                    <thead>
                      <tr>
                        <th>
                        id
                        </th>
                      </tr>
                    </thead>
                    <tbody>
                    <?php
                    $ids = array();
                      foreach($resultadmin as $r){
                      $ids[] = $r['id'];
                    ?>
                      <tr id="banner_<?php echo $r['id'];?>">
                        <td>
                            <?php echo $r['id']; ?>
                        </td>   
                      </tr>
                    <?php
                      }
                    ?>
                    </tbody>
                </table>
                <table class="table table-bordered" style="margin-top:20px;" id="dataTable">
                    <thead>
                      <tr>
                        <th>
                        Job Title
                        </th>
                      </tr>
                    </thead>
                    <tbody>
                    <?php
                    $ids = array();
                      foreach($resultadmin as $r){
                      $ids[] = $r['id'];
                    ?>
                      <tr id="banner_<?php echo $r['id'];?>">
                        <td>
                            <?php echo $r['title']; ?>
                            <form method="" action="<?php  echo site_url(); ?>/Upload_Controller/" target="_blank"> 
                                <button type="submit" class="btn btn-success">Apply Job</button>
                            </form>
                        </td>
                      </tr>
                     <?php
                      }
                    ?>
                    </tbody>
                </table>

                <table class="table table-bordered" style="margin-top:20px;" id="dataTable">
                    <thead>
                      <tr>
                        <th>
                        Description
                        </th>
                      </tr>
                    </thead>
                    <tbody>
                    <?php
                    $ids = array();
                      foreach($resultadmin as $r){
                      $ids[] = $r['id'];
                    ?>
                      <tr id="banner_<?php echo $r['id'];?>">
                        <td>
                            <?php echo $r['description']; ?>
                        </td>
                      </tr>
                    <?php
                      }
                    ?> 
                    </tbody>
                </table>

                <table class="table table-bordered" style="margin-top:20px;" id="dataTable">
                    <thead>
                      <tr>
                        <th>
                        Summary
                        </th>
                      </tr>
                    </thead>
                    <tbody>
                    <?php
                    $ids = array();
                      foreach($resultadmin as $r){
                      $ids[] = $r['id'];
                    ?>
                      <tr id="banner_<?php echo $r['id'];?>">
                        <td>
                            <?php echo $r['summary']; ?>
                        </td>
                      </tr>
                    <?php
                      }
                    ?> 
                    </tbody>
                </table>
                <table class="table table-bordered" style="margin-top:20px;" id="dataTable">
                    <thead>
                      <tr>
                        <th>
                        Duties And Responsibilities
                        </th>
                      </tr>
                    </thead>
                    <tbody>
                    <?php
                    $ids = array();
                      foreach($resultadmin as $r){
                      $ids[] = $r['id'];
                    ?>
                      <tr id="banner_<?php echo $r['id'];?>">
                        <td>
                            <?php echo $r['responsibilities']; ?>
                        </td>
                      </tr>
                     <?php
                      }
                    ?>
                    </tbody>
                </table>

                <table class="table table-bordered" style="margin-top:20px;" id="dataTable">
                    <thead>
                      <tr>
                        <th>
                        Qualification
                        </th>
                      </tr>
                    </thead>
                    <tbody>
                    <?php
                    $ids = array();
                      foreach($resultadmin as $r){
                      $ids[] = $r['id'];
                    ?>
                      <tr id="banner_<?php echo $r['id'];?>">
                        <td>
                            <?php echo $r['qualification']; ?>
                        </td>
                      </tr>
                     <?php
                      }
                    ?>
                    </tbody>
                </table>

                <table class="table table-bordered" style="margin-top:20px;" id="dataTable">
                    <thead>
                      <tr>
                        <th>
                        Experience Required
                        </th>
                      </tr>
                    </thead>
                    <tbody>
                    <?php
                    $ids = array();
                      foreach($resultadmin as $r){
                      $ids[] = $r['id'];
                    ?>
                      <tr id="banner_<?php echo $r['id'];?>">
                        <td>
                            <?php echo $r['experience']; ?>
                        </td>
                      </tr>
                    <?php
                      }
                    ?> 
                    </tbody>
                </table>

                <table class="table table-bordered" style="margin-top:20px;" id="dataTable">
                    <thead>
                      <tr>
                        <th>
                        Details
                        </th>
                      </tr>
                    </thead>
                    <tbody>
                    <?php
                    $ids = array();
                      foreach($resultadmin as $r){
                      $ids[] = $r['id'];
                    ?>
                      <tr id="banner_<?php echo $r['id'];?>">
                        <td>
                            <?php echo $r['details']; ?>
                        </td>
                      </tr>
                    <?php
                      }
                    ?> 
                    </tbody>
                </table>

最佳答案

这就是你可以做到的。你基本上需要一个主循环,如果它只是一个普通的表或一堆 div 会更容易。

如果您想按名称排除字段,只需将其添加到array('id', 'title')即可。

<html>
    <head>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
    </head>
    <body>
        <?php
        foreach ($resultadmin as $jobs) {
            $id = $jobs['id'];
            foreach ($jobs as $k => $v) {
                if (in_array($k, array('id', 'title'))) {
                    continue;
                }
                ?>
                <table class="table table-bordered" style="margin-top:20px;" id="dataTable">
                    <thead>
                        <tr>
                            <th>
                                <?php
                                if ($k == 'title') {
                                    echo 'Job Title';
                                } else {
                                    echo ucfirst($k);
                                }
                                ?>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr id="banner_<?php echo $id; ?>">
                            <?php if ($k == 'title') { ?>
                                <td>
                                    <?php echo $v; ?>
                                    <form method="" action="<?php //echo site_url();   ?>/Upload_Controller/" target="_blank"> 
                                        <button type="submit" class="btn btn-success">Apply Job</button>
                                    </form>
                                </td>
                            <?php } else { ?>
                                <td><?php echo $v; ?></td>   
                            <?php } ?>
                        </tr>
                    </tbody>
                </table>
                <?php
            }
            echo '<hr>'; // might make things more clear...
        }
        ?>
    </body>
</html>

其中 $resultadmin 如下所示:

$resultadmin = array(
    0 => array(
        'id'               => 1,
        'title'            => '1Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
        'description'      => '1Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
        'summary'          => '1Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
        'responsibilities' => '1Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
        'qualification'    => '1Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
        'experience'       => '1Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
        'details'          => '1Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
    ),
    1 => array(
        'id'               => 2,
        'title'            => '2Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
        'description'      => '2Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
        'summary'          => '2Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
        'responsibilities' => '2Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
        'qualification'    => '2Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
        'experience'       => '2Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
        'details'          => '2Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
    ),
);

关于mysql - 如何以行格式显示从数据库到 codeigniter View 的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47956074/

相关文章:

mysql - 使用 codeigniter 和 innodb 引擎 mysql 一次插入两个表

php - codeigniter 中的错误处理

php - 找到 5 个最流行的 mysql 值

MYSQL 创建函数 - 获取 : Not allowed to return a result set from a function

mysql - 当mysql中的指定日期是今天时,如何更新表?

javascript - 使用jquery和ajax删除记录

angular - 通过 POST 将参数从 IONIC 传递到 PHP [CODEIGNITER]

php - 为教程博客创建面包屑菜单导航的技巧

php - 如何根据传递的变量发布数据

mysql - Ruby on Rails 如何处理 "Select count(*) from products group by category"?