How to Codeigniter HMVC Callback function error on live server? - codeigniter

AGood morning all.
I have build a website in codeigniter3 hmvc. all routes and callback function work perfectly on my localhost xampp and after uploading the project on godday share hosting live server I am facing callback funciton problems and below errors show in log file I don't understand why this happen. my user logging function also show this error:-
Unable to access an error message corresponding to your field name Password.(verifylogin)
Error Logs in logs folder of codeigniter
ERROR - 2015-09-16 01:30:08 --> 404 Page Not Found:
ERROR - 2015-09-16 01:30:29 --> 404 Page Not Found: /index
ERROR - 2015-09-16 01:30:30 --> 404 Page Not Found:
ERROR - 2015-09-16 01:31:30 --> Could not find the language line "form_validation_verifylogin"
ERROR - 2015-09-16 01:31:30 --> 404 Page Not Found: /index
These are my application/config/config.php
$config['index_page'] = "index.php?";
$config['uri_protocol'] = "QUERY_STRING";
$config['url_suffix'] = '';
$config['language'] = 'english';
Bellow is my application/config/routes.php setting
$route['Frontend/album_photos/(:num)/(:any)/(:num)'] = 'Frontend/album_photos/index/$1/$2/$3';
$route['Frontend/album_photos/(:num)/(:any)'] = 'Frontend/album_photos/index/$1/$2';
$route['Frontend/news_detail/(:num)/(:any)'] = 'Frontend/news_detail/index/$1/$2';
$route['(:any)/(:any)'] = 'Frontend/index/$1';
$route['(:any)'] = 'Frontend/index/$i';
$route['default_controller'] = 'Frontend/index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['Frontend/download_documents'] = 'Frontend/download_documents/$1';
//these routes works for admin panel
$route['Users/index'] = 'Users/index';
$route['Users/validate_login'] = 'Users/validate_login';
$route['Users/logout'] = 'Users/logout';
$route['Dashboard/index'] ='Dashboard/index';
$route['Aps_pages/manage'] ='Aps_pages/manage';
$route['Aps_pages/create'] ='Aps_pages/create';
$route['Aps_pages/submit'] ='Aps_pages/submit';
$route['Aps_pages/delete'] ='Aps_pages/delete';
$route['Aps_news/manage'] ='Aps_news/manage';
$route['Aps_news/create'] ='Aps_news/create';
$route['Aps_news/submit'] ='Aps_news/submit';
$route['Aps_news/delete'] ='Aps_news/delete';
$route['Aps_events/manage'] ='Aps_events/manage';
$route['Aps_events/create'] ='Aps_events/create';
$route['Aps_events/submit'] ='Aps_events/submit';
$route['Aps_events/delete'] ='Aps_events/delete';
$route['Document_upload/manage'] ='Document_upload/manage';
$route['Document_upload/create'] ='Document_upload/create';
$route['Document_upload/submit'] ='Document_upload/submit';
$route['Document_upload/delete'] ='Document_upload/delete';
$route['Aps_image_slider/manage'] ='Aps_image_slider/manage';
$route['Aps_image_slider/create'] ='Aps_image_slider/create';
$route['Aps_image_slider/submit'] ='Aps_image_slider/submit';
$route['Aps_image_slider/delete'] ='Aps_image_slider/delete';
$route['Aps_testimonials/manage'] ='Aps_testimonials/manage';
$route['Aps_testimonials/create'] ='Aps_testimonials/create';
$route['Aps_testimonials/submit'] ='Aps_testimonials/submit';
$route['Aps_testimonials/delete'] ='Aps_testimonials/delete';
$route['Aps_album_photo/manage'] ='Aps_album_photo/manage';
$route['Aps_album_photo/create'] ='Aps_album_photo/create';
$route['Aps_album_photo/submit'] ='Aps_album_photo/submit';
$route['Aps_album_photo/delete'] ='Aps_album_photo/delete';
$route['Aps_album_photo/search_result'] ='Aps_album_photo/search_result';
$route['Aps_photo_album/manage'] ='Aps_photo_album/manage';
$route['Aps_photo_album/create'] ='Aps_photo_album/create';
$route['Aps_photo_album/submit'] ='Aps_photo_album/submit';
$route['Aps_photo_album/delete'] ='Aps_photo_album/delete';
This is my Home.php Controller in Frontend Modules
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends MX_Controller
{
public $data =array();
function __construct()
{
parent::__construct();
$this->load->model('mdl_home');
$this->load->helper('site_helper');
$this->load->library('pagination');
}
public function index()
{
$slug = (string)$this->uri->segment(1);
//die($slug.'=is a slug');
$this->data['page'] = $this->mdl_home->get_pages(trim($slug));
// Fetch the page data
if(!empty($this->data['page'])){
$method = '_' .trim($this->data['page']->ViewTemplate);
if (method_exists($this, $method)) {
$this->$method();
}
else {
//die('nothing found method not found');
log_message('error', 'Could not load template ' . $method .' in file ' . __FILE__ . ' at line ' . __LINE__);
show_error('Could not load template ' . $method);
}
//some config to view out put
$this->data['view_file'] = $this->data['page']->ViewTemplate;
$this->data['module_name'] ='Frontend';
//these variable used for main page layout meta keywords description title
$this->data['page_title'] = $this->data['page']->Title;
$this->data['keywords'] = $this->data['page']->Keywords;
$this->data['description'] = $this->data['page']->Description;
$this->data['body'] = $this->data['page']->Body;
//load template module for front end
$this->load->module('Template');
$this->template->frontend_layout($this->data);
}else{
show_404();
}
}
//home page method
private function _home_page(){
$this->data['SliderList'] = $this->mdl_home->get_slider_list();
$this->data['NewsList'] = $this->mdl_home->get_news_with_limit('aps_news',12,'','','NewsDate DESC');
$this->data['EventsList'] = $this->mdl_home->get_event_list();
$this->data['TestimonialList'] = $this->mdl_home->get_testimonial_list();
$this->data['Documents'] = $this->mdl_home->get_uploaded_documents();
}
//this method is for static pages
private function _static_page(){
}
//this method load news page
private function _news_page(){
$table = 'aps_news';
$offset = ($this->uri->segment(2) != '' ? $this->uri->segment(2): 0);
$config['total_rows'] = $this->mdl_home->total_rows($table);
$config['per_page']= 20;
$config['uri_segment'] = 2;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = floor($choice);
$config['base_url']= site_url((string)$this->uri->segment(1));
$this->pagination->initialize($config);
$this->data['paginglinks'] = $this->pagination->create_links();
$this->data['NewsList'] = $this->mdl_home->get_news_with_limit($table,$config["per_page"], $offset,'NewsDate DESC');
}
//this method load contact page
private function _contact_page(){
}
//this method load events page
private function _events_page(){
$table = 'aps_events';
$offset = ($this->uri->segment(2) != '' ? $this->uri->segment(2): 0);
$config['total_rows'] = $this->mdl_home->total_rows($table);
$config['per_page']= 20;
$config['uri_segment'] = 2;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = floor($choice);
$config['base_url']= site_url((string)$this->uri->segment(1));
$this->pagination->initialize($config);
$this->data['paginglinks'] = $this->pagination->create_links();
$this->data['EventsList'] = $this->mdl_home->get_with_limit($table,$config["per_page"], $offset,'EventDate DESC');
}
//this method load photo album page
private function _album_page(){
$table = 'aps_photo_album';
$offset = ($this->uri->segment(2) != '' ? $this->uri->segment(2): 0);
$config['total_rows'] = $this->mdl_home->total_rows($table);
$config['per_page']= 12;
$config['uri_segment'] = 2;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = floor($choice);
$config['base_url']= site_url((string)$this->uri->segment(1));
$this->pagination->initialize($config);
$this->data['paginglinks'] = $this->pagination->create_links();
$this->data['AlbumList'] = $this->mdl_home->get_with_limit($table,$config["per_page"], $offset,'AlbumID DESC');
}
public function download_documents($file){
$this->load->helper('download');
$path = './assets/uploaded_documents/'.$file;
force_download($path, NULL);
redirect(base_url());
}
}
This is my Form Validation Library file
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation{
function run($module = '', $group = ''){
(is_object($module)) AND $this->CI = &$module;
return parent::run($group);
}
}
and this is my Users Controller file
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Users extends MX_Controller
{
public function __construct()
{
parent::__construct();
//load financial year
$this->load->model('mdl_users');
}
public function index()
{
if($this->session->userdata('logged_in'))
{
redirect('Dashboard');
}else
{
$this->login();
}
}
//this function used to show login form view in template one column layout view
public function login()
{
$data['view_file'] = "loginform";
$data['module_name'] ='Users';
$data['page_title'] = 'Login Page';
$this->load->module('Template');
$this->template->login_layout($data);
}
//after entering the login values login form submit value handled by this function or method
public function validate_login()
{
$this->form_validation->set_error_delimiters('<div style="color:red;">', '</div>');
$this->form_validation->set_rules('username', 'Username', 'required|max_length[30]|xss_clean');
$this->form_validation->set_rules('password','Password', 'required|callback_verify_login');
if ($this->form_validation->run($this) == FALSE)
{
$this->index();
}
}
//this function for callback validation after submit button press
public function verify_login() {
$username = $this->input->post('username');
$password = $this->input->post('password');
//this method is in model folder/directory which interact with database https://www.youtube.com/watch?v=8fy8E_C5_qQ
$user = $this->mdl_users->check_login_authentication($username, $password);
if(empty($user)){
$this->form_validation->set_message('verify_login', 'Sorry the details you provided have not been found');
return FALSE;
}else{
//set user data in session and redirect to dashboard
$data = array(
'username' =>$user->UserName,
'userid' => $user->UserID,
'logged_in' => TRUE
);
$this->session->set_userdata($data);
redirect('Dashboard');
}
}
//logout function
public function logout()
{
$this->session->sess_destroy();
redirect('Users');
}
}

