user2667837
user2667837

Reputation: 11

Call two models in a controller using Code Igniter

What I am trying to do is to display two models in my view with help of the controller in Code Igniter and PHP. The first model was displayed successfully in my view, so please don't pay attention to it, but I have difficulties with the second. I cannot display it in my view.

Second model contains a JSON plugin and HTML parsing. I have tested it in a Code Igniter Controller, which works fine with no errors and gives the output I want to have - a HTML table, but now I want it to use it as a model and call it in another controller:

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;
    }

}

Controller:

<?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);
    }

My view works fine until I include line: $data['tab'] = $this->Telephonelist_model->telephoneNum(); After that nothing is displayed in my view,even the calender.

View:

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

Upvotes: 0

Views: 221

Answers (2)

thedjaney
thedjaney

Reputation: 1126

Try showing the errors by doing this in root's index.php line 35:

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

Upvotes: 0

Carlos Goce
Carlos Goce

Reputation: 1665

Try loading both models on the beginning. I have this issue frecuently and always need to do this.

So:

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

Also, check this line: foreach($content->parse->text as $myHtml);

maybe it's stopping the execution of the script? i have never seen a foreach like that.

Upvotes: 1

Related Questions