doutu2017 2016-11-20 14:15
浏览 55
已采纳

如何在codeigniter中循环表视图

I wnet through questions but cannot get it done Using CodeIgniter is it bad practice to load a view in a loop

from my controller i call my model and get an array , then i want to pass it to my view and i did that.

i loop through it and got the data inside the array i want to create a view for each item in the array and display them(like a list )

<?php

    $dataNumRows = count($database_data);
    for ($i = 0; $i < $dataNumRows; $i++) {
        $rows = array_values($database_data)[$i];
        $rawID = $rows["id"];
        $rawLink = $rows["link"];
        $rawHeading = $rows["heading"];
        $rawSubmitBy = $rows["submit_by"];
        echo $rawLink;
        echo $rawID;
        echo $rawHeading;
        echo $rawSubmitBy;
        echo "******<br>";
    }
    ?>


<table style="width:40%">

    <tr>
        <td colspan="1"></td>
        <td colspan="5">up vote</td>
        <td colspan="5"></td>
    </tr>
    <tr>
        <td colspan="1"><?php echo $rawID ?></td>
        <td colspan="5"><?php echo $rawLink ?></td>
        <td colspan="5">submitted by<?php echo $rawSubmitBy ?></td>
    </tr>
    <tr>
        <td colspan="1"></td>
        <td colspan="5">down vote</td>
        <td colspan="5">view comments</td>
    </tr>
</table>

</body>
</html>

This table is the view i need to recall each time my loop runs eg: 10 elements in array --> 10 table views

but this way i get only last element data as a single table view because i create table after the loop

how can i loop my table view inside the loop to get N number of tables (N= elements of array)?

  • 写回答

1条回答 默认 最新

  • douzuan2814 2016-11-20 14:42
    关注

    you can try this stop php , add your table, and resume php , inside your loop

    this should work !

    <!-- print the key as a var form the array data that passed from controller-->
    <!--<h1> Hello --><?php //echo $keyName;?><!-- </h1>-->
    
    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
    
    <!--use the key-->
    <!--<h1> Hello --><?php //echo $profile["fullName"]; ?><!-- </h1>-->
    
    
    <?php
    $dataNumRows = count($database_data);
    for ($i = 0; $i < $dataNumRows; $i++) {
        $rows = array_values($database_data)[$i];
        $rawID = $rows["id"];
        $rawLink = $rows["link"];
        $rawHeading = $rows["heading"];
        $rawSubmitBy = $rows["submit_by"];
        echo $rawLink;
        echo $rawID;
        echo $rawHeading;
        echo $rawSubmitBy;
        echo "******<br>";
    
        /* this will temporarily "stop" php --> */ ?>
        <table style="width:40%">
    
            <tr>
                <td colspan="1"></td>
                <td colspan="5">up vote</td>
                <td colspan="5"></td>
            </tr>
            <tr>
                <td colspan="1"><?php echo $rawID ?></td>
                <td colspan="5"><?php echo $rawLink ?></td>
                <td colspan="5">submitted by<?php echo $rawSubmitBy ?></td>
            </tr>
            <tr>
                <td colspan="1"></td>
                <td colspan="5">down vote</td>
                <td colspan="5">view comments</td>
            </tr>
        </table>
        <?php /* <-- php resumes now */
    
    }
    ?>
    
    
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