HOw do I pass $variable from comments() to someFunction()?
class Blog extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
public function comments()
{
$variable = "Hello";
}
public function someFunction()
{
echo $variable;
}
}
** EDIT ** Feel free to point out any other mistakes if you wish
class Home extends CI_Controller {
private $idArray;
function __construct()
{
parent::__construct();
$this->load->model('home_model');
$this->load->library('tank_auth');
$this->load->library('form_validation');
}
public function index() {
$home_data['initial_two'] = $this->home_model->get_two_brands();
$home_data['user_id'] = $this->tank_auth->get_user_id();
$home_data['username'] = $this->tank_auth->get_username();
$this->load->view('home_view', $home_data);
}
public function get_two() {
$get_results = $this->home_model->get_two_brands();
if($get_results != false){
$html = '';
foreach($get_results as $result){
$html .= '<li>'.$result->brand.'</li>';
}
list($result1, $result2) = $get_results;
$idOne = $result1->id;
$idTwo = $result2->id;
$this->idArray = array($result1->id, $result2->id);
//var_dump($this->idArray);
$result = array('status' => 'ok', 'content' => $html);
header('Content-type: application/json');
echo json_encode($result);
exit();
}
}//public function get_two() {
function user_pick() {
$this->form_validation->set_rules('pick', 'Pick', 'required|trim|integer|xss_clean');
$this->form_validation->set_rules('notPick', 'Not Pick', 'required|trim|integer|xss_clean');
//$arr = $this->idArray;
var_dump($this->idArray); // This is NULL
$pick = $_POST['pick'];
$notPick = $_POST['notPick'];
$user_id = $this->tank_auth->get_user_id();
if ($this->form_validation->run() == FALSE)
{
$result = array('status' => 'no', 'content' => "No good!");
header('Content-type: application/json');
echo json_encode($result);
exit();
}else{//if ($this->form_validation->run() == FALSE || $do_input == NULL)
$upload = $this->home_model->user_pick($user_id, $pick, $notPick);
$result = array('status' => 'ok', 'content' => "Thank you!");
header('Content-type: application/json');
echo json_encode($result);
exit();
}//if ($this->form_validation->run() == FALSE || $do_input == NULL)
}
}//class Home extends CI_Controller { closing bracket
/* End of file home.php */
/* Location: ./application/controllers/home.php */
class Blog extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
public function comments()
{
$_SESSION['variable'] = Array('k1'=>'v1','k2'=>'v2') ;
// Store the variable in session so it can be called
// in another page or ajax call
}
public function display()
{
echo $_SESSION['variable'] ;
}
}
// my index.php file
var $blog = new Blog() ;
$blog->comments() ;
//another ajax_called.php file, call in ajax on in another browser tab
var $blog = new Blog() ;
$blog->display() ;
Related
I have a function index() in my Admin.php controller and I'm trying to execute the 'if' statement but it is executing the 'else' statement. The admin user and password that I am logging in is correct. There's something wrong in my code and could anyone here please help me. Thanks in advance. :)
Here's my code:
//Admin.php controller
<?php
class Admin extends CI_Controller
{
public function __construct()
{
parent::__construct();
if ($this->session->userdata('logged_in') !== TRUE) {
redirect('Login');
}
}
function index()
{
if ($this->session->userdata('level') === '1') {
$this->load->view('admin_view');
} else {
echo "Access Denied";
}
}
}
//Login.php controller
<?php
class Login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Login_model');
}
public function index()
{
$this->load->view('login_view');
}
public function auth()
{
$username = $this->input->post('user_name', TRUE);
$password = $this->input->post('user_pass', TRUE);
$result = $this->Login_model->check_user($username, $password);
if ($result->num_rows() > 0) {
$data = $result->row_array();
$name = $data['user_name'];
$level = $data['user_lvl'];
$sesdata = array(
'user_name' => $username,
'user_lvl' => $level,
'logged_in' => TRUE
);
$this->session->set_userdata($sesdata);
if ($level === '1') {
redirect('Admin');
} elseif ($level === '2') {
redirect('User');
}
} else {
echo "<script>alert('Access Denied');history.go(-1);</script>";
}
$this->load->view('login_view');
}
}
I Suggest use double equals instead triple equals
if ($this->session->userdata('level') == '1') {
$this->load->view('admin_view');
} else {
echo "Access Denied";
}
And also check if level is string or int..
Maybe you guys can help me I am trying to do this:
public function index()
{
$r = array();
//some code
echo json_encode($this->utf8ize($r));
}
public function utf8ize($d) {
//some code
return $d;
}
But I get the "Call to undefined function utf8ize()" error
Why?
Edit 1: The complete code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Solicitud extends MX_Controller {
public function __construct()
{
/*
parent::__construct();
if(!$this->input->is_ajax_request())
{
show_404();
exit();
}
else
{
*/
$this->load->model('Solicitud_model', 'Model');
//}
}
public function index()
{
$bandera = $this->input->post('bandera');
$r = array();
if ($bandera == 1){
$result = $this->Model->getConsulta($this->session->session_facultad_apps);
$r = array("data" => $result,
"success" => true,
"bandera" => $bandera);
}else if($bandera == 2)
{
$result = $this->Model->get($this->session->session_facultad_apps);
$r = array("data" => $result,
"success" => true,
"bandera" => $bandera);
}else if ($bandera == 3){
$result = $this->Model->getAsigna($this->session->session_facultad_apps);
$r = array("data" => $result,
"success" => true,
"bandera" => $bandera);
}
echo json_encode(utf8ize($r));
}
public function utf8ize($d) {
if (is_array($d)) {
foreach ($d as $k => $v) {
$d[$k] = utf8ize($v);
}
} else if (is_string ($d)) {
$d = iconv('UTF-8', 'ISO-8859-1', $d);
return utf8_encode($d);
}
return $d;
}
this is used to reference the current instance of an object. In your case, you missed the reference to this for the recursive call.
Simple solution - also add $this-> to the inner utf8ize call
echo json_encode($this->utf8ize($r));
...
public function utf8ize($d) {
if (is_array($d)) {
foreach ($d as $k => $v) {
$d[$k] = $this->utf8ize($v);
}
} else if (is_string ($d)) {
$d = iconv('UTF-8', 'ISO-8859-1', $d);
return utf8_encode($d);
}
return $d;
}
It looks as if your code is correct.
However I do not see the class defenition nor can I see you using the class.
I created a class on my local machine that works, when I call the method of the test class the function returns an empty array.
<?php
class Test
{
public function index()
{
$r = array();
echo json_encode($this->utf8ize($r));
}
public function utf8ize($d)
{
//some code
return $d;
}
}
$test = new Test();
echo $test->index();
I hope this helps, if not feel free to reach out :)
Mmm not the best answer but this code works
public function index()
{
$r = array();
//some code
}
function utf8ize($d) {
//some code
}
echo json_encode(utf8ize($r));
}
I had to put the function inside the original function
Edit: The error was from the recursive call; not the first one.
Thanks to all!
How do I retrieve (and process) the params I have being passed in from this...
Modules::load('MembersList', $this->input->get(NULL, TRUE);
... into the module its being passed into?
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MemberList extends MX_Controller {
function __construct()
{
parent::__construct();
$this->load->model('memberslist_model');
}
public function index()
{
// $params = how_do_i_retrieve it?
if ( ! isset($list) || $list == 'individuals')
{
$data['module'] = 'Individuals';
$data['results'] = $this->memberslist_model->list_individuals();
$data['count'] = count($data['results']);
$data['view'] = $this->load->view('list_individuals', $data, TRUE);
}
if ( $list == 'families')
{
$data['module'] = 'Families';
}
return $this->load->view('memberslist', $data, TRUE);
}
public function settings()
{
$data['settings'] = $this->memberslist_model->settings();
return $this->load->view('settings', $data, TRUE);
}
}
I understand that it is being passed as URI segments like codeigniter but I've tried everything and I can't get it to work.
I'm not sure which version you use, but you can try this:
$whatEver = Modules::run('MembersList/index', $this->input->get(NULL, TRUE));
class MemberList extends MX_Controller {
function __construct()
{
parent::__construct();
$this->load->model('memberslist_model');
}
public function index($params)
{
print_r($params);
// $params = how_do_i_retrieve it?
if ( ! isset($list) || $list == 'individuals')
{
$data['module'] = 'Individuals';
$data['results'] = $this->memberslist_model->list_individuals();
$data['count'] = count($data['results']);
$data['view'] = $this->load->view('list_individuals', $data, TRUE);
}
if ( $list == 'families')
{
$data['module'] = 'Families';
}
return $this->load->view('memberslist', $data, TRUE);
}
public function settings()
{
$data['settings'] = $this->memberslist_model->settings();
return $this->load->view('settings', $data, TRUE);
}
}
Right now $renderData['username'] won't pass through to the view.
class HomeController extends MY_Controller {
public function index($renderData=""){
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$renderData['username'] = $session_data['username'];
//$this->load->view('pages/home_view', $renderData);
$this->_render('pages/home',$renderData);
}
else
{
//If no session, redirect to login page
redirect('LoginController', 'refresh');
}
}
}
The error I get is...
Which alludes to this code...
<h1>Home</h1>
<h2>Welcome <?php echo $username; ?>!</h2>
Logout
Here is my My_Controller where the _render function is located...
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller{
//Page info
protected $data = Array();
protected $pageName = FALSE;
protected $template = "main";
protected $hasNav = TRUE;
//Page contents
protected $javascript = array();
protected $css = array();
protected $fonts = array();
//Page Meta
protected $title = FALSE;
protected $description = FALSE;
protected $keywords = FALSE;
protected $author = FALSE;
function __construct()
{
parent::__construct();
$this->data["uri_segment_1"] = $this->uri->segment(1);
$this->data["uri_segment_2"] = $this->uri->segment(2);
$this->title = $this->config->item('site_title');
$this->description = $this->config->item('site_description');
$this->keywords = $this->config->item('site_keywords');
$this->author = $this->config->item('site_author');
$this->pageName = strToLower(get_class($this));
}
protected function _render($view,$renderData="FULLPAGE") {
switch ($renderData) {
case "AJAX" :
$this->load->view($view,$this->data);
break;
case "JSON" :
echo json_encode($this->data);
break;
case "FULLPAGE" :
default :
//static
$toTpl["javascript"] = $this->javascript;
$toTpl["css"] = $this->css;
$toTpl["fonts"] = $this->fonts;
//meta
$toTpl["title"] = $this->title;
$toTpl["description"] = $this->description;
$toTpl["keywords"] = $this->keywords;
$toTpl["author"] = $this->author;
//data
$toBody["content_body"] = $this->load->view($view,array_merge($this->data,$toTpl),true);
//nav menu
if($this->hasNav){
$this->load->helper("nav");
$toMenu["pageName"] = $this->pageName;
$toHeader["nav"] = $this->load->view("template/nav",$toMenu,true);
}
$toHeader["basejs"] = $this->load->view("template/basejs",$this->data,true);
$toBody["header"] = $this->load->view("template/header",$toHeader,true);
$toBody["footer"] = $this->load->view("template/footer",'',true);
$toTpl["body"] = $this->load->view("template/".$this->template,$toBody,true);
//render view
$this->load->view("template/skeleton",$toTpl);
break;
}
}
}
Here is an additional function that may be helpful...
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 to login page
$this->load->view('pages/login_view');
}
else
{
//Go to private area
// redirect('home', 'refresh');
redirect('HomeController', 'refresh');
}
}
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
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
}
?>
What am I doing wrong that is preventing the username to be passed to the view?
PS - let me know any additional documentation I can provide.
something like this
protected function _render($view,data, $renderData="FULLPAGE") {
$this->data = $data;
switch ($renderData) {
case "AJAX" :
$this->load->view($view,$this->data);
break;
case "JSON"
I am working with Codeigniter 2.1.4 and also new to this framework. When ever i am trying to load the model on the controller class, error id 500 being thrown by the browser. Here is the code of the model class file name:
merchant_login.php
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class Merchant_model extends MY_Model {
public $tbl_user = 'users';
function __construct(){
parent::__construct();
$this->load->database('merchant', TRUE, TRUE);
}
function user_login($data)
{
$this->db->from($this->tbl_user);
$this->db->where($data);
$query = $this->db->get();
if($query->num_rows()==1)
{
$this->db->select("CURRENT_LOGIN");
$this->db->from($this->tbl_user);
$this->db->where("USER_ID", $data["USER_ID"]);
$q = $this->db->get();
$res = $q->result();
$current_time = $res[0]->CURRENT_LOGIN;
$last_time = $current_time;
$current_time = date("Y-m-j H:i:s");
$arr = array(
"CURRENT_LOGIN" => $current_time,
"LAST_LOGIN" => $last_time,
"LAST_IP" => $this->input->ip_address()
);
$this->db->where("USER_ID", $data["USER_ID"]);
$this->db->update($this->tbl_user, $arr);
$this->db->from($this->tbl_user);
$this->db->where($data);
$query = $this->db->get();
return $query->row_array();
}
return false;
}
} ?>
Here is the code for controller and the file name is:
"login.php"
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Login extends MY_Controller {
function __construct() {
parent::__construct();
$this->load->library(array("form_validation", "session"));
}
//merchant section start
public function merchant_login() {
$this->session->keep_flashdata("return_url");
$this->form_validation->set_rules('loginName', 'Login', 'trim|valid_email|required');
$this->form_validation->set_rules('loginPassword', 'Password', 'trim|required');
if ($this->session->userdata("merchantLogged") != 1) {
if ($this->form_validation->run() == FALSE) {
$data["error"] = validation_errors();
header('Content-type: application/json');
echo json_encode($data);
} else {
$d['USER_ID'] = $this->input->post("loginName");
$d['USER_PASSWD'] = sha1($this->input->post("loginPassword"));
$this->load->model("merchant_model", "model", TRUE);
$user = $this->model->user_login($d);
if ($user) {
if ($this->input->post("security") == 'not') {
//Cal to a member function of the model class
if ($this->model->user_ip_check($d) == false) {
$this->session->set_userdata('u_id', $this->input->post("loginName"));
$this->session->set_userdata('u_pd', $this->input->post("loginPassword"));
$data['redirect'] = site_url("merchants/ip_security/");
} else {
$usr['type'] = "MR";
unset($user['USER_PASSWD']);
// unset($user['ID']);
unset($user['MM_NAME']);
//$this->load->database('default', TRUE, TRUE);
$this->session->set_userdata($usr);
$this->session->set_userdata($user);
$this->set_merchantLogged(1);
// if($this->session->flashdata("return_url")){
// $data['redirect'] = site_url("user/" . $this->session->flashdata("return_url"));
// }
// else{
$data['redirect'] = site_url("merchant/account");
// }
}
} else {
$d['que'] = $this->input->post("sec_q");
$d['ans'] = $this->input->post("sec_q_answer");
if ($this->model->check_que($d) == true) {
$this->session->unset_userdata('u_id');
$this->session->unset_userdata('u_pd');
$usr['type'] = "MR";
unset($user['USER_PASSWD']);
unset($user['MM_NAME']);
$this->session->set_userdata($usr);
$this->session->set_userdata($user);
$this->set_merchantLogged(1);
$data['redirect'] = site_url("merchant/account");
} else {
$data['redirect'] = site_url("merchants/ip_security");
}
}
} else {
$data["error"] = "Username or password are wrong";
$data["islogin"] = true;
}
header('Content-type: application/json');
echo json_encode($data);
}
} else {
if ($this->session->flashdata("return_url")) {
$data['redirect'] = site_url("merchant/" . $this->session->flashdata("return_url"));
} else {
$data['redirect'] = site_url("home");
}
header('Content-type: application/json');
echo json_encode($data);
}
}
}
?>
class Merchant_model extends MY_Model
replace
class Merchant_model extends CI_Model
class Login extends MY_Controller
replace
class Login extends CI_Controller
syntax error:$this->load->database('merchant', TRUE, TRUE); correct this one
My Suggestion please go through on the codeigniter document it can helpfull you
http://ellislab.com/codeigniter/user-guide/general/controllers.html
change your model file name from merchant_login.php to merchant_model.php
and change in login.php
$this->load->model("merchant_model", "model", TRUE);
to
$this->load->model("merchant_model", "", TRUE);