dtu11716 2018-03-07 23:30
浏览 60
已采纳

代码点火器不显示我的数据(但没有显示错误)

EDIT : Does not work on external hosting AND on localhost… So I must be doing something wrong.

I am working with Code Igniter and I have a view. My view page shows no error, but doesn't show the data I requested either. Everythings fine with my config and database I guess... Any idea ? Am I doing anything wrong here? I am using latest version of mamp on mac 4.4.1 and latest version of code igniter 3.1.7.

Thanks guys in advance for your help ;)

View

    <?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
    <body>
<div>

         <p> <?php

        echo 'Trying to display:' . $data . ' yes?';

        ?> </p>


    </div>
</body>
</html>

Model

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

class Welcome_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }

    function get_secret($id)
    { 
            $this->db->where('id', $id);
            $this->db->select('secret');
            $query = $this->db->get('tch_api');

            if ($query->num_rows==1) {
                foreach ($query->result() as $row) {
//  tried with this line too : $secret = $row->secret; and without the following one same result 
                    $data[] = $row->secret;
                }
                return $data;
            }

    }
}

Controller

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

class Welcome extends CI_Controller {

    public function index()
    {

        $this->load->model('welcome_model');

        $secret = $this->welcome_model->get_secret(1);
        $data = array(
            'data' => $secret
        );
        $this->load->view('welcome_message', $data);

    }
}
  • 写回答

4条回答 默认 最新

  • dr200166 2018-03-08 17:47
    关注

    Did you try with isset? Please change the view like:

        <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    ?><!DOCTYPE html>
    <html lang="en">
    <head>
        <body>
    <div>
    
             <p> <?php
                  if (isset($data))
                   { echo 'Trying to display:' . $data . ' yes?';} else { echo 'No data found' ;} ?> </p>
    
    
        </div>
    </body>
    </html>
    

    UPDATE

    Assuming from your above code that you want to get the data of Id 1 from table and show it in your view.

    Please do the following changes and try again.

    Model

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Welcome_model extends CI_Model {
    
        function __construct()
        {
            parent::__construct();
        }
    
        function get_secret($id)
        { 
                $this->db->where('id', $id);
                $this->db->select('secret');
                $query = $this->db->get('tch_api');
             return $query->result();
    
        }
    }
    

    ?>

    Controller

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Welcome extends CI_Controller {
    
    public function __construct()
        {
            parent::__construct();
            $this->load->model('welcome_model','wl');
        }
    
    
        public function index()
        {
    
            $data['secret'] = $this->wl->get_secret(1);
            $this->load->view('welcome_message', $data);
    
        }
    }
    ?>
    

    View

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <title>Data Fetching</title>
    </head>
        <body>
    <div>
    
             <p><?php
             //testing 1
             if(isset($data)){var_dump($data);}
    
             //testing 2
                  if (isset($data))
                   { echo 'Trying to display:' . $data . ' yes?';} else { echo 'No data found' ;} ?> </p>
    
    
        </div>
    </body>
    </html>
    

    Please try this and post your result here..

    Please mark it as Answer if the issue has been Fixed.

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