I am seeing some odd behavior in my basic PHP codeigniter app.
My output looks like this:
number of rows:
Warning notice
undefined variable: rows
number of rows: 10
What is most peculiar is that it looks like it is trying to double execute my PHP code, but I cannot figure out why. Any insight is appreciated:
Code below:
model:
class foo extends CI_Controller
{
public function index()
{
$this->go();
}
public function go()
{
$this->load->model('model');
$data = array('rows'=> $this->model->count());
$this->load->view('view',$data);
}
}
model:
class model extends CI_Model
{
public function count()
{
$query = "Select count(1) as count from table";
$result = $this->db->query($query);
return $result->result_array();
}
}
view:
<html>
<body>
Number of rows: <?php print_r($rows[0]['COUNT']); ?>
</body>
</html>
it looks like your index function is firing and calling go() before go is called.
I would guess your htaccess rules are written incorrectly.
try changing your index function to this
public function index()
{
redirect('index.php/foo/go');
}
Related
I'm a newbie in CodeIgniter. I want to use the following function and needs to be run in every page. I'm calling the function in the top header of the view. Even though different subviews are loaded in the content below, the navigation bar should load this function in order to count notifications and messages on every run.
function count_records_in_table($table, $where)
{
$this->db->select('count(id) as rows');
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();
foreach($query->result() as $r)
{
return $r->rows;
}
}
first create the common model
then put this function on it and load this model in
$autoload['model'] = array('mdl_common');
autoload file in config
finally you can use your function by calling model common like this
$this->mdl_common->count_records_in_table("table_name","where");
you can use in every page you want try it it will helps .
Here is suitable solution
First create one file say common.php in application/libraries/common.php
Now write below code in common.php file
class Common extends CI_Controller
{
public function customFailMessage($msg)
{
$xml = new SimpleXMLElement('<Response/>');
$xml->addChild('status','success');
$xml->addChild('msg',"my message");
print ($xml->asXML());
die;
}
}
Now call you function from any of your controller like this
class Ws_plan extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('common');
}
public function test()
{
$this->common->customFailMessage();
}
}
Hope you got idea how to use common function in all controller
This is the code for my controller:
class products extends CI_Controller {
public function index() {
$this->load->model('getproducts');
$this->load->view('productlist');
}
}
?>
When I load this, it returns a blank page, however when I comment out the line that loads the model, it returns the view as expected (pointing to an error in the model). Here is the code for the model:
<?php
class getproducts extends Model {
function listProducts() {
$query = $this->db->get('ProjectProducts');
if ($query->num_rows() > 0) {
foreach ($query->result() as $row {
$productdata[] = $row;
}
return $productdata;
}
}
}
?>
Am I missing something obvious here? I'm not even using any of the data from the model yet.
What version you use of codeigniter
please note that you should extends CI_Model not model
class getproducts extends CI_Model {
Please make sure your controller start letter be capital ie. upper
case and extend it as CI_Model
you are missing
function __construct()
{
parent::__construct();
}
and this code before your function listProducts and after class starting hopefully this will resolve your issue.
and also use same function in your controller as well
I am having problems with my code. When I try to debug my web application I get the following error message....Call to a member function find() on a non-object.....here is my code
class TeamsController extends AppController {
var $name = 'Teams';
function index() {
$this->set('teams', $this->team->find('all'));
}
function Welcome() {
}
}
I am trying to display records from my MySQL database. Now with that said, I did this tutorial and I followed the instructions down to the tee.....but somehow my code has bugs. The only difference between my code and the code of the tutorial I did is the variable names...and the controller names....and the I dont have the hello world function... Here is a sample of the code from the tutorial I did....
class PostsController extends AppController {
var $name = 'Posts';
function index() {
$this->set('posts', $this->Post->find('all'));
}
function hello_world() {
}
}
With that said, am I suppose to declare an instance of an object to get this to work?
It's likely a case sensitivity issue:
function index() {
$this->set('teams', $this->Team->find('all'));
}
If not, ensure your controller has access to the Teams model (e.g. $uses).
I am still new at PHP and the MVC concept. I have been trying to duplicate the CI News Tutorial(http://codeigniter.com/user_guide/tutorial/news_section.html), while using my own database and rewriting the code.
I have been unsuccessful.
Here is my controller:
class Main extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->library('tank_auth');
$this->load->model('structures_model');
}
public function structures()
{
$this->load->model('structures_model');
if (!$this->tank_auth->is_logged_in()) {
redirect('/auth/login/');
} else {
$data['structures_all'] = $this->structures_model->get_structures();
$this->load->view('templates/header', $data);
$this->load->view('templates/navmain', $data);
$this->load->view('structures', $data);
$this->load->view('templates/footer', $data);
}
}
Here is my model (structures_model)
<?php
class Structures_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_structures()
{
$query = $this->db->get('structures');
return $query->result_array();
}
}
And my view:
<?php foreach ($structures_all as $structures_info): ?>
<h2>Structures</h2>
<div id="main">
<?php echo $structures_info['str_name'] ?>
</div>
<?php endforeach ?>
The error im getting is the common :
A PHP Error was encountered<
Severity: Notice
Message: Undefined variable: structures_all
Filename: main/structures.php
Line Number: 2
I am at a loss. I have looked at all the similar errors people have gotten, but can't figure out why exactly the structure_all array is not being defined. Shouldn't it get created in the controller function where I set :
$data['structures_all'] = $this->structures_model->get_structures();
What am I missing?
The best way to debug this would be to assign $data['structures_all'] a definite array value, say: $data['structures_all'] = array('foo' => 'bar');
Is the $structures_all variable available in the view now? If it is available, you know that $this->structures_model->get_structures(); is returning null.
Do you have a table in your database called structures?
Are you sure your database connection details are filled out in config/database.php?
Do you have php error reporting set to all? There might be hidden messages... call error_reporting(E_ALL); in the constructor of your controller.
Also try echoing: $this->db->last_query(); to verify your query is being constructed the same way you tried in phpmyadmin...
Hopefully this puts you on the right track.
I'm trying to get familiar with CI and I have run into a problem while trying to implement my Model. I get the following error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Home::$OrderModel
Filename: controllers/home.php
Line Number: 12
My assumption was that I was breaking some convention with the naming of the Model. If
I change the line in question and call the model using all lower case:
$data['query'] = $this->ordermodel->get_all_workorder_names();
Nothing is returned to the view.. blank page; no source, no error.
Here is my model:
<?php
class OrderModel extends Model{
function OrderModel()
{
parent::Model();
$db = $this->load->database();
}
function get_all_workorder_names()
{
$this->db->select('name');
$query = $this->db->get('WorkOrder');
return $query->result();
}
}
?>
This is the calling controller:
<?php
class Home extends Controller{
function Home()
{
parent::Controller();
$this->load->model('ordermodel');
$this->load->helper('url');
}
function index()
{
$data['query'] = $this->OrderModel->get_all_workorder_names();
$this->load->view('Header');
//$this->load->view('Home',$data);
$this->load->view('Footer');
}
}
?>
What am I doing wrong? A side question: Is there a debugger available for PHP in Eclipse?
Use $this->ordermodel. The variable name will match whatever you use with load->model()
For further reading, you can check out CodeIgniter's User Guide entry on loading models here.