I have a list of messages and when the click on the title it takes them to another view where they can see the expanded message.
This is the view from which I click the link.
Postings View.
Link
Message Controller:
class Message extends CI_Controller {
var $TPL;
public function __construct()
{
parent::__construct();
}
private function display()
{
$query = $this->db->query("SELECT FROM messages WHERE id = '$id';");
$this->TPL['message'] = $query->result_array();
$this->template->show('Message', $this->TPL);
}
public function index()
{
$this->display();
}
}
Message View
<?$int=0;?>
<? foreach ($threads as $row) { ?>
<div class="row">
<div class="message">
<h3><?= $row['title']?></h3>
<p><?= $row['message']?></p>
<p><?= $row['member']?></p>
</div>
</div>
<hr>
<? $int++;?>
<? } ?>
This is easy. Change your code as follwoing
Link
Then change you display method as public and send ID param there like
public function display($id){
$this->db->where('id', $id);
$query = $this->db->get('messages');
$this->TPL['message'] = $query->result_array();
$this->template->show('Message', $this->TPL);
}
And finally remove $int=0 & $int++; from view file as you are not using this. Now test
Change the link
Link
And in your controller
public function display() {
$id=$this->uri->segment(3);
if($id==null) {
redirect('Index');
}
else {
$this->db->where('id', $id);
$query = $this->db->get('messages');
$this->TPL['message'] = $query->result_array();
$this->template->show('Message', $this->TPL);
}
}
Related
I am new to codeigniter and trying to get a percentage result outputted into the view of codeigntier for my project. Once I fix this, things will be much smoother.
He is the model function:
<?php
public function _construct()
{
$this->load->database();
}
public function percent_specilation1($numstudent)
{
$query = $this->db->query("SELECT * FROM Student WHERE Student.JRNOption ='1'");
$numstudent = $query->num_rows()/25;
if($query->num_rows() > 0){
return $numstudent;
}
return null;
}
?>
Here is the controller.
<?php
class Statscontroller extends CI_Controller {
public function _contruct(){
parent::_construct();
$this->load->model('Stats_model');
}
public function index(){
//echo "Various stats for the Journalism department:";
$data['Test'] = $this->Stats_model->percent_specilation1($numstudent);
}
public function view($page = 'Statsview')
{
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
//$data['title'] = ucfirst($page);
$this->load->view('templates/header',$data);
$this->load->view('Statscontroller/index',$data);
$this->load->view('templates/header');
}
?>
Here is the view
<html>
<h1> Various Statistics for the Journalism department</h1>
<?php
echo "Percent of students that are undecided:";
echo $Test; ?>
</html>
What am I doing wrong? I been trying to get it right for over an hour and I just want to output the single result from the database query. Any help you can provide would be great.. please be respectful. Thanks!
Try this code, I have updated some of code of controller and model and use primary key of student table for student_id in model query.
Update Model:
<?php
public function _construct()
{
$this->load->database();
}
public function percent_specilation1()
{
$query = $this->db->query("SELECT (SELECT COUNT(s2.student_id)
FROM Student s2
WHERE s2.JRNOption='1')*100/COUNT(s1.student_id) AS percentage
FROM Student s1");
$result = $query->row_array();
return $result['percentage'];
}
?>
Updated controller:
<?php
class Statscontroller extends CI_Controller {
public function _contruct(){
parent::_construct();
$this->load->model('Stats_model');
}
public function index(){
//echo "Various stats for the Journalism department:";
$data['Test'] = $this->Stats_model->percent_specilation1();
$this->load->view('templates/header',$data);
$this->load->view('Statscontroller/index',$data);
$this->load->view('templates/header');
}
}
?>
Hope this will help you.
I'm new in codeigniter, I'm having a challenge with num_rows on view page
Here's the sample of the code - Updated
Model
public function __construct()
{
parent::__construct();
}
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Referrals_form_model', 'referral'); /* LOADING MODEL * Referral_form_model as referral */
}
public function index()
{
$this->load->helper('url');
$this->load->view('referrals_form_view');
}
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
}
View
<span><?php
echo $query->num_rows();
?></span>
when i run the code, it tell me undefined variable "query"
Please help.
Thank you
Please change your code as below.
MODEL:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
View :
<span>
<?php
echo $total_referred_in;
?>
</span>
As you can see in Controller line :
$this->load->view("referral_form_view", $data);
$data here is the data array you are passing to your view. In your view you can access data in $data variable by using $data variable's KEY as php variable(In our case its "total_referred_in").
You aren't returning anything from your function, you're just echoing. Try this:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller:
public function referral_in()
{
$data['num_rows'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
And then in your view:
<span><?=$num_rows?></span>
I am a beginner in the CodeIgniter. Working on one small basic project, while I am working on the List view I get an error " Trying to get property of non-object.
Please help me!
Here the screen shots.
Error
My code
Here is my view :
<ul id="actions">
<h4>List Actions</h4>
<li> Add Task</li>
<li> Edit List</li>
<li><a onclick="return confirm('Are you sure?')" href="<?php echo base_url();?>lists/delete/<?php echo $list_detail->id;?>"> Delete List</a></li></ul>
<h1><?php echo $list_detail->list_name; ?></h1>
Created on : <strong><?php echo date("n-j-Y",strtotime($list_detail->create_date)); ?></strong>
<br/><br/>
<div style="max-width:500px;"><?php echo $list_detail->list_body; ?></div>
<!-- <?php //echo print_r($query); ?>
-->
<br/><br/>
Here is Controller
public function show($id){
$data['list_detail'] = $this->List_model->get_lists($id);
$data['main_content']='lists/show';
$this->load->view('layout/main',$data);
}
Here is model
public function get_list($id){
$query=$this->db->get('lists');
$this->db->where('id', $id);
return $query->row();
}
This error is generating because you are trying to access an array in class object style.
Ex:
$data['list_details'] = 'some value';
and accessing it like:
$data->list_details; // it will generate the error "Trying to get property of non object"
After viewing your code. I think you have written wrong in your model function
Your model function must be like below
public function get_list($id) {
$this->db->where('id',$id);
$query = $this->db->get('lists');
return $query->row();
}
Also in your view before you print the value please check it with the condition of !empty($list_details). so if value is not there still it will not throw any error.
I hope this will help you.
From your controller you are calling the model function as get_lists().
But there is no function as get_lists in model. U need to change the function name in controller to get_list()
When your using $query->row() on your model function then on controller
Model Function
public function get_list($id) {
$this->db->where('id',$id);
$query = $this->db->get('lists');
return $query->row();
}
Controller
public function show($id){
// Gets data where it belongs with that id
$list_data = $this->model_name->get_list($id);
$data['id'] = $list_data->id;
$data['create_date'] = $list_data->create_date;
$data['list_body'] = $list_data->list_body;
$this->load->view('someview', $data);
}
When your using $query->row_array(); on your model function then on controller
public function get_list($id) {
$this->db->where('id',$id);
$query = $this->db->get('lists');
return $query->row_array();
}
Controller
public function show($id) {
// Gets data where it belongs with that id
$list_data = $this->model_name->get_list($id);
$data['id'] = $list_data['id'];
$data['create_date'] = $list_data['create_date'];
$data['list_body'] = $list_data['list_body'];
$this->load->view('someview', $data);
}
On view then you can access
<?php echo $id;?>
<?php echo $create_date;?>
<?php echo $list_body;?>
Update for multiple data
public function get_list() {
$query = $this->db->get('lists');
return $query->result();
}
Controller
$results = $this->model_name->get_list();
// Or
//$results[] = $this->model_name->get_list();
$data['list_detail'] = $results;
$this->load->view('someview', $data);
View
<?php foreach ($list_detail as $list) {?>
<?php echo $list->id;?>
<?php echo $list->create_date;?>
<?php echo $list->list_body;?>
<?php }?>
I have the following code that query and display the result from database.
Database
id title desc
1 BUILD The
2 BEAUTIFUL Use
The piece of code in model folder
<?php
class Portfolio_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_webinfo()
{
$query = $this->db->select('title')->from('webinfo')->where('id', 1)->get();
return $query->row_array();
}
}
?>
The piece of code in controllers folder
<?php
class Portfolio extends CI_Controller {
public function view($portfolio = 'home')
{
if ( ! file_exists(APPPATH.'/views/portfolio/'.$portfolio.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($portfolio); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('portfolio/'.$portfolio, $data);
$this->load->view('templates/footer', $data);
}
public function __construct()
{
parent::__construct();
$this->load->model('portfolio_model');
$this->load->helper('url_helper');
}
public function index()
{
$data['webinfo'] = $this->portfolio_model->get_webinfo();
$this->load->view('templates/header', $data);
$this->load->view('portfolio/index', $data);
$this->load->view('templates/footer');
}
}
?>
The page that I want to display the data
<h2 class="intro-text text-center">
<strong><?php echo $webinfo['title']; ?></strong>
</h2>
However, I get the following error when I run the display page
A PHP ERROR WAS ENCOUNTERED
SEVERITY: NOTICE
MESSAGE: UNDEFINED VARIABLE: WEBINFO
May I know how should I go about editing the code to resolve the error?
Try 01
Controller
<?php
class Portfolio extends CI_Controller {
public function view($portfolio = 'home')
{
if ( ! file_exists(APPPATH.'/views/portfolio/'.$portfolio.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($portfolio); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('portfolio/'.$portfolio, $data);
$this->load->view('templates/footer', $data);
}
public function __construct()
{
parent::__construct();
$this->load->model('portfolio_model');
$this->load->helper('url_helper');
}
public function index()
{
$data['webinfo'] = $this->portfolio_model->get_webinfo();
print_r($data['webinfo']);
/* $this->load->view('templates/header', $data);
$this->load->view('portfolio/index', $data);
$this->load->view('templates/footer');*/
}
}
?>
Model
<?php
class Portfolio_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_webinfo()
{
$query = $this->db->query("SELECT title FROM webinfo WHERE id = 1");
$result = $query->result_array();
return $result;
}
}
?>
view
<h2 class="intro-text text-center">
<strong><?php echo (!empty($webinfo[0]['title'])) ? $webinfo[0]['title'] : 'Empty Title' ;; ?></strong>
</h2>
Try this
In Model
public function get_webinfo()
{
$query = $this->db->query("SELECT title FROM webinfo WHERE id = 1");
$result = $query->result_array();
return $result;
}
In View
<h2 class="intro-text text-center">
<strong><?php echo (!empty($webinfo['title'])) ? $webinfo['title'] : 'Empty Title' ;; ?></strong>
</h2>
Note: If I'm right sometimes this $webinfo['title'] gives error and its work fine with with $webinfo[0]['title']
And there is way to write controller.
__construct
index()
then other all functions
EDIT 01
public function index()
{
$data['webinfo'] = $this->portfolio_model->get_webinfo();
print_r($data['webinfo']);
/* $this->load->view('templates/header', $data);
$this->load->view('portfolio/index', $data);
$this->load->view('templates/footer');*/
}
controller:
public function index()
{
$data['result'] = $this->portfolio_model->get_webinfo();
//$this->load->view('templates/header', $data);
//$this->load->view('portfolio/index', $data);
// $this->load->view('templates/footer');
print_r($data);
}
model
public function get_webinfo()
{
$this->db->select('*');
$this->db->from('pwebinfo');
$this->db->where('id',1);
$query = $this->db->get();
if($query->num_rows() ==''){
return 'failure';
}else{
$results = $query->result();
$result = $results[0];
return $result;
}
}
view
<h2 class="intro-text text-center">
<strong> <?php echo $result[0]->title; ?> ?></strong>
</h2>
I'm new to Codeigniter and i have been trying to develop some part using it.
On my header file, i need to load my menu items and i have create a menu controller, menu model and a view.
Controller page
<?php
class Menu extends CI_Controller
{
public function __construct(){
parent::__construct();
$this->load->model('menu_model');
}
public function index(){
$data['menuArray'] = $this->mainMenuDataLoad();
if($data['menuArray']){
$this->load->view('menu' , $data);
}
}
public function mainMenuDataLoad(){ /* create menus Array */
$rootMenuData = $this->menu_model->loadManuData();
if($rootMenuData){
for($e=0; $e<count($rootMenuData); $e++){
if($rootMenuData[$e]){
$data[$e] = array(
'title' => $rootMenuData[$e]['title'],
'menu_id' => $rootMenuData[$e]['menu_id'],
'url' => $rootMenuData[$e]['url'],
'menu_icon' => $rootMenuData[$e]['menu_icon'],
);
$get_sub = $this->mainMenuDataLoad($rootMenuData[$e]['menu_id']);
if($get_sub){
$data[$e]['sub'] = $get_sub;
}
}
}
return $data;
}
return false;
} }
this is my model page
class Menu_model extends CI_Model{
public function loadManuData(){
$this->db->select("*");
$this->db->from('tbl_menu');
$this->db->order_by("order", "DESC");
$query = $this->db->get();
if ($query->num_rows() > 0) {
$r=0;
foreach ($query->result() as $row) {
$data[$r]['root_id'] = $row->root_id;
$data[$r]['menu_id'] = $row->menu_id;
$data[$r]['title'] = $row->title;
$data[$r]['url'] = $row->url;
$data[$r]['menu_icon'] = $row->menu_icon;
$r++;
}
return $data;
}
return false;
}
public function __construct(){
parent::__construct();
}}
andon my menu view page i am looping the menu data.
But on my header.php if i try to call the menu controller like this
$this->load->controller('menu');
it gives me an error like this.
Fatal error: Call to undefined method CI_Loader::controller() on header.php
What am i doing wrong?.
Someone please guide me.
thanks in advance
menu.php view Page
<ul class="nav navbar-nav">
<?php
print_r($menuArray);
for($q=1; $q<count($menuArray); $q++){
?><li>
<a href="<?php echo base_url($menuArray[$q]['url']);?>">
<span class="<?php echo $menuArray[$q]['menu_icon'];?>">
<?php echo $menuArray[$q]['title'];?>
</span>
</a>
</li>
<?php }
?>
</ul>
you cant call a controller from view
i.e. in view page writing this code $this->load->controller('menu'); is not permissible.
The controller loads the view and model, its the controller that is the prime here
[More Edit:]
change your model to this
class Menu_model extends CI_Model{
public function __construct(){
parent::__construct();
}
public function get_menu()
{
$this->db->select("*");
$this->db->from('tbl_menu');
$this->db->order_by("order", "DESC");
$query = $this->db->get();
return $query;
}
}?>
then in the controller do this
public function index(){
$data['menuArray'] = $this->menu_model->mainMenuDataLoad()->result_array();
$this->load->view('header', $data);
$this->load->view('menu'); // u are passing data from here//
$this->load->view('landing_page');
$this->load->view('footer');
}
and finally the view
<?php
if(count($menuArray)>0)
{
for($q=0; $q<count($menuArray); $q++){
?><li>
<a href="<?php echo base_url($menuArray[$q]['url']);?>">
<span class="<?php echo $menuArray[$q]['menu_icon'];?>">
<?php echo $menuArray[$q]['title'];?>
</span>
</a>
</li>
<?php }
}?>
You also dont need the public function mainMenuDataLoad() function in controller
Better solution if you make a BaseController with a function and call it from extended controllers.
BaseController:
class BaseController extends CI_Controller
{
protected $data = array();
function __construct() {
parent::__construct();
$this->load->model('my_model');
}
protected function LoadContView($aContentView) {
$this->data['menu'] = $this->my_model->getMenu();
$this->load->view('common/ViHeader', $this->data);
$this->load->view($aContentView, $this->data);
$this->load->view('common/ViSidebar', $this->data);
$this->load->view('common/ViFooter', $this->data);
}
Mypage:
class Mypage extends BaseController{
function __construct() {
parent::__construct();
}
public function index() {
$this->LoadContView('my_view');
}
}
also use foreach( $menu_array as $menu_item) :)