Android 发表于 2017-3-15 11:09:22

动态添加数据问题

我通过类的对象 向视图里面动态添加数据,在视图文件里面我怎么调用类的方法。。。。。

Android 发表于 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>

Android 发表于 2017-3-15 14:19:13

Closer 发表于 2017-3-15 14:02
請提供你的原始碼
和你有疑問的地方
不然感覺我們在雞同鴨講

<?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>


Android 发表于 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>


Closer 发表于 2017-3-15 11:29:37

利用 Ajax 去調用
之後 JQuery 去改變畫面

Android 发表于 2017-3-15 11:33:49

Closer 发表于 2017-3-15 11:29
利用 Ajax 去調用
之後 JQuery 去改變畫面

还没有到网络请求那一步呢,只是在类里面写个方法,然后echo一个字符串, 怎么在视图里面调用这个方法。。。显示这个字符串。。。

Closer 发表于 2017-3-15 11:46:51

應該只有兩種方式:
1. 用 view() 的第二參數傳遞
2. 用 helper

Android 发表于 2017-3-15 11:49:38

Closer 发表于 2017-3-15 11:46
應該只有兩種方式:
1. 用 view() 的第二參數傳遞
2. 用 helper

大哥 能具体点嘛?

Closer 发表于 2017-3-15 12:17:20

Android 发表于 2017-3-15 11:49
大哥 能具体点嘛?

請參閱:
1. CI 手冊 - 視圖
2. CI 手冊 - 輔助函數

Android 发表于 2017-3-15 13:39:20

Closer 发表于 2017-3-15 12:17
請參閱:
1. CI 手冊 - 視圖
2. CI 手冊 - 輔助函數

我就是在  视图里面看到的  动态添加数据

Closer 发表于 2017-3-15 14:02:20

Android 发表于 2017-3-15 13:39
我就是在  视图里面看到的  动态添加数据

請提供你的原始碼
和你有疑問的地方
不然感覺我們在雞同鴨講

Hex 发表于 2017-3-15 14:13:30

Android 发表于 2017-3-15 13:39
我就是在  视图里面看到的  动态添加数据

$data = array(
    'title' => 'My Title',
    'heading' => 'My Heading',
    'message' => 'My Message'
);

$this->load->view('blogview', $data);
就是这样,你那里有问题么?
页: [1] 2 3 4
查看完整版本: 动态添加数据问题