For smart Primates & ROBOTS (oh and ALIENS ).

Blogroll

Friday, November 07, 2014

Pass sql result in codeigniter view

Problem:

PHP Codeigniter framework is a good MVC framework for PHP developer. While passwing result to it's view you may face the sql result problem. The problem is that you can not see sql result in the "View". You can face the belwo error:
-----------------------------------------------------
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: statename_list
( ! ) Fatal error: Call to a member function result() on a non-object in.
-----------------------------------------------------
So what is the proper method to pass sql result to Codeigniter view?

Solution:

You always pass array variable in the view to overcome this problem. Here I am showing the proper way that how you pass sql result to view 

Example:

IN CONTROLLER :
<?php
passed $statename_list data records from mysql table by controller:
$statename_list=$this->main_model->get_statenames();
$this->load->view('main_include/6_main_content_view',$statename_list);
?>

IN VIEW :
<?php
$this->load->database();
 foreach ($statename_list->result() as $row_state) {
 echo $row_state->statename;
 //$matter_id=$row_state->matter_id;
 }
?>

By using above statements you will get the listed below error:
---------------------------------------------------------------------------------
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: statename_list
( ! ) Fatal error: Call to a member function result() on a non-object in.
---------------------------------------------------------------------------------

PROPER SOLUTION: In controller use as:

$statename_list=$this->main_model->get_statenames();
$data['statename_list']=$statename_list;

and pass it in view as:
$this->load->view('main_include/6_main_content_view',$data);

and use it as in view:
    <?php foreach ($statename_list->result() as $row_state) {
 $statename= $row_state->statename;
 $state_id=$row_state->state_id;
 ?>
Share:

0 comments:

Post a Comment

Add Dependent Scripts

   here is the code that allow to add dependent script :         <script>         const addDependentScripts = async function( scriptsT...

Ads Inside Post

Powered by Blogger.

Arsip