I a controller class as shown here
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Results extends CI_Controller
{
public function index()
{
$this->load->model('my_model');
}
function validate()
{
//set validation rules here
//validate form input
if ($this->form_validation->run() == FALSE) {
// fails
$this->load->view('get_results');
}
}
function ranking1(){
$data['returned_records']= $this->my_model- >get_ranking();
$html=$this->load->view('view_ranking', $data, true);
$pdfFilePath = "ranking.pdf";
$this->load->library('m_pdf');
//generate the PDF from the given html
$this->m_pdf->pdf->WriteHTML($html);
//download it.
$this->m_pdf->pdf->Output($pdfFilePath, "D");
}
function top5(){
$data['returned_records']= $this->school_model->get_top5();
$html=$this->load->view('view_top5', $data, true);
//this the the PDF filename that user will get to download
$pdfFilePath = "top5.pdf";
//load mPDF library
$this->load->library('m_pdf');
//generate the PDF from the given html
$this->m_pdf->pdf->WriteHTML($html);
//download it.
$this->m_pdf->pdf->Output($pdfFilePath, "D");
}
}
I want to call these functions from my view like this:
Top 5<br>
ranking<br>
when I call the functions in the controller within the index() method they work fine but calling them in an a tag like shown above does work.
I solved it.It was a stupid mistake. I needed to call my_model from the controller's constructor to make the functions access.
Related
we have two controllers one is members.php which is registering the user another one is reserv.php for reserving a ticket for busses.
I want to load second controller after completion of first controller class.
members.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Members1 extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('member_model1');
}
public function index() {
$table = $this->member_model1->insert_members();
$data['members'] = $table;
//$this->load->view('header1', $data);
$this->load->view('reservation_view',$data);
//$this->load->view('members');
//$this->load->view('footer');
}
}
reserv.php
<?php
class reserve extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('member_model1');
}
function index() {
$table = $this->member_model1->get_members();
$data['members'] = $table;
//$this->load->view('header1', $data);
$this->load->view('reservation_view',$data);
//$this->load->view('reservation_view');
// $this->load->helper(array('form'));
//$this->load->view('reservation_view');
}
}
?>
To call a controller from another controller, you can use the function redirect() :
// In Members1 controller
redirect('/reserve/index');
Note that you won't be able to send data along with the redirect, such as when you load a view. To send data, you must use $this->session->set_flashdata(); because the redirects seem to use header() (I advise you to have a loot at this post).
// In Members1 controller
$this->session->set_flashdata('key', 'value');
redirect('/reserve/index');
By the way, when I look at your two classes, I wonder why you don't simply create a unique Ticket class with two methods like registerUser() and bookTicket() (these names are pure fancy, be free to use yours).
I am using HMVC with CodeIgniter.
I have this in my testmodule controller:
public function index()
{
$this->view_data['main_content'] = 'frontpage';
$this->load->view('template', $this->view_data);
}
And this in my view template.php of that controller that is loaded by this controller:
<?php
$this->load->view('includes/header');
$this->load->view($main_content);
$this->load->view('includes/footer');
?>
but, when I var_dump($main_content) in the view and die() it shows null instead of frontpage
How, come? I don't get it at all.
If you want to use $this->view_data you have to declare $view_data as a property first (at the top of your controller):
class TestModule extends CI_Controller
{
public $view_data = array();
public function index()
{
// Now you can use $this->view_data in this function:
$this->view_data['main_content'] = 'frontpage';
$this->load->view('template', $this->view_data);
}
}
I've got a problem when assigning data to an object variable. In all my time, i haven't seen such weird behaviour.
I have a class:
class Main extends CI_Controller {
public $data = array();
public function __construct(){
parent::__construct();
/**
* Load Models
*/
$this->load->model('rss');
$this->load->model('model_product');
/**
* Default loaded Data
*/
$this->data['newsfeed'] = $this->rss->load($this->config->item('newsfeed'));
}
}
You see that i want to add the newsfeed to the $this->data object. It will held in a key named newsfeed. But now, i want this newsfeed on different pages. It is useless and inefficient to load this on every page, so i extended my main controller.
class Shop extends Main {
public function __construct(){
parent::__construct();
}
public function index()
{
$this->data['content'] = 'shop/default';
$this->load->view('template/index', $this->data);
}
public function product($id){
$this->data['product'] = $this->model_product->getProducts(null, $id);
if($this->data['product']->categoryid != $this->config->item('gamecategory')){
redirect('');
}
var_dump($this->data['product']);
$this->data['content'] = 'shop/product';
$this->load->view('template/index', $this->data);
}
}
The expected behaviour would be that $this->data would hold an array of objects (in each key ) but when i var_dump in the shop controller it gives me the entire $data object as return data, instead of the provided key. This totally blows my mind and i cant seem to understand this weird behaviour.
Expected var_dump ($this->data['product']):
Array(x){
key: value,
key: value,
.....
}
actual result:
Object{
newsfeed:
- data
product:
- data
....
....
}
Model rss:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Rss extends CI_Model{
public function __construct(){
parent::__construct();
}
public function load($item){
return ($xml = simplexml_load_string(file_get_contents($item))) ? $xml : false;
}
}
I have the below code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('users/user_model');
$this->load->library('form_validation');
}
function _load_login_page()
{
$this->load->model('sysconfig/sysconfig_model');
$a = $this->sysconfig_model->get_sysconfig();
$row = $a[0];
$this->lang->load('klas',$row->sysconfig_language);
$data['sys_name'] = $row->sysconfig_system_name;
$data['template_name'] = $row->systemplate_name;
$data['css_script'] = base_url().'assets/'.$row->systemplate_name;
if($row->sysconfig_maintenance === 'Y')
{
$this->load->view('sysconfig/maintenance',$data);
}
else {
$this->load->view('login',$data);
}
}
function index()
{
$this->form_validation->set_rules('username', 'Username', 'trim|required|max_length[12]|xss_clean|callback_check_auth');
$this->form_validation->set_rules('password','Password','trim|required');
if($this->form_validation->run($this) == FALSE)
{
$this->_load_login_page();
} else {
redirect('welcome','refresh');
}
}
function check_auth()
{
if($this->user_model->authentication())
{
return TRUE;
}
$this->form_validation->set_message('check_auth',$this->lang->line('invalid_username'));
return FALSE;
}
?>
user_model.php
<?php
class User_Model extends CI_Model
{
function authentication()
{
$this->db->where('useracc_id', $this->input->post('username'));
$this->db->where('useracc_password', md5($this->input->post('password')));
$q = $this->db->get('base_useracc');
if($q->num_rows() == 1)
{
$session_data = array('isUserLogged'=>TRUE);
$this->session->set_userdata($session_data);
return TRUE;
}
}
?>
From here we can see if the user didn't fill the username and password fields, it will show the error and everything works as expected. The problem is, if the user provides an invalid username or password, the error message won't show.
for the information, I already put $lang['invalid_username'] = 'Invalid username or password'; on the language file.
I am doing this using the HMVC technique. Please help me.
You don't seem to be passing any data to your callback function. Callback functions expect the value of the input field to be passed as a parameter. I don't think you can make this work as a form validation callback be because the authentication method presumably needs to know both the password and the username. At present you're not passing any data into your check_auth method or indeed onwards to your user_model->authentication() method. That is why form_validation is ignoring it.
Rather than calling check_auth as a callback, why not run the form_validation first (this is really what it's for - checking the data is correct and sanitised) and then pass the values from your form to your check_auth function as part of an if statement. You will not be able to use the form_validation set_message method to display errors but I think this is a cleaner approach.
To summarise, use the form_validation to check the data you are receiving is ok and display relevant messages if it is not. Authenticating a user based on that information is a separate procedure that I don't think belongs with validation. Do you see the difference?
from the HMVC wiki page :
When using form validation with MX you will need to extend the CI_Form_validation class as shown below, before assigning the current controller as the $CI variable to the form_validation library. This will allow your callback methods to function properly. (This has been discussed on the CI forums also). ie:
<?php
/** application/libraries/MY_Form_validation **/
class MY_Form_validation extends CI_Form_validation
{
public $CI;
}
<?php
class Xyz extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->form_validation->CI =& $this;
}
}
i need to create an url like this: www.example.com/index.php/scheda/name-surname-id.html
so i create the Scheda controller, this is the code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Scheda extends CI_Controller{
function __construct(){
parent::__construct();
}
function index(){
$name_scheda = $this->uri->segment(2);
//i need only the id for the search into db
$id = substr($name_scheda, strripos($name_scheda,'-')+1, strlen($name_scheda));
echo "name:".$id;
}
}
but when i write the url in the address bar i get an 404 error...can someone help me to understand why?
Your url:
www.example.com/index.php/scheda/name-surname-id.html
Should be:
www.example.com/index.php/scheda/index/name-surname-id.html
index() is the default method, but the index segment can only be missing from the URL if there are no arguments, otherwise Codeigniter will think you are trying to call the method name-surname-id.html().
You can use routes.php or _remap() to clean up the URL and remove the index segment.
// routes.php
$routes['scheda/(:any)'] = 'scheda/index/$1';
OR:
class Scheda extends CI_Controller{
function _remap($method, $args) {
$name_scheda = $method;
$id = substr($name_scheda, strripos($name_scheda,'-')+1, strlen($name_scheda));
echo "name:".$id;
}
}