Php 使用代码点火器调用控制器中的两个模型
我试图在CodeIgniter和PHP中的控制器的帮助下,在视图中显示两个模型。第一个模型在我的视图中显示成功,所以请不要注意它,但我对第二个模型有困难。我无法在我的视图中显示它 第二个模型包含一个JSON插件和HTML解析。我已经在CodeIgniter控制器中对其进行了测试,该控制器工作正常,没有错误,并提供了我想要的输出—一个HTML表,但现在我希望它将其用作模型,并在另一个控制器中调用它:Php 使用代码点火器调用控制器中的两个模型,php,codeigniter,Php,Codeigniter,我试图在CodeIgniter和PHP中的控制器的帮助下,在视图中显示两个模型。第一个模型在我的视图中显示成功,所以请不要注意它,但我对第二个模型有困难。我无法在我的视图中显示它 第二个模型包含一个JSON插件和HTML解析。我已经在CodeIgniter控制器中对其进行了测试,该控制器工作正常,没有错误,并提供了我想要的输出—一个HTML表,但现在我希望它将其用作模型,并在另一个控制器中调用它: class Telephonelist_model extends CI_Model{ p
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);
}
控制器:
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\u model->telephoneNum;在此之后,我的视图中没有显示任何内容,即使是日历
视图:
尝试在开始时加载两个模型。我经常遇到这个问题,而且总是需要这样做 因此: 此外,请选中此行: foreach$content->parse->text作为$myHtml
也许它会停止脚本的执行?我从未见过这样的foreach。尝试在root的index.php第35行中显示错误:
case 'development':
error_reporting(E_ALL);
ini_set('display_errors', '1');
在加载视图之前,您是否使用vardump$data['tab'],只是为了检查调用函数后会出现什么数据?可能是某个地方的语法错误。尝试在root的index.php第35行显示错误:case'development:error\u reportingE\u ALL;ini_设置“显示错误”,“1”@Deepanshu,我已经做了,但是没有任何东西可以作为输出。我认为问题在于:$data['tab']=$this->Telephonelist\u model->telephoneNum;因为我放了一个简单的回声1;在这一行之前,1作为输出。@thedjaney,谢谢,现在我知道了问题:致命错误:找不到类“ServerClient”。JSON插件需要此服务器客户端,但不知何故找不到它。这意味着您的函数telephoneNum未执行谢谢@Carlos Goce!但这没有帮助。我的视图中仍然没有显示任何内容。请检查您的电话号码是否正常工作。进行一次“测试”;当然,我的JSON插件有问题。我的$content是空的,因此$myHtml也是空的。非常感谢!很抱歉,我不能投票支持你的评论,但我的声誉不够高。我想你可以通过点击复选图标来接受这个答案?