I'm trying to create a new site and it includes database, as it said every time you want to access your database you need to do it in your Model but I don't know why I am having a problem with my Model. Please the codes below.
Controller:main
class Main extends CI_Controller {
public function index()
{
$this->home();
}
public function home(){
$this->announce($data);
}
public function announce(){
$this->load->library('table');
$this->load->model('home_model');
//echo "test";
}
Model:home_model.php
class Home_model extends CI_Model{
echo '';
}
after running the program. localhost/ci_gcc/main/announce or localhost/ci_gcc/main I got this error.
Parse error: parse error, expecting `T_FUNCTION' in C:\wamp\www\ci_gcc\application\models\home_model.php on line 4
but if I didn't type anything inside the model class there is no error.
Thus anyone have a solution for this?
You are doing a bad attempt. Don't echo like this in model class.
$this->load->model('home_model');
This will load your model class. Inorder to do any operations inside the model class, you have to write functions inside it. And you can call that function from your controller like,.
$this->home_model->functionname();
Related
I have two controllers. Let's say A and B.
For clarity's sake I want to run some functions in B controller but I have trouble getting back to A.
Of course I could just run
$this->load->view('pages/examplepage');
But this example page has to get data from database and of course it gives me an error that it doesn't have the necessary data.
Now I could just run
$data['exampledata'] = $this->example_model->get_data();
$this->load->view('pages/examplepage', $data);
Now for couple of rows it's okay but isn't there a better way? What am I missing? Can't I just run the A controller's function from B controller and let the already existing function do the job?
Hope this will help you :
user codeigniter url helper redirect()
Example :
Controller A
class Abc extends CI_Controller {
public function first()
{
redirect('xyz/some_method');
}
}
Controller B
class Xyz extends CI_Controller {
public function some_method()
{
redirect('abc/first');
}
}
for More : https://www.codeigniter.com/user_guide/helpers/url_helper.html
I'm trying to use traits in CodeIgniter. I put the Trait in the libraries directory and then loaded the library in the controller. Then I used the trait in the model, but it didn't work and I received the following error
Trait file (in library):
trait DataTablesTrait{
public function query(){
$this->db->select('*');
$this->db->limit(10, 1);
return $this->db->get('my_table');
}
}
?>
Controller:
class myController extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->library('DataTablesTrait');
$this->load->model('ABC_Model');
}
}
Model:
class ABC_Model extends CI_Model {
use DataTablesTrait;
function callQuery(){
$this->query();
}
}
I got this error:
Non-existent class: DataTablesTrait
Please advise
CodeIgniter (CI) isn't trait or namespace friendly but they can be used. It requires working around CI a bit though.
The main problem, the CI thing you have to work around, is the call
$this->load->library('DataTablesTrait');
CI will find this file but load->library will try to instantiate the trait which fails because it is not possible to instantiate a Trait on its own.
Replace the line above with
include APPPATH.'/libraries/DataTablesTrait.php';
You should be free of the error with that. But you're not going to get results because callQuery() does not return anything. To round out the test have callQuery() return the CI_DB_result object the Trait should produce
public function callQuery()
{
return $this->query();
}
Add an index function to the controller so we can dump the output
public function index()
{
$DB_result = $this->ABC_Model->callQuery();
var_dump($DB_result->result());
}
This should produce the expected output - assuming that 'my_table' has data :)
Worth saying that traits are now fully supported in Codeigniter 4 and can be implemented as you would expect using namespaces etc.
// TRAIT
namespace App\Controllers\traits;
trait YourTraitName {
}
// CONTROLLER
use App\Controllers\traits\YourTraitName;
class Admin extends BaseController {
use YourTraitName;
I am learning codeigniter 3
In my config.php:
$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/ci3x/admin/';
In my routes.php:
$route['customer'] = 'customer/index';
In controllers/customer.php:
class Customer extends MY_Controller{
function index(){
// some code here
}
}
When I type: http://localhost/ci3x/admin/customer on brower, it back error 404.
I have no clue to fix, please help me to solve it.
Many thanks
As you are extending a Class and that class does have a constructor, your class needs a constructor that also calls the constructor of the extended class.
Else things won't get up and running correctly.
class Customer extends MY_Controller{
public function __construct() {
parent::__construct(); // Call the MY_Controller constructor
}
function index(){
// some code here
}
}
The same goes for your MY_Controller constructor as it will need to call CI_Controller constructor... It ripples down and everything gets initialised correctly (in simplistic terms) .
Note:
Be careful with your controller and method files names. If you read the user guide it will say that the file names for controllers and methods should start with a capital letter.
So your controllers/customer.php should be controllers/Customer.php. If you are running on Windows it won't care. If you are running on Linux it will definitely matter.
I want to call A (Codeigniter) login Class and it functions from B Codeigniter. Anyone can explain and share idea how can do this. Thanks
This question is similar to:
How to load a controller from another controller in codeigniter?
or Codeigniter call function within same class
Try this, you will get solution.
<?php
class MY_Controller extends CI_Controller {
public function is_logged()
{}
}
class Home_Controller extends MY_Controller {
public function show_home()
{
if (!$this->is_logged()) {
return false;
}
}
}
?>
I'm having a litle problems with my concepts of OOP. I'll try to explain the best I can.
I have this class
class Application_controller extends CI_Controller{
public function addItem(){
"some code to add the item to the database (working)";
}
}
And I have another class, both controllers:
require_once 'application_controller.php';
class Contact extends Application_controller{
public function __construct(){
parent::__construct("variables needed");
}
}
And in the View add of the contact I added the following action contact/addItem.
Ok, now here's what I know about OOP in general.
Isn't the method addItem supposed to be part of the Contact class because its extends Application_controller?
I'm asking because when I submit the form I get no action, and when I add the method addItem in the class Contact overriding the parent one it works.
The reason you get no action is that codeigniter doesn't find a method addItem in your Contact class (update: this is probably due to the way CodeIgniter routing works). The solution would be to make addItem a generic method in a Model that stores data in a table, move it to a Model, and load the model in your controller.
Create application/models/writeModel.php
class writeModel extends CI_Model{
function addItem(){
// code here
}
}
In your controller:
class Contact extends Controller{
function __controller(){
parent::Controller();
$this->load->model('writeModel');
}
function somefunction(){
$this->writeModel->addItem(); // call the method here
}
}
Reference: CodeIgniter Models
The problem here (other then the several syntax errors in the OP) is likely to be that "Contact" can not extend "Application_controller" because it does not know it exists. If we setup a test like this:
/controllers/Test.php
class Test extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
echo 'test';
}
}
/controllers/TestTwo.php
require_once("Test.php");
class TestTwo extends Test
{
function __construct()
{
parent::__construct();
}
function index()
{
parent::index();
echo ' and test two';
}
}
We will get the desired output of "test and test two" by navigating to appurl/TestTwo/. This is because TestTwo knows of Test. Removing the require(); line from TestTwo.php will break the relation.
Removing the index() function from TestTwo will then result in only "test" being output by navigating to appurl/TestTwo/.
I found an answer to some similar question on the Codeigniter forums. It says this
your ShopDownloads will inherit (methods,properties etc etc) from the Shop controller. and as said in the video tutorial, u must inherit your class from the controller class so that it can inherit all the properties and methods codeigniter provides for u.
Sohaib,
The link for the post is http://codeigniter.com/forums/viewthread/102718/#518120
I don't know how but this is working today. It was probably the server. Just needed a restart.
Its Solved, just by start the server today and start developing LOL. Thanks for youre time guys.
Regards,
Elkas