Right im new to this and a little out of my depth. Ok so I'm trying to bring in some data and im getting some errors and am I a little confused as to whats going on.
here is the model code:
public function get_webstore_sets($limit, $offset) {
$this->db->select("*");
$this->db->from('st_posts');
$this->db->where("type", "webstore");
$this->db->limit($limit, $offset);
$this->db->order_by('created', 'DESC');
$query = $this->db->get();
$data = $query->result_array();
return$data;
}
public function count_webstore_sets() {
$this->db->select("*");
$this->db->from('st_posts');
$this->db->where("type", "webstore");
$this->db->order_by('created', 'DESC');
$query = $this->db->get();
$count = $query->num_rows();
return $count;
}
and here is what im trying in the controller:
class Webstore extends MY_Controller {
public function __construct() {
parent::__construct();
$this->load->model("post");
$this->load->database();
$this->load->helper("url");
}
public function index() {
$this->data['posts'] = $this->post->get_webstore_sets($limit=null, $offset=null); // calling Post model method getPosts()
$data = array('page_title' => 'Store', 'video_content');
$this->layout("pages/webstore", $data);
}
}
and then bringing it in on HTML:
<?php foreach ($posts as $webstore): ?>
<div class="col-md-12" style="height: 225px;">
<div class="col-md-3">
<?php
$webstore['images'] = explode(".", $webstore['images']);
?>
<img class="thumbnail" width="250px" src="<?php echo $video['images'][1]; ?>.gif"/>
</div>
<div class="col-md-5">
<h3><?php echo $webstore['title']; ?></h3>
</div>
<div class="col-md-4 text-center">
Date Added: <?php echo date('d-m-y', strtotime($webstore['created'])) ?>
<hr>
Select or Exclusive to www.Borderlandbound.com<br> Please Visit Our Clips Store For <br>Many More Ultra-Hot Videos Like This
<hr>
</div>
</div>
<hr>
<?php endforeach; ?>
I know the connection is ok as the site is bringing in data from else where.
and this is the error im getting :
Severity: Notice
Message: Undefined variable: posts
Filename: pages/webstore.php
Line Number: 39
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: pages/webstore.php
Line Number: 39
I have blanked out the file path just in case ;) Edited for the above errors
In this code:
public function index()
{
$this->data['posts'] = $this->post->get_webstore_sets($limit=null, $offset=null); // calling Post model method getPosts()
$data = array('page_title' => 'Store', 'video_content');
$this->layout("pages/webstore", $data);
}
you are passing $data array to view, but not $this->data one. Also, 'video_content' element of $data array should have it's key to let you to approach to it in view. Or it is key itself missing value?
Change it to:
public function index() {
$this->data['posts'] = $this->post->get_webstore_sets($limit=null, $offset=null); // calling Post model method getPosts()
$this->data['page_title'] = 'Store';
$this->layout("pages/webstore", $this->data);
}
Now, you should be able to get $posts variable in view file. Just check what you have to do with value/key 'video_content'.
You are calling get_webstore_sets() without a parameter, so either fill in some parameters (limit, offset) or define the function as follows:
public function get_webstore_sets($limit=null, $offset=null) ...
I would suggest giving the parameter though in the function index() of class Webstore.
Related
I'm trying to use php build a blog website where all the posts are publicly available, but somehow I'm having issues getting the posts to actually display on the page.
The code for the query is:
<?php
class Blog {
private $db;
public function __construct(){
$this->db = new Database;
}
public function getBlogs(){
$this->db->query('SELECT * FROM posts');
$results = $this->db->resultSet();
return $results;
}
}
The code for generating the view is:
public function __construct(){
$this->blogModel = $this->model('Blog');
}
public function index(){
// Get posts
$posts = $this->blogModel->getPosts();
$data = [
'posts' => $posts
];
$this->view('pages/blog', $data);
}
And the actual HTML for the view looks like this:
<?php require APPROOT . '/views/inc/header.php'; ?>
<div class="row mb-3">
<div class="col-md-6">
<h1>ArtÃculos</h1>
</div>
</div>
<?php foreach($data['posts'] as $post) : ?>
<div class="card card-body mb-3 mt-3">
<h4 class="card-title"><?php echo $post->title; ?></h4>
More
</div>
<?php endforeach; ?>
<?php require APPROOT . '/views/inc/footer.php'; ?>
However, when I actually try to go to pages/blog, I get a Notice: Undefined index: posts in C:\xampp\htdocs\edutechne\app\views\pages\blog.php on line 7, meaning that the array is apparently not being passed to the view.
Any idea what might be wrong with my code?
EDIT: Someone asked that I include the code for the view so here it is:
public function view($view, $data = []){
// Check for view file
if(file_exists('../app/views/' . $view . '.php')){
require_once '../app/views/' . $view . '.php';
} else {
// View does not exist
die('View does not exist');
}
}
A workaround and maybe a cleaner way of coding would be declaring the $data array as a property:
class Blog {
private $db;
private $data;
...
Within your construction method you may now initiate the array like that:
public function __construct(){
$this->db = new Database;
$this->data = ['posts' => ['no posts']]
}
(The actual value depends on your data structure, passing a default value is not a bad idea. I just assumed an array holding one item 'no posts')
Within your index method you pass the results to the property:
public function index(){
...
$this->data['posts'] = $posts
...
Now you do not need to pass the $data to the method $Blog->view(), you can address it within. Remove the argument from the method declaration:
public function view($view){
...
And modify your for-each-loop like that:
<?php foreach($this->data['posts'] as $post) : ?>
But heads up: This way you need to make sure to properly populate and clean up the $data-property. It's now kind of persistant within the class instance.
I am not used to this way of coding in php, but to me it looks like your function named index is reason you can't get what you need. Try adding return $data to your code so it looks like this:
public function index(){
$posts = $this->blogModel->getPosts();
$data = [
'posts' => $posts
];
$this->view('pages/blog', $data);
return $data;
}
I'm learning CodeIgniter From this link. I've a little confusion in fetching the data from database.
<h2><?php echo $title; ?></h2>
<?php foreach ($news as $news_item): ?>
<h3><?php echo $news_item['title']; ?></h3>
<div class="main">
<?php echo $news_item['text']; ?>
</div>
<p>View article</p>
While displaying the data in view they have used a variable $news in foreach loop. However this variable is never defined anywhere
Copied from here
$query = $this->db->get('mytable');
// Produces: SELECT * FROM mytable
The second and third parameters enable you to set a limit and offset clause:
$query = $this->db->get('mytable', 10, 20);
// Produces: SELECT * FROM mytable LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)
You'll notice that the above function is assigned to a variable named $query, which can be used to show the results:
$query = $this->db->get('mytable');
foreach ($query->result_array() as $row)
{
echo $row['title'];
}
Try like this hope this will give you what you want simply make a controller and call the associated model in the constructor and perform the function what your want it will return the array so you will be eassilly fetch and use it in your view.
Controller code
class TestController extends Controller{
function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->model('User');
}
public function index(){
$data['all_user']=$this->User->getAllUser();
$this->load->view('index',$data);
}
}
this is my model code
class User extends CI_Model{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function getAllUser()
{
$this->db->select('*');
$this->db->from('users');
return $this->db->get()->result();
}
}
just follow the steps
make a controller
make a function inside the controller
call the associated model function into it and you will definately get the result.
Have you seen this Controller code ? (above the code that you pick, in the tutorial)
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');
}
$news is passed through as $data element from the Controller to the View
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'm working on a view that shows me the data from the database.
but the result is
Severity: Notice
Message: Undefined variable: data
Filename: mentee/index.php
UserModel.php
<?php
class UserModel extends CI_Model{
public function __construct(){
parent::__construct();
}
public function userProfile($nim){
$this->db->where('userNIM',$nim);
$query= $this->db->get('msuser');
return $query->result();
}
}
MainController.php
public function indexMentee()
{
$this->load->model('UserModel');
$userID=$this->session->userdata("userNIM");
$data['data'] = $this->UserModel->userProfile($userID);
$this->load->view('mentee/index', array('data' => $data));
}
View
<?php
die(var_dump($data));
if(is_array($data)){
foreach ($data as $profile):
?>
<div class="header-content">
<div class="header-content-inner">
<h1>Learning Center</h1>
<hr>
<h3> Welcome,<?php echo $profile->userName ?></h3>
</div>
</div>
<?php
endforeach;
}
?>
Please help me..Thanks a lot!
Instead of passing array('data' => $data)); just pass $data to the view.
First print session data. If session data present then pass data to view page
$data['details'] = $this->UserModel->userProfile($userID);
$this->load->view('mentee/index', $data);
And print print_r($details); on your view page
I am trying to access all the bands in my table and print them out in a list, but when I run it I get this error:
Severity: Notice
Message: Undefined property: CI_Loader::$model_bands
Filename: views/band_view.php
Line Number: 16
band_view.php:
<h3>List of Bands:</h3>
<?php
$bands = $this->model_bands->getAllBands();
echo $bands;
?>
model_bands.php:
function getAllBands() {
$query = $this->db->query('SELECT band_name FROM bands');
return $query->result();
}
Could someone pleease tell me why it is doing this?
Why do you need to do this, the correct way is to use the model methods inside a controller, then passing it to the view:
public function controller_name()
{
$data = array();
$this->load->model('Model_bands'); // load the model
$bands = $this->model_bands->getAllBands(); // use the method
$data['bands'] = $bands; // put it inside a parent array
$this->load->view('view_name', $data); // load the gathered data into the view
}
And then use $bands (loop) in the view.
<h3>List of Bands:</h3>
<?php foreach($bands as $band): ?>
<p><?php echo $band->band_name; ?></p><br/>
<?php endforeach; ?>
Did you load your model in the Controller?
$this->load->model("model_bands");
You need to change your code like
Controller
public function AllBrands()
{
$data = array();
$this->load->model('model_bands'); // load the model
$bands = $this->model_bands->getAllBands(); // use the method
$data['bands'] = $bands; // put it inside a parent array
$this->load->view('band_view', $data); // load the gathered data into the view
}
Then View
<h3>List of Bands:</h3>
<?php foreach($bands as $band){ ?>
<p><?php echo $band->band_name; ?></p><br/>
<?php } ?>
Your Model is ok
function getAllBands() {
$query = $this->db->query('SELECT band_name FROM bands');
return $query->result();
}
You forgot to load the model on your controller:
//controller
function __construct()
{
$this->load->model('model_bands'); // load the model
}
BTW, why are you calling the model directly from your view? Should it be:
//model
$bands = $this->model_bands->getAllBands();
$this->load->view('band_view', array('bands' => $bands));
//view
<h3>List of Bands:</h3>
<?php echo $bands;?>