Query Strings with Code Igniter - codeigniter

I'm trying to setup CodeIgniter (v2.1.4) with Query Strings and having problems with passing variables. The pages work when the controller_trigger and function_trigger are passed:
example.org/?c=page&m=index
But when I try to pass a variable:
example.org/?c=page&m=view&id=1
The script throws 'Missing argument' and 'Undefined variable' errors.
In 'application/config/config.php' I've set:
$config['enable_query_strings'] = TRUE;
In 'application/config/routes.php' I have:
$route['default_controller'] = "page";
And my Controller looks like:
<?php
class Page extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('page_model');
}
public function index()
{
$data['title'] = 'Page Title';
$this->load->view('templates/header', $data);
$this->load->view('page/page_index', $data);
$this->load->view('templates/footer');
}
public function view($id)
{
$data['title'] = 'Id Page Title';
$data['page_item'] = $this->page_model->get_page($id);
$this->load->view('templates/header', $data);
$this->load->view('page/page_view', $data);
$this->load->view('templates/footer');
}
}
Does anyone know what I've missed?
Any help would be greatly appreciated

In your config.php change:
$config['allow_get_array'] = TRUE; #example.com?who=me&what=something&where=here

Write it like this
example.org?c=page&m=index
without "/"

Related

Autoloading form_validation library

I am having problem autoloading the form_validation library in Codeigniter. I have a controller Posts with a create function which works great.
public function create(){
$data['title']='New Post';
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
}
Now I want to do form validation . Something like this :
public function create(){
$data['title']='New Post';
$this->form_validation->set_rules('title', 'Title','trim|required|min_length[5]|max_length[128]');
$this->form_validation->set_rules('body', 'Blog','trim|required|min_length[5]');
if($this->form_validation->run==FALSE){
$data['errors']=validation_errors();
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
}else {
$this->load->view('templates/header');
$this->load->view('posts/success', $data);
$this->load->view('templates/footer');
}
}
For now I am not calling any model to store the data, just showing the success message by loading posts/success view . However , even before I code the create function with the validation(that is the above code) , the moment I add form_validation in the autoload.php (even with the first code) like so :
$autoload['libraries'] = array('database','form_validation');
I am getting the below error :
Message: Undefined property: Post_model::$load
Filename: libraries/Form_validation.php
Line Number: 147
Backtrace:
File: C:\xampp\htdocs\ciblog\index.php Line: 292 Function:
require_once
I do not understand the error as I am not even using the Post_model in the method .
My Post_model.php is :
class Post_model extends CI_Controller {
public function get_post($slug=NULL){
if(!$slug) {
$query = $this->db->get('posts');
return $query->result();
} else {
$this->db->where('slug',$slug);
$query = $this->db->get("posts");
return $query->result();
}
}
}
My complete post controller is this :
<?php
class Posts extends CI_Controller {
public function index(){
$data['title']='Latest Posts';
$posts=$this->post_model->get_post();
$data['posts']=$posts;
$this->load->view('templates/header');
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
public function view($slug){
$posts=$this->post_model->get_post($slug);
if(empty($posts)){
show_404();
} else {
$data['posts']=$posts;
$this->load->view('templates/header');
$this->load->view('posts/view',$data);
$this->load->view('/templates/footer');
}
}
public function create(){
$data['title']='New Post';
$this->form_validation->set_rules('title', 'Title','trim|required|min_length[5]|max_length[128]');
$this->form_validation->set_rules('body', 'Blog','trim|required|min_length[5]');
if($this->form_validation->run()==FALSE){
$data['errors']=validation_errors();
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
}else {
$this->load->view('templates/header');
$this->load->view('posts/success', $data);
$this->load->view('templates/footer');
}
}
}
I have autoladed the model which is working .I searched other posts , most were to do with the first letter of the model name being not in caps which is not my case . Could somebody please make me understand whats going wrong ?
You used your model in your controller without loading it, you have to load it first:
$this->load->model('post_model');
Another error here is that model extends CI_Model:
class Post_model extends CI_Model
As the documentation says ref you have to validate like this :
I have seen mistake behind the run() method
Please fix it with :
if ($this->form_validation->run() == FALSE)
Also you can check by loading form_validation library directly by updating your constructor
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
}

Codeigniter model not loading

