用户
 找回密码
 入住 CI 中国社区
搜索
收起左侧

[已解决] 动态添加数据问题

[复制链接]
 楼主| 发表于 2017-3-15 14:19:37 | 显示全部楼层
Hex 发表于 2017-3-15 14:13
就是这样,你那里有问题么?

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

class Welcome extends CI_Controller {

        /**
         * Index Page for this controller.
         *
         * Maps to the following URL
         *                 http://example.com/index.php/welcome
         *        - or -
         *                 http://example.com/index.php/welcome/index
         *        - or -
         * Since this controller is set as the default controller in
         * config/routes.php, it's displayed at http://example.com/
         *
         * So any other public methods not prefixed with an underscore(强调) will
         * map to /index.php/welcome/<method_name>
         * @see https://codeigniter.com/user_guide/general/urls.html
         */
   
    public function index(){
        // 通过对象传递数据
        $data = new Testclass();
        $this -> load -> view('blogview',$data);
       // echo "这是第二个方法";
        }
}                         //////  控制器 (controller)    welcome
 -------------------------------

  这是一个自定义的类
  <?php
/**
*   这个类是我写在 libraries 里面的一个脚本文件,
*   我想在 view 层里面 调用这个loadfun() 方法 .
*   显示 echo 所输出的内容,,,
*/
class Testclass{

    protected  $CI;
    public function _construct() {

       $this -> $CI =& get_instance();
    }
    public function testlass() {

       echo $this ->loadfun();
    }
   
    public function loadfun() {

        echo "通过 this 加载数据";
    }
}

------------------------------------------
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>
   
<!-- 通过Testclass的对象 调用 调用自己的方法loadfun(), 显示出内容 -->
   <h1> 这是一个 view </h1>
</body>
</html>


 楼主| 发表于 2017-3-15 14:24:42 | 显示全部楼层
Hex 发表于 2017-3-15 14:13
就是这样,你那里有问题么?

 你的是一个数组,如果换成类的对象呢,类里面有一个方法,我在 view层里面调用方法。。ps:文档里面不是还有第二种方法了嘛?? 通过类的对象
发表于 2017-3-15 14:38:10 | 显示全部楼层
Android 发表于 2017-3-15 14:24
 你的是一个数组,如果换成类的对象呢,类里面有一个方法,我在 view层里面调用方法。。ps:文档里面不 ...

你的想法很好,但是,这不是 CI 里 MVC 的正确用法,正确(或者说合理)方式是,只传递给视图数据,不传递其他东西。但你非要传递对象你应该这样:
PHP复制代码
 
$test = new Testclass();
$data = array(
    'title' => 'My Title',
    'heading' => 'My Heading',
    'message' => 'My Message',
    'test' => $test,
);
 
$this->load->view('blogview', $data);
 
复制代码

然后视图里这样用:
HTML复制代码
 
<!DOCTYPE html>
<html lang="en">
<body>
<?=$test->loadfun()?>
</body>
</html>
 
复制代码

然后你的 Testclass 里不要 echo 任何内容,只采用 return 'xxx' 的形式,然后在视图里用 <?php echo $test->xxx(); ?> 或者 <?=$test->xxx()?> 的形式显示内容。

使用 MVC 框架后,从思考问题的方式上就要做一些转变,在 MVC 框架中,输出皆视图
 楼主| 发表于 2017-3-15 14:41:21 | 显示全部楼层
Hex 发表于 2017-3-15 14:38
你的想法很好,但是,这不是 CI 里 MVC 的正确用法,正确(或者说合理)方式是,只传递给视图数据,不传递 ...

啊啊啊,大哥你这一讲 茅塞顿开啊,我测试一下 谢谢你大哥,
发表于 2017-3-15 14:43:03 | 显示全部楼层
Android 发表于 2017-3-15 14:41
啊啊啊,大哥你这一讲 茅塞顿开啊,我测试一下 谢谢你大哥,

这些东西手册都说的很清楚,看手册不能只看皮毛,需要你多思考,多实践。编程就是这样一种活动,多思考,多实践,你才能有所提高。
 楼主| 发表于 2017-3-15 14:45:38 | 显示全部楼层
Hex 发表于 2017-3-15 14:43
这些东西手册都说的很清楚,看手册不能只看皮毛,需要你多思考,多实践。编程就是这样一种活动,多思考, ...

