Undefined variable & Invalid argument supplied for foreach() in CodeIgniter - php

I have the following code that query the database and display it in view.
However, I am getting these error:
- Message: Undefined variable: portfolio Filename: portfolio/home.php
- Message: Invalid argument supplied for foreach() Filename: portfolio/home.php
How should I resolve the error?
Controller (Portfolio.php)
<?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['portfolio'] = $this->portfolio_model->get_portfolio();
$this->load->view('templates/header', $data);
$this->load->view('portfolio/home', $data);
$this->load->view('templates/footer');
}
}
?>
Model (Portfolio_model)
<?php
class Portfolio_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_portfolio()
{
$query = $this->db->select('title')->from('webinfo')->get();
return $query->result_array();
}
}
?>
View (home.php)
<?php foreach ($portfolio as $portfolio_item): ?>
<h3><?php echo $portfolio_item['title']; ?></h3>
<?php endforeach; ?>
routes.php
$route['portfolio/(:any)'] = 'portfolio/view/$1';
$route['portfolio'] = 'portfolio';
$route['default_controller'] = 'portfolio/view';
$route['(:any)'] = 'portfolio/view/$1';

Couple of things may work
Model
public function get_portfolio() {
$query = $this->db->get('webinfo');
if ($query->num_rows() > 0 ) {
return $query->result_array();
} else {
return FALSE;
}
}
On controller
<?php
class Portfolio extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('portfolio_model');
$this->load->helper('url');
}
public function index() {
$data['portfolio'] = array();
$results = $this->portfolio_model->get_portfolio();
// You may need to remove !
if (!isset($results) {
foreach ($results as $result) {
$data['portfolio'][] = array(
'title' => $result['title']
);
}
}
$this->load->view('templates/header', $data);
$this->load->view('portfolio/home', $data);
$this->load->view('templates/footer');
}
public function view($portfolio = 'home') {
if (!file_exists(APPPATH.'/views/portfolio/'.$portfolio.'.php')) {
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);
}
}
View
<?php if ($portfolio) {?>
<?php foreach ($portfolio as $portfolio_item) { ?>
<h3><?php echo $portfolio_item['title']; ?></h3>
<?php } ?>
<?php } else { ?>
<h3>No Result</h3>
<?php } ?>
I also would recommend auto loading the database
$autoload['libraries'] = array('database');
Instead of using $this->load->database();

Related

Undefined variable in foreach loop in Codeigniter

