doupijin0397 2018-08-13 08:48
浏览 25
已采纳

如何将多个数据传递给codeigniter控制器

I am working on a template, i need to pass database results into jquery datatables in view page. now the problem is there is also a template named located at application/view/admin/layout.php that we use globally it includes head, footers, sidebars, and other global library. now the issue is i am not able to pass this template to controller.

Belwo is the my controller (application/controllers/admin/Applications.php

       <?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    class Applications extends MY_Controller {
        public function __construct(){
            parent::__construct();

        }

        public function appindex(){
            $data['view'] = 'admin/dashboard/index';
            $this->load->model('admin/App_model');
            $result['data']=$this->App_model->applists();
            $this->load->view('admin/layout', $data, $result);

        }               
            }

?>  

Below is the my model ( application/models/admin/App_model.php

    <?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class App_model extends CI_Model{

    public function applists(){

            $query=$this->db->query("select * from tblapps");
            return $query->result();

        }       
}
?>

this is the code for my global file (layout.php) view

            <section id="container">
            <!--header start-->
            <header class="header white-bg">
                <?php include('include/navbar.php'); ?>
            </header>
            <!--header end-->
            <!--sidebar start-->
            <aside>
                <?php include('include/sidebar.php'); ?>
            </aside>
            <!--sidebar end-->
            <!--main content start-->
            <section id="main-content">
                <div class="content-wrapper" style="min-height: 394px; padding:15px;">
                    <!-- page start-->
                    <?php $this->load->view($view);?>
                    <!-- page end-->
                </div>
            </section>
            <!--main content end-->
            <!--footer start-->
            <footer class="main-footer">
                <strong>Copyright © 2018 <a href="#">Indian I Services</a></strong> All rights
                reserved.
            </footer>
            <!--footer end-->
        </section>

this is my index.php in view page where i am passing database query results.

 <tbody>
            <?php
                 $i=1;              
                foreach($data as $row)
            {
               echo "<tr>";
               echo "<td>".$i."</td>";
               echo "<td>".$row->app_id."</td>";
               echo "<td>".$row->firstname."</td>";
               echo "<td>".$row->email."</td>";
                echo "<td>".$row->sdate."</td>";
               echo "</tr>";
               $i++;
             }
            ?>              
            </tbody>

its giving me error,

A PHP Error was encountered Severity: Notice Message: Undefined variable: data Filename: applications/index.php Line Number: 42

if i use this code in controller - it shows just table. but how to include that template file so it can show all header, footer, and menus.

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    class Applications extends MY_Controller {
        public function __construct(){
            parent::__construct();

        }

        public function appindex(){
            $this->load->model('admin/App_model');
            $result['data']=$this->App_model->applists();
            $this->load->view('admin/applications/index', $result);

        }               
            }

?>
  • 写回答

2条回答 默认 最新

  • doushichun9409 2018-08-13 09:15
    关注

    Hope this will help you :

    Pass both view and data results like this :

    public function appindex()
    {
        $result['view'] = 'admin/dashboard/index';
        $this->load->model('admin/App_model');
        $result['data'] = $this->App_model->applists();
        $this->load->view('admin/layout', $result);
    }
    

    Your view should be like this :

    <section id="main-content">
                <div class="content-wrapper" style="min-height: 394px; padding:15px;">
                    <!-- page start-->
                    <?php if ( !empty($view))
                    {
                       $this->load->view($view);
                    }?>
                    <!-- page end-->
                </div>
    </section>
    

    This is your data part

    <tbody>
      <?php
      if (! empty($data))
      {
        $i=1;              
        foreach($data as $row)
        {
          echo "<tr>";
          echo "<td>".$i."</td>";
          echo "<td>".$row->app_id."</td>";
          echo "<td>".$row->firstname."</td>";
          echo "<td>".$row->email."</td>";
          echo "<td>".$row->sdate."</td>";
          echo "</tr>";
          $i++;
        }
      }
      ?>              
    </tbody>
    

    For more : https://codeigniter.com/user_guide/general/views.html#adding-dynamic-data-to-the-view

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?