----当你使用对象时,对象中的变量会转换为数组元素。----  你指的是这句话把,我理解错了,我以为把整个对象里面的东西(变量,常量啊,方法,)当成一个数组,当时我就困惑了,,,,, 
发表于 2017-3-15 14:51:37 | 显示全部楼层
Android 发表于 2017-3-15 14:45
----当你使用对象时,对象中的变量会转换为数组元素。----  你指的是这句话把,我理解错了,我 ...

这其实不是重点,重点是,视图里其实就是执行了一次 extract() 函数,你可以看看这个函数是做什么的。 http://php.net/manual/zh/function.extract.php

看源码也是很重要,不要觉得看不懂,当你真的去看了,就会发现其实是能看懂的。
 楼主| 发表于 2017-3-15 14:55:45 | 显示全部楼层
Hex 发表于 2017-3-15 14:51
这其实不是重点,重点是,视图里其实就是执行了一次 extract() 函数,你可以看看这个函数是做什么的。 htt ...

好的,好的
 楼主| 发表于 2017-3-15 15:43:05 | 显示全部楼层
Hex 发表于 2017-3-15 14:51
这其实不是重点,重点是,视图里其实就是执行了一次 extract() 函数,你可以看看这个函数是做什么的。 htt ...

问题一:
  ××××××××××××××××××××××××××××××
  <?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

        /**
         * Index Page for this controller.
         *
         * Maps to the following URL
         *                 http://example.com/index.php/welcome
         *        - or -
         *                 http://example.com/index.php/welcome/index
         *        - or -
         * Since this controller is set as the default controller in
         * config/routes.php, it's displayed at http://example.com/
         *
         * So any other public methods not prefixed with an underscore(强调) will
         * map to /index.php/welcome/<method_name>
         * @see https://codeigniter.com/user_guide/general/urls.html
         */

    public function index(){

      /* $nji = $this->load->library('testclass');
        $data = array(
            'title' => 'My Title',
            'heading' => 'My Heading',
            'kiu' => $nji
        );

        $this -> load ->view('blogview.html',$data);*/

        $this -> load->library('testclass');

        $this ->testclass->loadfun();
        }
}

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
*   这个类是我写在 libraries 里面的一个脚本文件,
*   我想在 view 层里面 调用这个loadfun() 方法 .
*   显示 echo 所输出的内容,,,
*/
class Testclass{

    protected  $CI;
    public function _construct() {

       $this -> $CI =& get_instance();
    }
    public function testlass() {

       echo $this ->loadfun();
    }

    /*
     *  此注释 只针对列出的问题一, 使用return 不会显示任何内容  echo却能显示的,,,
     * */
    public function loadfun() {

        return  "通过 this 加载数据";
    }
}
 ***********************************************************************************************

问题二  在view 调用,还是不行,,,,
  <?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

        /**
         * Index Page for this controller.
         *
         * Maps to the following URL
         *                 http://example.com/index.php/welcome
         *        - or -
         *                 http://example.com/index.php/welcome/index
         *        - or -
         * Since this controller is set as the default controller in
         * config/routes.php, it's displayed at http://example.com/
         *
         * So any other public methods not prefixed with an underscore(强调) will
         * map to /index.php/welcome/<method_name>
         * @see https://codeigniter.com/user_guide/general/urls.html
         */

    public function index(){

       $nji = $this->load->library('testclass');
        $data = array(
            'title' => 'My Title',
            'heading' => 'My Heading',
            'kiu' => $nji
        );

        $this -> load ->view('blogview.html',$data);


       /* $this -> load->library('testclass');

        $this ->testclass->loadfun();*/
        }
}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>

   <h1> 这是一个 view </h1>

   <!--
      错误信息: Type: Error
       Message: Call to undefined method CI_Loader::loadfun()
   -->
  <!-- <?php

    echo $kiu ->loadfun();
    ?>-->


    <!--
       或是另一种写法, 没有任何数据输出
    -->
   <?php

    echo $nji ->loadfun();
   ?>
  
</body>
</html>

发表于 2017-3-15 16:03:05 | 显示全部楼层
Android 发表于 2017-3-15 15:43
问题一:
  ××××××××××××××××××××××××××××××
  -->

1. 你是在手冊的哪一頁看到這種用法?
$nji = $this->load->library('testclass');

2. 名稱錯了
testclass & testlass

3. Hex 已經有提過,不要用 echo 而是 return

 

本版积分规则