I am a beginner in codeigniter and I am making a basic crud for learning. I am facing an error which is in index.php
<h2><?=$title?></h2>
<?php foreach($posts as $post)?>
<h3><?php echo $post['title'];?></h3>o
<small class="post-date">Posted on:<?php echo $post['created at'];?> </small><br>
<?php echo word_limiter($post['body'],60);?>
<p><a class="btn btn-default" href="<?php echo site_url('/posts/' .$post['slug']);?>">read more</a></p>
<?php endforeach;?>
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: posts
Filename: posts/index.php
Line Number: 2
Backtrace:
File: /opt/lampp/htdocs/codeigniter/application/views/posts/index.php
Line: 2
Function: _error_handler
File: /opt/lampp/htdocs/codeigniter/application/controllers/Posts.php
Line: 23
Function: view
File: /opt/lampp/htdocs/codeigniter/index.php
Line: 315
Function: require_once
my controller is
<?php
class Posts extends CI_Controller {
public function index(){
$data['title']='Latest Posts';
$data['posts']=$this->Post_model->get_posts();
$this->load->view('templates/header');
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
public function view($slug=NULL)
{
$data['post'] = $this->Post_model->get_posts($slug);
if(empty($data['post'])){
show_404();
}
$data['title']=$data['post']['title'];
$this->load->view('templates/header');
$this->load->view('posts/view', $data);
$this->load->view('templates/footer');
}
public function create(){
$data['title'] = 'create post';
$this->form_validation->set_rules('title','Title','required');
$this->form_validation->set_rules('body','body','required');
if($this->form_validation->run() === false) {
$this->load->view('templates/header');
$this->load->view('posts/create',$data);
$this->load->view('templates/footer');
} else {
$this->Post_model->create_post();
redirect('posts');
}
}
public function delete($id) {
$this->Post_model->delete_post($id);
redirect('posts');
}
public function edit($id) {
$data['post'] = $this->Post_model->get_posts($slug);
if(empty($data['post'])){
show_404();
}
$data['title'] = 'edit post';
$this->load->view('templates/header');
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
public function update()
{
$this->Post_model->update_post();
redirect('posts');
}
}
my model file is
<?php
class Post_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function get_posts($slug = false){
if($slug === false) {
$this->db->order_by('id','DESC');
$query=$this->db->get('posts');
return $query->result_array();
}
$query = $this->db->get_where('posts',array('slug' => $slug));
return $query->row_array();
}
public function create_post(){
$slug=url_title($this->input->post('title'));
$data=array(
'title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body')
);
return $this->db->insert('posts',$data);
}
public function delete_post($id){
$this->db->where('id',$id);
$this->db->delete('posts');
return true;
}
public function update_post() {
$slug=url_title($this->input->post('title'));
$data=array(
'title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body')
);
$this->db->where('id',$this->input->post('id'));
return $this->db->update('posts',$data);
}
}
On your edit function on controller you have edit($id)
But where you load your model you have get_posts($slug) it should be get_posts($id)
And also change $data['post'] to $data['posts']
public function edit($id) {
$data['posts'] = array();
// rename slug to id
$data['posts'] = $this->post_model->get_posts($id);
if(empty($data['posts'])){
show_404();
}
$data['title'] = 'edit post';
$this->load->view('templates/header');
/*
Tip You don't have to use index.php on for view you could create a
another view instead $this->load->view('posts/edit_view',$data);
*/
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
On the when load model on controller
Change
$this->Post_model->get_posts($id);
To
$this->post_model->get_posts($id);
https://www.codeigniter.com/user_guide/general/models.html#loading-a-model
On Autoload.php
Change
$autoload['model'] = array('Post_model');
To
$autoload['model'] = array('post_model');
And load database in autoload.php make life easier
$autoload['libraries'] = array('database');

CI 2.1.1 documentation The page you requested was not found

I Have following step by step the documentation in news section. In news, CI can open data from database. in database there are 2 data like this
Put news data from database
In there, there is view article link. but after click the link, CI open error 404 like this
News Item from View article
can help me to solve the problem? i'm sorry my english is bad
News_model.php
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
}
Controller (News.php)
<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
public function view($slug)
{
$data['news'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
}
Index.php in news View
<?php foreach ($news as $news_item): ?>
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
<p>View article</p>
<?php endforeach ?>
view.php in view
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
You haven't completed the "Routing" section at the end of that tutorial.
Also, don't start learning with CodeIgniter 2.x - it's no longer supported - go for version 3 instead.

Query and display result in CodeIgniter

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>

View data from a controller function with 2 models

I trying to get Banner Images from DB via controller and model.
My controller:
class Home extends CI_Controller {
//put your code here
public function __construct() {
parent::__construct();
$this->load->model('home_model');
}
public function index() {
$data = array();
$data['bannerInfo'] = $this->home_model->selectBanner($banner_id);
$data['result'] = $this->home_model->selectCategory($category_id);
$data['banner'] = $this->load->view('banner',$data, TRUE);
$data['maincontent'] = $this->load->view('home_message',$data,TRUE);
$data['title'] = 'NZ Furniture Products ';
$data['keywords'] = 'furniture bangladesh';
$this->load->view('home', $data);
}
My model class:
class Home_Model extends CI_Model {
// put your code here
public function selectCategory($category_id)
{
$this->db->select('*');
$this->db->from('tbl_category');
$this->db->order_by("category_id", "desc");
$query_result= $this->db->get();
$results = $query_result->result();
return $results;
}
public function selectBanner($banner_id)
{
$this->db->select('*');
$this->db->from('tbl_banner');
$this->db->where('banner_id',$banner_id);
//$this->db->order_by("product_id", "desc");
$query_result = $this->db->get();
$results = $query_result->result();
return $results;
}
$
Views:
home_message:::
<?php foreach ($result as $values) { ?>
<div class="single_product">
<div class="product_image">
<img src="<?php echo base_url();?><?php echo $values->category_image ?>" />
</div>
<span class="category_title"> <?php echo $values->category_name ?> </span>
</div>
<?php } ?>
banner
<div>
<?php foreach ($bannerInfo as $values) { ?>
<?php echo $values->banner_image ?>
<?php } ?>
</div>
If you want to show all banners. You have to remove where clause. Default ordering is desc.
Controller file:
class Home extends CI_Controller
{
public function __construct() {
parent :: __construct();
$this->load->model('home_model');
}
public function index() {
$data = array();
$data['bannerInfo'] = $this->home_model->selectBanner();
$data['result'] = $this->home_model->selectCategory($category_id);
$data['banner'] = $this->load->view('banner', $data, TRUE);
$data['maincontent'] = $this->load->view('home_message', $data, TRUE);
$data['title'] = 'NZ Furniture Products ';
$data['keywords'] = 'furniture bangladesh';
$this->load->view('home', $data);
}
}
Model file:
class Home_Model extends CI_Model
{
public function selectCategory($category_id)
{
$this->db->select('*');
$this->db->from('tbl_category');
$this->db->order_by("category_id", "desc");
$query_result = $this->db->get();
$results = $query_result->result();
return $results;
}
public function selectBanner($order_by = "desc")
{
$this->db->select('*');
$this->db->from('tbl_banner');
$this->db->order_by("banner_id", $order_by);
return $this->db->get()->result();
}
}

CodeIgniter News Tutorial - Delete and Update

I am new to CodeIgniter, and I have followed the tutorial to make a news application. I am now on deleting and updating the news article but I can't figure out how to do it.
This is what I have come up with so far, but it doesn't work because when I click delete article in index all that happens is that I get an error message saying : webpage cannot be found.
news controller :
<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
public function view($slug)
{
$data['news'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
else
{
$this->news_model->set_news();
$this->load->view('news/success');
}
}
public function delete($id) {
$this->news_model->delete_news($id);
$this->load->helper('url');
redirect('/news');
}
}
news_model:
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
public function set_news()
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text')
);
return $this->db->insert('news', $data);
}
public function delete_news($id)
{
$this->db->delete('news', array('id' => $id));
}
}
index :
<?php foreach ($news as $news_item): ?>
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
<p>View article</p>
<p>delete article</p>
<?php endforeach ?>
you have to use absolute path
delete article
also in your controller after delete
instead of
redirect('/news');
use absulot path or you can simply write
$this->index();

Categories