Related

Page number url pagination CodeIgniter

I use pagination CodeIgniter. I want to remove page number from url or show page number 1,2,3 not 5,10,15. How can I do that please help me I'm beginner at CodeIgniter.
Thanks
Here is my action inside my controller
class Wiki_egypt extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper("url");
$this->load->model('Tours_model');
$this->load->model('Wiki_egypt_model');
$this->load->model('Meta_model');
$this->load->library("pagination");
}
public function Wikipedia_egypt() {
$config = array();
$config["base_url"] = base_url() . "/wikipedia-egypt";
$config["total_rows"] = $this->Wiki_egypt_model->record_count();
$config["per_page"] = 5;
$config["uri_segment"] = 2;
$config['num_links'] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$data["wikis"] = $this->Wiki_egypt_model->
fetch_departments($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$data['meta_tags'] = $this->Meta_model->meta_wikis_eg();
$this->load->view("Header", $data);
$this->load->view("Nav", $data);
$this->load->view("wiki_egypt/wikieg", $data);
$this->load->view("Footer");
}
}
Here is my action inside my Model
public function record_count() {
return $this->db->count_all("wiki_egypt");
}
public function fetch_departments($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->order_by('id', 'DESC')->get("wiki_egypt");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
Here is my action inside my View
<?php echo $links; ?>
<?php
foreach($wikis as $row)
{
?>
<?php
} ?>
First you need another config there:
$config['use_page_numbers'] = TRUE;
Then you need to change the way your page variable works.
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 1;
$page = $config['per_page'] * $page;
So now you're saying that your offset will be equal to the url page times the offset.
All the rest should work just the same.

Get Username Matches ID

I have tried many ways of getting the username in codeigniter models. that matches the row id. But can not seem to get my model to work.
I have looked at user guide many times all ways get errors.
It shows the row id / user id OK when echo it
But can not seem to make a model to be able to match username with row id and then echo it.
Any suggestion on suitable model function.
when I click on my edit button it shows up in url http://localhost/codeigniter/codeigniter-blog/admin/users/edit/1 which works.
Model
// Not return username that matches id.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_user extends CI_Model {
function getUsername() {
$this->db->select('username');
$this->db->where('user_id');
$query = $this->db->get('user');
return $query->row();
}
}
Controller function.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('user');
if ($this->session->userdata('isLogged') == TRUE) {
return true;
} else {
redirect('/');
}
}
public function index() {
$data['title'] = "Users";
$data['base'] = config_item('HTTP_SERVER');
$data['isLogged'] = $this->user->isLogged();
$this->load->model('users/model_user');
$data['text_enabled'] = "Enabled";
$data['text_disabled'] = "Disabled";
$results = $this->model_user->getUsers();
foreach ($results as $result) {
$data['users'][] = array(
'user_id' => $result['user_id'],
'username' => $result['username'],
'edit' => site_url('users/edit/' . $result['user_id'])
);
}
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_list', $data);
}
function edit($user_id = 0, $user_group_id = 0) {
$data['title'] = "Users";
$data['base'] = config_item('HTTP_SERVER');
$data['isLogged'] = $this->user->isLogged();
$this->load->model('users/model_user');
$data['user_id'] = "Current User ID" . " " . $user_id . ":";
$data['user_group_id'] = "Current User Group ID" . " " . $user_group_id . ":";
$data['username'] = "Current User Name:" . " " . $this->model_user->getUsername();
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_form', $data);
}
}
'where' requires a second argument, which you would pass as an argument to your model function. so something like this in the model
class Model_user extends CI_Model {
function getUsername($id) {
$this->db->select('username');
$this->db->where('user_id', $id);
$query = $this->db->get('user');
return $query->row();
}
}
this corresponds to an sql query like
SELECT username FROM user WHERE user_id = ?
so in the controller just pass the user id in the argument to the model function
$data['username'] = "Current User Name:" . " " . $this->model_user->getUsername($user_id);
I have found best way to get my username to match user id
On My Model
function getUsername($user_id) {
if (empty($user_id)) {
return FALSE;
}
$this->db->select('username');
$this->db->where('user_id', $user_id);
$query = $this->db->get('user');
if ($query->num_rows() == 1) {
$result = $query->result_array();
return $result[0]['username'];
} else {
return FALSE;
}
}
on Controller function
$this->model_user->getUsername($user_id);

