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 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突