【问题标题】:Call two models in a controller using Code Igniter使用 Codeigniter 在控制器中调用两个模型
【发布时间】:2013-08-14 09:02:38
【问题描述】:

我要做的是借助 Code Igniter 和 PHP 中的控制器在我的视图中显示两个模型。第一个模型在我看来成功显示,所以请不要关注它,但是我对第二个有困难。我无法在我的视图中显示它。

第二个模型包含一个 JSON 插件和 HTML 解析。我已经在 Code Igniter 控制器中对其进行了测试,它可以正常工作且没有错误,并给出了我想要的输出 - 一个 HTML 表,但现在我希望它把它用作模型并在另一个控制器中调用它:

class Telephonelist_model extends CI_Model{
    public function telephoneNum(){
        //authentication
        $username = '****';
        $password = '****';
        $sc = new ServerClient();
        $login = "******";
        $content = $sc->getContent($login,array(),false);   


        $host = "*******";
        $content = $sc->getContent($host);
        $content = json_decode($content);

        //Prepair for parsing, select the text part
        foreach($content->parse->text as $myHtml);
        /**
         * Parsing
         */
        //create new dom object
        $dom = new DOMDocument();

        //load html source
        $html = $dom->loadHTML($myHtml);

        //discard white space
        $dom->preserveWhiteSpace = false;

        //the table by its tag name
        $table = $dom->getElementsByTagName('table');

        //get all rows from the table
            $rows = $table->item(2)->getElementsByTagName('tr');
            $tab = '<table>';
            foreach ($rows as $row)
            {
                // get each column by tag name
                $head = $row->getElementsByTagName('th');
                // echo the values
                if(isset($head->item(0)->nodeValue)){
                //echo '<th>';
                    $tab = '<tr><th>'.$tab.$head->item(0)->nodeValue.' </th>';
                }
                if(isset($head->item(1)->nodeValue)){
                //echo '<th>';
                $tab = '<th>'.$tab.$head->item(1)->nodeValue.' </th>';
                }

                if(isset($head->item(2)->nodeValue)){
                $tab = '<th>'.$tab.$head->item(2)->nodeValue.'<br /></th></tr>';
                }
                $cols = $row->getElementsByTagName('td');
                // echo the values
                if(isset($cols->item(0)->nodeValue)){
                $tab ='<tr><td>'.$tab.$cols->item(0)->nodeValue.' </td>';
                }

                if(isset($cols->item(1)->nodeValue)){
                $tab = '<td>'.$tab.$cols->item(1)->nodeValue.' </td>';
            }

                if(isset($cols->item(2)->nodeValue)){
                $tab = '<td>'.$tab.$cols->item(2)->nodeValue.' <br /></td></tr></table>';
                }
            }
            return $tab;
    }

}

控制器:

<?php
class Page extends CI_Controller{

    /**
     * Startmethode des Controllers
     * @param string $param1
     * @param string $param2
     */
    public function index($param1=null,$param2=null){
        $this->myCal($param1,$param2);
    }   

    protected function myCal($year=null, $month=null){
        $year = Util_helper::getParamAsInt($year);
        $month = Util_helper::getParamAsInt($month);

        $this->load->model('Mycal_model');
        $data['calendar'] = $this->Mycal_model->generate($year,$month);


        //Get the telephonenumber list
        $this->load->model('Telephonelist_model');
        $data['tab'] = $this->Telephonelist_model->telephoneNum();

        $this->load->view('home',$data);
    }

在我包含以下行之前,我的视图工作正常: $data['tab'] = $this->Telephonelist_model->telephoneNum();之后,我的视图中什么都没有显示,即使是日历。

查看:

<!DOCTYPE html>
<html>
<head>
</head>

<body>
<div class="main"> 
       <div class = "table">

        <p>Telefonliste</p>
        <?php echo $tab;?>

    </div>

    <div class = "calender">

        <?php echo $calendar; ?>

    </div>

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

【问题讨论】:

  • 在加载视图之前你有没有 vardump($data['tab']),只是为了检查调用函数后有什么数据
  • 可能是某个地方的语法错误。尝试通过在 root 的 index.php 第 35 行执行此操作来显示错误: case 'development': error_reporting(E_ALL); ini_set('display_errors', '1');
  • @Deepanshu,我已经做到了,但没有任何输出。我认为问题在于: $data['tab'] = $this->Telephonelist_model->telephoneNum();因为我放了一个简单的回声“1”;在这一行之前,1 作为输出。
  • @thedjaney ,谢谢,现在我知道问题所在了:致命错误:找不到“ServerClient”类。 JSON 插件需要此​​ ServerClient,但不知何故找不到。
  • 表示你的函数电话号码没有执行

标签: php codeigniter


【解决方案1】:

尝试在开始时加载这两个模型。我经常遇到这个问题,并且总是需要这样做。

所以:

protected function myCal($year=null, $month=null){
    $this->load->model('Mycal_model');
    $this->load->model('Telephonelist_model');
    //the rest of your code

另外,检查这一行: foreach($content->parse->text as $myHtml);

也许它正在停止脚本的执行?我从来没有见过这样的foreach。

【讨论】:

  • 谢谢@Carlos Goce!但它没有帮助。我的视图中仍然没有显示任何内容。
  • 检查您的电话号码()是否正常工作。放一个返回“测试”;在方法或类似的东西之上。
  • 肯定是我的 JSON 插件有问题。我的 $content 是空的,因此 $myHtml 也是。
【解决方案2】:

尝试在 root 的 index.php 第 35 行中显示错误:

case 'development':
    error_reporting(E_ALL);
    ini_set('display_errors', '1');

【讨论】:

  • 非常感谢!很抱歉我不能投票给你们的 cmets,但我的声誉还不够高。
  • 我认为您可以通过单击“检查”图标来接受这个答案?
猜你喜欢
  • 2021-08-25
  • 2016-04-10
  • 1970-01-01
  • 1970-01-01
  • 2013-09-25
  • 1970-01-01
  • 2011-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多