How to create a codeigniter class to store all emails to a table

How can I create a helper class in codeigniter to store all email which sends and receives in my website. I need to call that class with all email functions
$this->load->library('myclass');
If I call this class then this function should store $to,time, body and subject of the email to a table (table1). How it is possible?
Create a library with the name of "myemail" and place that in application/libraries.
application/libraries/myemail.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CI_Myemail
{
public function __construct()
{
$this->CI =& get_instance();
}
public function saveEmail($to,$body,$subject)
{
$this->CI->load->model("table_model");
$this->table_model->save(array("to"=>$to,"body"=>$body,"subject"=>$subject,"mail_sent_time"=>date("Y-m-d H:i:s")));
}
}
Then you have to create table_model and write function to save the data into data.
In controller, you have to load this library as
$this->load->library('myemail');
In controller, you have to call as
$this->myemail->saveEmail($this->to,$body,$subject);// Here, $this->do is controller variable as you mentioned in comment
You can easily do this by having a helper function to send an email. Where ever you need to send the email call that function. Inside the function save the data to a table before calling the method to send the email. Also you can save the emails without sending with status pending and send it by a cronjob to improve user experience. I am doing same thing in my website.
The helper function as I am using it below. You can tune it for your needs. The data array should have all the details when called from a controller.
function sendEmail($data, $immediate=FALSE) {
$subject = $data['subject'];
$to = $data['to'];
$viewName = $data['template'];
$CI = & get_instance();
$CI->config->load("thephpcode.com");
$from = $CI->config->item('Sender');
$fromName = $CI->config->item('SenderName');
$priority = $CI->config->item('Priority');
if (isset($data['from'])) {
$from = $data['from'];
$fromName = $data['fromName'];
}
if (isset($data['priority']))
$priority = $data['priority'];
$body = $CI->load->view($viewName, $data, TRUE);
if ($from == "") {
log_message('error', 'From value is not set in Email helper for sending email');
return;
}
$bcc = '';
if (isset($data['bcc'])) {
$bcc = $data['bcc'];
}
/*
$replyto ='';
if (isset($data['reply_to'])) {
$replyto = $data['reply_to'];
$replytoname = $data['reply_to_name'];
}
*/
$status = 'Pending';
if ($immediate)
{
$status = 'Sent';
}
$dbdata = array();
$dbdata['from'] = $from;
$dbdata['fromName'] = $fromName;
if (isset($data['reply_to'])) {
$dbdata['replyto'] = $data['reply_to'];
$dbdata['replytoname'] = $data['reply_to_name'];
}
$dbdata['to'] = $to;
$dbdata['subject'] = $subject;
$dbdata['body'] = $body;
$dbdata['bcc'] = $bcc;
$dbdata['status'] = $status;
$dbdata['priority'] = $priority;
$CI->load->model('email_model', 'email_model');
$CI->email_model->insert($dbdata, 'email_queue');
if (!$immediate)
{
return TRUE;
}
//Send the email
$CI->load->library('email');
$CI->email->initialize($CI->config->item('email_config'));
$CI->email->from($from, $fromName);
$CI->email->to($to);
if (isset($data['bcc']))
{
$CI->email->bcc($data['bcc']);
}
if (isset($data['reply_to']))
{
$CI->email->reply_to($data['reply_to'],$data['reply_to_name']);
}
$CI->email->subject($subject);
$CI->email->message($body);
$CI->email->send();
return;
}

