I now trying CodeIgniter for the first time. I created a new route rule in the routes.php
$route['ajax/rendeles/(:any)']='ajax/rendeles/$1';
$route['ajax']="ajax";
And a controller (ajax.php):
<?php
class ajax extends CI_Controller{
public function rendeles($id){
$this->load->view("ajax/rendeles/".$id, $);
echo $id;
}
}
?>
A view (rendeles_view.php):
<?php
$this->load->model("ajax/rendeles_model");
$result=$this->rendeles_model->rendelesReszletei($id);
foreach ($result as $row){
echo $row->product."<br>";
}?>
And a model (rendeles_model.php):
<?php
class rendeles_model extends CI_Model{
public function rendelesReszletei($id){
$query=$this->db->get_where("_order_items", array("id_order"=>$id));
return $query->result();
}
}?>
But when I enter into the browser the domainname/ajax/rendeles/4072 (<-this is an id of an order), but It don't show anything.
Somebody can help me?
Thanks in advance, kukko.
<?php
class ajax extends CI_Controller{
public function rendeles($id){
$data['id'] = $id;
$this->load->view("ajax/rendeles", $data);
echo $id;
}
}
?>
Your class names must start with an uppercase letter, like this:
class Ajax extends CI_Controller{
// ...
class Rendeles_model extends CI_Model{
// ...
Related
Hello I'm currently facing a problem of adding another controller and the problem is that
I have 2 controllers
class 1st_Controller extends CI_Controller {
}
and
class 2nd_Controller extends CI_Controller{
my 2 models are working perfectly fine and the only problem is that I need to call 1 model per each controller
for example
1st controller is for 1st model and 2nd controller is for 2nd model.
Now what I've tried so far is
class 2nd_Controller extends 1st_Controller {
public function __construct()
{
header("Access-Control-Allow-Origin: *");
parent::__construct();
$this->load->model('2nd_model','2ndmodel');
$this->load->helper('url');
$this->load->library("pagination");
$this->load->library("session");
}
public function index()
{
$data['title'] = 'System Login';
$get_all_inv = $this->2ndmodel->get_all();
$data["tryvariable"] = $get_all_inv;
$this->template->load('default_layout','contents','myview2nd', $data);
}
}
I tried echo in my view like this
<?php echo $tryvariable; ?>
but no luck because the error says that it is an undefined variable .
Your second controller can't be define cause it's not define as subclass_prefix in your codeigniter application.
class 2nd_Controller extends 1st_Controller { //codeigniter don't recognize this.
}
The simpliest way to solve your problem is.. the fact that you can call multiple models in 1 Controller.
so you can have:
class 1st_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('1st_model','1stmodel');
$this->load->model('2nd_model','2ndmodel');
}
}
or call just once.
class 2nd_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('2nd_model','2ndmodel');
}
}
Hope that helps.
So what I did was write like this on my route to have my 2nd_Controller working
$route['default_controller'] = '1st_Controller';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['2nd_Controller'] = '2nd_Controller';
Now everything is working perfectly. Thanks for all the help
I have this simple PHP code where I have created a new model with a celebrity name, a controller that accesses the celebrity name from the model, and a view to display the name. For some reason using the path below I can't display the name accessing the controller. I can't find out what I am missing or doing wrong.
Thanks for the help!
http://localhost/ci/Starcontroller/guess
Controller->
<?php
class Starcontroller extends CI_Controller{
function __construct() {parent::__construct();}
function guess()
{
$this->load->model('starmodel');
$newstarr = $this->starmodel->lookup();
$this->load->view('starview',$newstarr);
}
}
?>
Model->
<?php
class starmodel extends CI_Model{
function __construct()
{
parent::__construct();
}
public function lookup(){
$celebrity = "Anna";
return $celebrity;
}
}
?>
View ->
<html>
<head></head>
<body>
<?php
echo $newstarr;
?>
</body>
</html>
First initial alphabet of Class name and File name of controller and model are in capital letter's check your model class name
Then in controller change to this
<?php
class Starcontroller extends CI_Controller {
function __construct() {
parent::__construct();
// Would load model in here and other helpers and libraries.
// So can access through all functions on controller.
// $this->load->model('starmodel');
}
function guess() {
$this->load->model('starmodel');
$newstarr['data'] = $this->starmodel->lookup();
$this->load->view('starview',$newstarr);
}
}
In view
<?php echo $data ?>
From controller, you would like to pass an associative array of data to the view.
So change this part:
$newstarr = $this->starmodel->lookup();
$this->load->view('starview',$newstarr);
To this:
$data['newstarr'] = $this->starmodel->lookup();
$this->load->view('starview',$data);
Rest of your code may stay the same.
I am not sure on how to do this with the model, view, and controller. I currently have it in the view only. I know this is bad practice that is why I am trying to see if I can get help. I have google how to create count but I cannot find any tutorials on this. Any help would be appreciated also this code is working.
view page
?php $this->db->where('status','Happy');
$this->db->from('data');
$count = $this->db->count_all_results();?>
<a href='http://google.com'><?php echo 'Happy =' . $count;
?></a><br>
Model
<!-- model.php -->
<?php
class Model extends CI_Model{
public fucntion __construct(){
$this->load->database();
}
public function count(){
$this->db->select(*);
$this->db->from('data');
$this->db->where('status','Happy');
$count = $this->db->count_all_results();
return $count;
}
}
?>
Controller
<!-- controller.php -->
<?php
class Controller extends CI_controller{
public function __construct(){
parent::__construct();
}
public function index(){
$this->load->model('model');
$data['result'] = $this->Model->count();
$this->load->view('printresult',$data);
}
}
?>
View
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
This is my AdminBase.php
<?php
class AdminBase extends Controller{
public function __construct(){
parent::Controller();
$admin = $this->session->userdata('username');
if(!isset ($admin)){
redirect('/Site/Home');
}
}
}
And this is my Admin Controller :-
<?php
class Admin extends AdminBase{
public function index(){
echo "You are in Admin panel!!";
}
}
When i browse to Admin controller, i get this error :-
Fatal error: Class 'AdminBase' not found in C:\Program Files\wamp\www\College\application\controllers\Admin.php on line 3
You need to put this line on top of Admin.php
<?php
// Include Base Controller
include ('AdminBase.php');
class Admin extends AdminBase{
public function index(){
echo "You are in Admin panel!!";
}
}
Also your base admin class should be like
<?php
class AdminBase extends Controller{
public function __construct(){
parent::Controller();
$admin = $this->session->userdata('username');
if(empty($admin)){
redirect('/Site/Home');
}
}
}
Thanks
There is another way to do that just created filename MY_Controller.php at application/libralies
and then create like this
<?php
class AdminBase extends Controller {
public function __construct(){
parent::Controller();
$admin = $this->session->userdata('username');
if(!isset ($admin)){
redirect('/Site/Home');
}
}
}