如何以行格式显示从数据库到codeigniter视图的响应

时间:2017-12-23 21:16:14

标签: mysql codeigniter

我正在以行格式显示从数据库到codeigniter视图的数据,但现在问题是如果我显示两个不同的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>

1 个答案:

答案 0 :(得分:0)

这就是你如何做到的。你基本上需要一个主循环,如果它只是一个普通的表或一堆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.',
    ),
);