Stacked in Pagination in Codeigniter

i'm new with CI and i tried a lot posibilities to make this run, but still it ain't working. Could you please tell me what i'm doing wrong?
So, Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('model_add','',TRUE);
}
public function _remap($a){
if (isset($a) && !empty($a)):
switch ($a) {
case 'index':
$this->index();
break;
default:
$this->one_news($a);
break;
}
endif;
}
public function index()
{
$this->load->library('pagination');
$home=$this->uri->segment(2);
$limit=2;
$offset=0;
$query=$this->model_add->count_news($limit,$offset);
$config['base_url'] = base_url().'/news/';
$config['total_rows'] = $this->db->count_all('tdt_news');
$config['per_page'] = 2;
$config['uri_segment'] = 2;
$this->pagination->initialize($config);
$data = array('query' => $query,'page'=>$home);
$data['news'] = $query;
$this->load->view('main/header');
$this->load->view('main/news', $data);
$this->load->view('main/footer');
}
}
And the Model:
function count_news()
{
$query=$this->db->query("SELECT * FROM `tdt_news` ORDER BY `id` DESC LIMIT $limit, $offset;");
return $query->result();
}
I'll be very thankful for your help, thank you!
You have a series of errors
your count function in your model is missing parameters
you should reformat the SQL string
alternate and standard syntax in your view
renamed model class to empty string
heres a fixed version,
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('model_add');
}
public function _remap($a){
if (isset($a) && !empty($a)) {
switch ($a) {
case 'index':
$this->index();
break;
default:
$this->one_news($a);
break;
}
}
}
public function index()
{
$this->load->library('pagination');
$home = $this->uri->segment(2);
$limit = 2;
$offset = 0;
$query=$this->model_add->count_news($limit,$offset);
$config['base_url'] = base_url().'/news/';
$config['total_rows'] = $this->db->count_all('tdt_news');
$config['per_page'] = 2;
$config['uri_segment'] = 2;
$this->pagination->initialize($config);
$data = array( 'query' => $query,
'page'=>$home,
'news' => $query);
$this->load->view('main/header');
$this->load->view('main/news', $data);
$this->load->view('main/footer');
}
}
Model:
// with default parameters just in case
function count_news($limit = 50, $offset = 0)
{
// and there is a nicer way to write your query
// it works with multiple dbs thanks to active record
$this->db->order_by('id','desc');
$query = $this->db->get('tdt_news', $limit, $offset);
return $query->result();
}