I'm a newbe in codeigniter.
something is going wrong with I think the model.
this is the controler:
<?php
class Fuel extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('html');
$this->load->library('table');
}
public function image() {
$data['title'] = 'test';
$data['main_content'] = 'imagetest';
$this->load->view("template", $data);
}
public function overview() {
$this->load->model('Get_DB');
$this->Get_DB->overview() ;
$data['title'] = 'overview';
$data['main_content'] = 'overview';
$this->load->view("template", $data);
}
when I load the image function, it works just fine, but the function overview is the problem.
this is my model:
<?php
class Get_DB extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function overzicht() {
$query = $this->db->query("SELECT * FROM invoer "
. "ORDER BY datum DESC");
$gen_query = $this->table->generate($query);
return $gen_query;
}
}
and this is my view:
<?php
echo $gen_query;
and if you want to know: my template is this:
<?php
$this->load->view('templates/header');
$this->load->view($main_content);
$this->load->view('templates/footer');
now when I open my view I get this message:
A PHP Error was encountered
Severity: Notice Message: Undefined variable: gen_query Filename:
views/overzicht.php Line Number: 3
in the model you see that I have made a var $gen_query
so why is that undifined?
regards,
Ralph
Try:
public function overview() {
$this->load->model('Get_DB');
$data = array();
$data['gen_query'] = $this->Get_DB->overzicht() ; #corrected model function and save the result in `gen_query`
$data['title'] = 'overview';
$data['main_content'] = 'overview';
$this->load->view("template", $data);
}
In Controler:
public function overview() {
$this->load->model('Get_DB');
$result = $this->Get_DB->overview() ;
$data['title'] = 'overview';
$data['main_content'] = 'overview';
$data['re'] = $result;
$this->load->view("template", $data);
}
And In view page you can retrive the result
like
foreach($re->result() as $row)
{
//You can get each row data here $row->your_field_names
}

Codeigniter 1.7.2 and PHP 5.3.1, Data not passing to view from controller

I have an app using CI 1.7.2.
In controller i have $data['title'] = 'Sample Title';
In view
<?php echo $title; ?>
Should print it.
But not working.
I have another app with latest CI, in that passing like this works fine.
Anybody know reason for this strange issue?
You should pass the data to view like,
$this->load->view('view_file', $data);
There is another way to pass data to view use the library parser
class home extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url', 'date'));
$this->load->library('form_validation');
$this->load->library('session');
$this->load->library('parser');
}
public function index() {
$this->sssssssss();
}
function sssssssss($data = '') {
$header = $this->parser->parse('interface', array(), TRUE);
$data['interface'] = $header;
//bind all together and parse to template view
$this->parser->parse('template_view', $data);
}
}

Code Igniter - Unable to load requested file after redirect

I am VERY new to codeigniter so please excuse me if this is a n00bish sort of question...
I have a controller called dashboard.php:
class Dashboard extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->load->view('site_header');
$this->load->view('dashboard');
$this->load->view('site_footer');
}
else
{
//If no session, redirect to login page
echo 'hello there';
//redirect('main', 'refresh');
}
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('home', 'refresh');
}
}
?>
This page loads fine when i access it via localhost/sitename/dashboard.
HOWEVER i am having issues trying to redirect to this controller from another controller of mine. The controller that is calling the redirect is verifyLogin.php (in same directory level)
class VerifyLogin extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
function index()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected welcome page
redirect('');
}
else
{
//Go to private area
redirect('dashboard', 'index');
}
}
function check_database($password)
{
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
//query the database
$result = $this->user->login($username, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'id' => $row->ID,
'username' => $row->username,
'stay_logged' => true
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
}
?>
When the redirect() is called i get an error - Unable to load the requested file: dashboard.php
After this error, i can no longer access localhost/sitename/dashboard (i just get that same error).
Some advice would be amazing right now as well as a way of debugging this for future problems.
Cheers!
Use this pattern to redirect
redirect("controllername","function name");
or
redirect(base_url().'index.php/controller/function');
Trying to load a view that doesn't exist. Load dashboard.php to your view folder. I hope it works ;))
By looking at you problem, I think this should help you out use this:
redirect(site_url().'dashboard', 'index');
try this one in verifylogin controller
redirect('Dashboard', 'refresh');
Let me know the result

Variable inside function __construct() in Codeigniter

I am using Tank_auth for user authentication in codeigniter. To check if any user is logged in or not, if he is then to get the username and id I need to use the following code in every functions of my controllers, which is very irritating.
// If any user is logged in get his/her userid or name
if ($this->tank_auth->is_logged_in()) {
$data['user_id'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
}
So , I was wondering if I could make life easier by putting the following code inside the function __construct() like following,
but its not working.
Could you please tell me how to get it work?
Thanks in Advance :)
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->library('tank_auth');
if ($this->tank_auth->is_logged_in()) {
$data['user_id'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
}
}
if you plan to use this array only inside this controller, you can use it like this:
//outside the functions define the $data as controller class property:
private $data = array();
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->library('tank_auth');
if ($this->tank_auth->is_logged_in()) {
$this->data['user_id'] = $this->tank_auth->get_user_id();
$this->data['username'] = $this->tank_auth->get_username();
}
}
//and then use it like
function index(){
$this->load->view("something.php",$this->data);
}

Resources