我要做的是在Code Igniter和PHP中的控制器的帮助下,在我的视图中显示两个模型。第一个模型在我看来是成功展示的,所以请不要关注它,但我对第二个模型有困难。我无法在视图中显示它。
第二个模型包含JSON插件和HTML解析。我已经在代码点火器控制器中测试了它,它运行良好,没有错误,并给出了我想要的输出-一个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>
尝试在开始时加载两个模型。我经常遇到这个问题,而且总是需要这样做。
因此:
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为$myHtml);
也许它停止了脚本的执行?我从来没有见过这样的前臂。
尝试在root的index.php行35:中显示错误
case 'development':
error_reporting(E_ALL);
ini_set('display_errors', '1');