codeigniter pagination error 404

I have Admin controller in codeigniter
class Admin extends CI_Controller {
function __construct() {
parent::__construct();
if (!$this->tank_auth->is_logged_in()) redirect('login');
$this->load->library('pagination');
}
function index() {
$offset = $this->uri->segment(2);
$config['per_page'] = 3;
$data['sitetitle'] = 'Výpis jobů';
$data['listings'] = $this->Jobs_model->get_listings(0,$user_id = FALSE,$config['per_page'],$offset);
$config['uri_segment'] = 2;
$config['base_url'] = base_url().'admin/';
$config['total_rows'] = $this->db->count_all_results('jobs');
$this->pagination->initialize($config);
$this->template->set('title', 'Domovská stránka');
$this->template->load('template', 'site', $data);
}
}
and Jobs_model
function get_listings($category, $user_id = false, $limit = 0, $offset = 0) {
$data = array();
$this->db->order_by('id', 'desc');
$q = $this->db->get('jobs');
if ($category) {
$options = array('category' => $category);
$this->db->order_by('id', 'desc');
$this->db->where('category', $category);
$q = $this->db->get('jobs', $limit, $offset);
}
else {
$query = $this->db->order_by('id', 'desc');
if ($user_id) $query = $query->where('user_id', $user_id);
$q = $query->get('jobs',$limit, $offset);
}
if ($q->num_rows() > 0) {
foreach ($q->result_array() as $row) {
$data[] = $row;
}
}
$q->free_result();
return $data;
}
first page in paginatiom obtain data, but links generating in pagination localhost/sitename/admin/3 produce 404 error.
Where is problem in my script
You need to change:
$config['base_url'] = base_url().'admin/';
To:
$config['base_url'] = base_url().'admin/index/';
If you need the url to be like admin/3, you can use a route or _remap.
Side note: consider using this to get your page number rather than the URI class:
function index($offset = 0) {
// your code
}
It will do the same thing, but it's convenient to use the controller method arguments when possible.
You need to do some change.
$config['base_url']=base_url().'admin/index';
change
$config['uri_segment'] = 3;
check if you are using .htaccess or not. If you aren't, then $config['base_url'] in above should be
$config['base_url']=base_url().'index.php/admin/index';
If you get 404 error on codigniter paggination please check follwing things :
1 ) Check your controller it's index or somthing else if it's index then base_url should be :
base_url().'admin/index/pages' and if it's somethings else it should be :
base_url().'admin/somethingelse/pages'
2 ) Check 'uri_segment' by $this->uri->segment(4) so by this you got the page number then else will do the codiegniter.

Resources