I am newbie in CodeIgniter. When I try to data passing with controller and view and model, I have got this:
Syntax error: Unexpected '$data'(T_VARIABLE) in c:\xampp\htdocs\ci\application\controllers\users.php on line 10
Here is my controller users.php code:
<?php
class Users extends CI_Controller
{
public function show()
{
$data['results']=$this->user_model->get_users();
$this->load->view('user_view',$data);
}
}
?>
At views folder user_view.php code:
<body>
<?php
foreach($results as $object){
echo $object->username;
}
?>
</body>
At model folder user_model.php code
<?php
class User_model extends CI_Model
{
public function get_users()
{
$query=$this->db->get('users');
return $query->result();
}
}
?>
How can I solve this error?
Why do your error says c:\xampp\htdocs\ci\application\users.php shouldn't it have been c:\xampp\htdocs\ci\application\controller\users.php`
In your controller
public function show()
{
$data['results']=$this->user_model->get_users();
$this->load->view('user_view',$data);
}
In your model
public function get_users(){
$query=$this->db->get('users');
$result = $query->result_array(); //added
return $result; //added
}
In view
<body>
<?php
foreach($results as $new_results) //changed
{
echo $new_results['username']; //changed
}
?>
</body>
You are trying to access wrong ARRAY in your view.
In your controller array is $data['results'] not $data['result']. So you need to access $results in foreach loop.
In your view Change from
foreach($result as $object){
To
foreach($results as $object){
Use $results in foreach loop and check
First of all, you have to load your model as:
$this->load->model('User_model','user_model');
Then you can use the function defined in that model as :
$this->user_model->get_users();
You have one more error in your code i.e. you have not loaded the model in the function
update your code to add the following line in first line of show() function
$this->load->model('user_model');
Related
I'm using the CI framework and I'm trying to make a foreach loop so that all products of my site are displayed.
This is my controller file:
class AlleCadeausController extends CI_Controller {
public function index()
{
$this->load->model('allecadeaus_model');
$data['cadeaus'] = $this->allecadeaus_model->get_cadeaus();
$this->load->view('allecadeaus');
}
This is my model file:
<?php
class Allecadeaus_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function get_cadeaus()
{
$query = $this->db->query('SELECT * FROM products');
$result = $query->result_array();
return $result;
}
}
this is my view:
<?php
foreach ($cadeaus as $cadeau)
{
echo $cadeau->product_naam;
}
?>
The error is:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: cadeaus
Filename: views/allecadeaus.php
Line Number: 3
Backtrace:
File: /home/ubuntu/workspace/application/views/allecadeaus.php Line: 3
Function: _error_handler
File:
/home/ubuntu/workspace/application/controllers/AlleCadeausController.php
Line: 11 Function: view
File: /home/ubuntu/workspace/index.php Line: 315 Function:
require_once
My table name is products
You havn't passed the $data variable in your controller. Please follow below code:
$this->load->view('allecadeaus', $data);
So, your controller function should be like below.
public function index()
{
$this->load->model('allecadeaus_model');
$data['cadeaus'] = $this->allecadeaus_model->get_cadeaus();
$this->load->view('allecadeaus', $data);
}
Cheers, you are done.
public function index()
{
$this->load->model('allecadeaus_model');
$data['cadeaus'] = $this->allecadeaus_model->get_cadeaus();
$this->load->view('allecadeaus',$data);
}
try this in you controller
You haven't pass data to the view you just load view.
for passing data to view you have to do
$this->load->view('allecadeaus',$data);
Here are good documentation for passing data from controller to view Contact ME
You should pass the data to the view follow the below code:
public function index()
{
$this->load->model('allecadeaus_model');
$data['cadeaus'] = $this->allecadeaus_model->get_cadeaus();
$this->load->view('allecadeaus, $data');
}
You have to pass $data with the view.
$this->load->view('allecadeaus',$data);
You need pass array of data in second argument.
Please use this :
$this->load->view('allecadeaus',$data);
If you load child view in allecadeaus, then you don't need to pass array of data. You can directly access all variables from array of data.
Hello guys i am trying to connect my database to get some data using codeigniter , there is my code:
i am getting this error:
Fatal error: Class 'Model' not found in /Applications/MAMP/htdocs/ci/application/models/Data_model.php on line 3
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/ci/application/models/Data_model.php:3)
Filename: core/Common.php
Line Number: 569
Backtrace:
A PHP Error was encountered
Severity: Error
Message: Class 'Model' not found
Filename: models/Data_model.php
Line Number: 3
Backtrace:
Models:
Data_model.php:
<?php
class Data_model extends Model {
function getAll(){
$q = $this->db->query("SELECT * from data");
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data [] = $row;
}
return $data;
}
}
}
views:
home.php
<htmL>
<body>
<div> view has been loaded</div>
<!--<p> <?php echo $myValue; ?> </p>
<p> <?php echo $anotherValue; ?> </p> -->
<pre>
<?php foreach ($rows as $r) {
echo '<h1>' . $r->title . '</h1>';
}
?>
</pre>
</body>
</htmL>
controllers:
site.php
<?php
Class Site extends CI_Controller {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function index(){
$this->load->model('data_model');
$data['rows'] = $this->data_model->getAll();
$this->load->view('home', $data);
}
}
Always make sure that you extends your model with CI_Model for it will be recognized .
class Model extends CI_Model {
//you can always put function construct
public function __construct (){
parent::__construct ();
}
}
In your controller :
class Sample extends CI_Controller {
public function __construct () {
parent:: __construct();
//you can load here the model that you will just often so will load it everytime to use it in a function
$this->load->model('nameofModel');
}
}
Remember : The name of the model or controller must be the same with its filename.
Make sure your class extends the base Model class. In CodeIgniter this is "CI_Model"...
class Data_model extends CI_Model {
// Your model class code here...
}
make sure you have already load Model
for that:
1.open application/config/autoload.php
and
2. edit
$autoload['model'] = array(''); with
$autoload['model'] = array('Model');
thats works
Hi guys i have problem using two tables from same DB in one view. When i use only one of the tables it works but when i try to fetch both of them my view does not load. This is my model: schedule_model.php:
<?php
class Schedule_model extends CI_Model {
public function __construct()
{
}
public function get_schedules()
{
$this->db->select('schedule.id, schedule.name');
$this->db->from('Schedule');
$this->db->group_by("schedule.id");
$query = $this->db->get();
return $query->result_array();
}
public function get_subscheds()
{
$this->db->select('subsched.id, subsched.name, subsched.enable, subsched.from, subsched.to, subsched.mode, subsched.fcu1, subsched.fcu2, subsched.fcu3, subsched.pon, subsched.vt, subsched.sr, subsched.cet, subsched.pet, subsched.sab, subsched.ned');
$this->db->from('Subsched');
$this->db->group_by("subsched.id");
$query = $this->db->get();
return $query->result_array();
}
}
?>
This is the controller: schedule.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Schedule extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('schedule_model');
}
public function index()
{
if(!$this->session->userdata('loggedIn')){
redirect("login");
}
$data['schedules'] = $this->schedule_model->get_schedules();
//$data['subscheds'] = $this->schedule_model->get_subscheds();
$this->load->view('schedule', $data); //my view is also named schedule :)
}
}
?>
Notice that i have commented the line that calls the function which gets the elements from the second table. If i uncomment it, my view freezes. Otherwise it works fine with only one table. In my view i loop through elements in foreach loops. What could be the problem? Thanks
Hope this one help you..
$schedules = $this->schedule_model->get_schedules();
$subscheds = $this->schedule_model->get_subscheds();
$data['schedules'] = $schedules;
$data['subscheds'] = $subscheds;
## and then pass to view.
$this->load->view('schedule', $data);
Try this one..I have made some modification..You can access "schedules" as $schedules and "subscheds" as $subscheds in 'schedule' view you have defined.
$data['schedules'] = $this->schedule_model->get_schedules();
$data['subscheds'] = $this->schedule_model->get_subscheds();
This will load both datas from shedules table and subsheds table. and then you can load view with these data by..
$this->load->view('schedule', $data);
Now you can loop through $data by using $data['schedules'] and $data['subscheds'] to use db fields wherever necessary.
I'm using the following from the controller to call a method from the model but receiving and error:
//from the controller:(main.php)
<?php
class Main extends CI_Controller {
public function __construct() {
parent::__construct();
}
function index() {
.....
$this->load->view('view_form');
}//END Fn index()
function get_th() {
//$the=$this->input->post('th', TRUE);
$d['shit']=$this->model_data->tst();
$this->load->view('view_form',$d);
}//END Fn get_th()
}//END Cls Main
?>
//from the model:(model_data.php)
<?php
class Model_data extends CI_Model {
function slider() {
...
}//END Fn slider()
function check_input($data) {
...
}//END Fn check_input()
function tst() {
$tsts= "hellos";
return $this->tsts;
}
}//END Cls model_data
?>
$autoload['model'] = array('model_data');
The error:
Fatal error: Call to undefined method Model_data::tst() in ... application\controllers\main.php...
i think you forgot to load the model in the controller.
$this->load->model('Model_name');
function get_th() {
$this->load->model('model_data');
$d['shit']=$this->model_data->tst();
$this->load->view('view_form',$d);
}
FIXED :/ can t believe I had an additional bracket at the the end of a long file :( wtf
"}"<-- this was the problem.
btw as I said #pramodhkumar use autoload.php.. I had the model autoload so no need for $this->load->...
Can anyone please explain me the object flow in codeigniter MVC ? I can see for example when I put the followong code in controller it works, but i am not being able to figure out which part of this goes in model in vews. I tried several ways but coudn't. When i use the example codes from other it works but myself i am getting confused. Please help
$query = $this->db->query("YOUR QUERY");
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->body;
}
That would translate into something like:
Model:
class SomeModel extends Model {
function SomeModel() {
parent::Model();
}
function get_some_data() {
return $this->db->query('some_query')->result_array();
}
}
Controller:
class SomeController extends Controller {
function SomeController() {
parent::Controller();
}
function index() {
$this->load->model('SomeModel');
$some_data = $this->SomeModel->get_some_data();
$this->load->view('some_view');
}
}
View:
foreach($some_data as $data) {
echo $data->title;
echo $data->name;
echo $data->body;
}
However, for your communication between controller and view I would recommend using a template parser such as Dwoo or Twig (I don't like the one that comes with CI).
On the controller part I was doing:
class SomeController extends Controller {
function SomeController() {
parent::Controller();
}
function index() {
}
function show_data(){
$this->load->model('SomeModel');
$some_data = $this->SomeModel->get_some_data();
$this->load->view('some_view.php');
$this->index()
}
}
is that not the way to do it when we have many function ? When i look at other's code I see something like that or I am wrong ?