codeigniter error loop with page redirect - php

Hello I have created an update query using a concept that if the id is valid then it should update the data otherwise it will not do so but when any of the conditions meet it redirects and says page is redirecting to the loop why is this happening so ?
Controller
public function confirm() {
if($this->session->userdata('is_logged_in') == true) {
$invc = "#".$this->input->get('id');
if($invc == true) {
$this->data_update->update_trans($invc);
} else {
redirect('home/confirm');
}
} else {
redirect('user/login');
}
}
Here is my model file
public function update_trans($invc_id) {
$get_trans = $this->db->get_where('transaction', array("invc_id" => $invc_id));
$user_id = $get_trans->row()->userid;
$amount = $get_trans->row()->ammount;
if(!empty($user_id)) {
$check = $this->db->get_where('account', array("user_id" => $user_id));
if($check->row()->balance == true) {
$new_amount = ($amount + $check->row()->balance);
$dataupdate = array(
'user_id' => $user_id,
'balance' => $new_amount
);
$this->db->where('id', $user_id);
$this->db->update('account', $dataupdate);
$this->db->where('invc_id', $invc_id);
$this->db->update('transaction', array('status' => 'Confirmed'));
redirect('home/confirm?id='.$invc_id);
} else {
$data1 = array(
'user_id' => $user_id,
'balance' => $amount
);
$this->db->insert('account', $data1);
$this->db->where('invc_id', $invc_id);
$this->db->update('transaction', array('status' => 'Confirmed'));
redirect('home/confirm?id='.$invc_id);
}
} else {
redirect('home/confirm');
}
}
And this is my view file
<?php
require('sidebar.php');
?>
<!-- Body Content -->
<div id="body">
<?php
if($this->input->get('id') == true) {
?>
<div class="msg">
<h4>Your Payment Has been confirmed successfully</h4>
</div>
<?php } else { ?>
<div class="msg">
<h4>Payment Not confirmed</h4>
</div>
<?php } ?>
</div>
<div id="break"> </div>
</div>
I dont know why but it is giving me the same error This webpage has a redirect loop can anyone help me out with this please

In your function its going every time in else part (redirect('home/confirm');) so its showing error. Your function should be like this..
public function confirm() {
if($this->session->userdata('is_logged_in') == true) {
$invc = "#".$this->input->get('id');
if($this->input->get('id')) {
$this->data_update->update_trans($invc);
} else {
redirect('home/confirm');
}
} else {
redirect('user/login');
}
}
Hope this will solve your issue.

Try your confirm() function as like that:
public function confirm()
{
if($this->session->userdata('is_logged_in') == true) {
$id = intval($this->input->get('id'));
$invc = "#".$id;
if($id > 0) {
$this->data_update->update_trans($invc);
} else {
redirect('home/confirm');
}
} else {
redirect('user/login');
}
}
You are expecting this "#".$this->input->get('id') will return you true or false.
And more if still facing same issue, than check what are you getting in $_GET like:
if($this->session->userdata('is_logged_in') == true) {
print_r($_GET); // will return you an array
}

Related

like clause in codeigniter

I want to include like clause in the case of last_name.This is my controller code used for search box.I want to compare form value last_name with db value family_name.
public function filtered_volunteer_details()
{
$user_data = $this->session->userdata("logged_user");
$tab_result = array();
if (is_array($user_data) && $user_data["user_role"] == "staff")
{
if ($this->input->method() == "post")
{
$condition = array();
$data = $this->input->post();
if(isset($data["membership_number"]) && $data["membership_number"]!= "")
{
$condition["membership_number"]=trim($data["membership_number"]);
}
if(isset($data["last_name"]) && $data["last_name"]!= "")
{
$condition["family_name "]=trim($data["last_name"]);
}
$this->load->model(array(
"Users"));
$result = $this->Users->get_volunteers("*",$condition);
$volunteer_tbl_list=array();
foreach($result as $volunteer_results)
{
$volunteer_tbl_list[] = $this->load->view("staff/manage_volunteer/volunteer_list_tbl_row", $volunteer_results, TRUE);
}
echo json_encode(array(
"status" => "success",
"volunteer_tbl_list" => $volunteer_tbl_list));
return;
}
}
echo json_encode(array(
"error" => "true",
"msg" => "Invalid Access."));
return;
}
This is my model code
public function get_volunteers($fields = "*",$condition=array())
{
$condition["staff"]="N";
$this->db->select($fields);
$query = $this->db->get_where(self::$tbl_name, $condition);
//var_dump($this->db->last_query());
return( $query->result() );
}
Try adding a $this->db->like() to the query in get_volunteers(). documentation
public function get_volunteers($fields = "*",$condition=array())
{
$condition["staff"]="N";
$this->db->select($fields);
$this->db->like($condition);
$this->db->from(self::$tbl_name);
return( $query->result() );
}

read more function with codeigniter

i am trying to create a readmore function in codeigniter where the readmore link will be linked to a controller which would show all the data about that particular id....i am kind of confused on how to go about it... i tried...
<?php
$research_detail_url = site_url()."/research/research_details";
//echo json_encode($research)
if($research)
{
foreach ($research as $_research) {
$author = $_research->author;
$content = $_research->content;
$dsubmitted = $_research->dsubmitted;
echo "<div class='menu-collapse'> <h5>$author</h5>";
echo "<p>";
echo "<span class='support_text'>$content <span><br />";
echo "<span class='support_text'>$dsubmitted <span><br />";
echo "<a href='$research_detail_url' target='_blank' style='text-decoration:underline; color:#0088cc;'>
read more ยป </a>";
echo "</p> </div>";
}
}
?>
but i seem not be getting any results...i need help...
and this is my controller function.....
public function research_details($id='')
{
if(!$id)
{
echo "Project Id required";
return;
}
$_result = $this->projects_model->get_project($id);
if($_result)
{// success in fetching data hurray
$result['projects'] = $_result;
$users_ids = $this->users_model->get_user_ids(); //return available user id's
$groups_ids = $this->groups_model->get_group_ids(); //return available group id's
//echo json_encode($users_ids);
//echo json_encode($groups_ids);
$group_record = $this->map_names_to_ids($users_ids , $groups_ids );
$result['group_record'] = $group_record;
//load the view
$this->load->view('__includes__/header');
$this->load->view('__includes__/boostrap_responsive');
$this->load->view('projects/project_panel', $result);
$this->load->view('__includes__/footer_scripts');
$this->load->view('__includes__/wijmo_file_jquery');
$this->load->view('__includes__/footer');
}
else
{
exit("An Error occured in fetching the requested project");
}
}
and this is my model.....
<?php
class research_model extends CI_Model {
function add()
{
$this->db->insert('research',$_POST);
if($this->db->_error_number())
{
return $this->db->_error_number();
}
}
function update($article_id, $data_fields = NULL){
if($data_fields == NULL)
{
$this->db->where("article_id =".$article_id);
$this->db->update('research',$_POST);
}
else
{
$this->db->where("article_id =".$article_id);
$this->db->update('research',$data_fields);
}
$is_error = $this->db->_error_number();
if($is_error){
echo $is_error;
}
return TRUE;
}
function delete($id){
$this->db->where("article_id =".$id);
return $this->db->delete('research');
}
//return the research with this id
function get_research($id){
$this->db->where("article_id =".$id);
$query = $this->db->get('research');
if ($query->num_rows() > 0){
return $query->row_array();
}
else
echo $this->db->_error_message();
return FALSE;
}
//return the available research in the table
function get_research_all(){
$query = $this->db->get('research');
if ($query->num_rows() > 0)
{
foreach($query->result() as $row)
{
$result[] = $row;
}
return $result;
}
}
}
and my entire controller.....
<?php
class Research extends Public_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('research_model');
}
function index()
{
if($this->ion_auth->is_admin())
{
$result = $this->research_model->get_research_all();
$data = array(
'main_content' => 'research/index',
'research' => $result
);
$this->load->view("loader", $data);
}
else
{
redirect('home');
}
}//END INDEX
// public view
function current()
{
$result = $this->research_model->get_research_all();
$data = array('research' => $result);
$this->load->view('__includes__/header');
$this->load->view('__includes__/navbar');
$this->load->view('research/current', $data);
$this->load->view('__includes__/footer');
}
function add()
{
if($this->ion_auth->is_admin())
{
$this->load->view("loader",array('main_content'=>"research/add_research"));
}
}//END ADD
function edit($id='')
{
if(! $id)
{
echo "research Id required";
return;
}
$result = $this->research_model->get_research($id);
if( ! $result)
{
echo "Nothing to edit";
return;
}
$result['main_content'] = "research/add_research";
$this->load->view("loader",$result);
}//END EDIT
function delete($id='')
{
if(! $id)
{
echo "Id required";
return;
}
$this->research_model->delete($id);
$this->get_research();
}//END DELETE
function submit($id='')
{
//validate form [perform validation server-side to make sure of fields]
$this->load->library('form_validation');
$this->form_validation->set_rules('author', 'Author', 'trim|required|min_length[4]');
if ($this->form_validation->run() == FALSE){
//ajax data array
$data = array(
'server_validation' => validation_errors()
);
echo str_replace('\\/', '/', json_encode($data));
}
else{
if($id){
$result = $this->research_model->update($id);
$content = "article has been UPDATED successfully";
//$retArr["content"] = $content;
//echo json_encode($retArr);
}
else{
$result = $this->research_model->add();
$content = "article has been CREATED successfully";
//$retArr["content"] = $content;
//echo json_encode($retArr);
}
//if duplicate key
if($result == 1062){
//ajax data array
$data = array();
$data['is_valid'] = 0;
echo json_encode($data);
}else{
//ajax data array
$data = array(
'is_valid' => 1,
'content' => $content
);
echo json_encode($data);
}
}//end ELSE form valid
}//END SUBMIT
public function research_details($id='')
{
if(!$id)
{
echo "Project Id required";
return;
}
$_result = $this->research_model->get_research($id);
if($_result)
{// success in fetching data hurray
$result['article'] = $_result;
//load the view
$this->load->view('__includes__/header');
$this->load->view('__includes__/boostrap_responsive');
$this->load->view('research/research_details', $Aresult);
$this->load->view('__includes__/footer_scripts');
$this->load->view('__includes__/wijmo_file_jquery');
$this->load->view('__includes__/footer');
}
else
{
exit("An Error occured in fetching the requested project");
}
}//END EDIT
}
?>
my public controller
<?php
abstract class Public_Controller extends CI_Controller
{
public $about_data;
function __construct()
{
parent::__construct();
//Making This variable availale for the whole site
$this->load->model('about_model');
$this->load->model('captcha_model');
//get your data
$this->about_data = $this->about_model->get_abouts();
}
}
?>
I see a couple of problems:
In your view, you are not closing the span tags. You need
</span>
In your view, you are creating the link, but you are not
including an ID, which you need in your controller. What I mean by this is the following:
First,you create this $research_detail_url = site_url()."/research/research_details";.
Then echo "<a href='$research_detail_url' target='_blank' style=''>read more</a>";
As you can see, your URL does not have an ID when created. What you should do is something like this: $research_detail_url = site_url()."/research/research_details/31"; where 31 is a random ID. You need to come up with the right ID needed in order to pass it to your controller. Your controller is currently assigning a blank ID since none is being passed which is why you end up with a result of "Project Id required"

To store session in database

In the below codeigniter code I have placed the controller and model. My aim is to store the session college name in the db. I tried but it is not entering the session college name into db.college_name is in blank.
Controller
function create() {
$j=1;
$createcustomer = $this->input->post('createcustomer');
if( $this->input->post('createcustomer') != false ) {
foreach($createcustomer as $row_id) {
//this is validation command to update on the screen
$this->form_validation->set_rules("exam_name_" . $row_id, "'Exam name'","required");
$this->form_validation->set_rules("month_". $row_id,"`Month`","required");
$this->form_validation->set_rules("year_". $row_id,"`Year`","required","required|");
}
}
if ($this->form_validation->run() == FALSE) {
$data["message"]="";
$this->load->view("exam_view",$data);
} else {
while($j<=$this->uri->segment(3)) {
$data = array(
'exam_name' => $this->input->post('exam_name_'.$j),
'month' => $this->input->post('month_'.$j),
'year' => $this->input->post('year_'.$j)
);
$exam_name=$this->input->post('exam_name_'.$j);
$data1 = $this->session->userdata("college_name");
if ($exam_name != "") {
$this->exam_model->add_record($data,$data1);
// $this->load->view("success_msg",$data);
}
$j++;
} //end of while condition
} //end of if condition
redirect('exam_site', 'refresh');
//$this->index();
}
Model:
function add_record($data,$data1) {
$this->db->insert('exam_table', $data,$data1);
//$this->db->insert('exam_table', $data1);
if ($this->db->_error_number() == 1062) {
$this->session->set_flashdata('duplicate', 'duplicate');
}
if ($this->db->_error_number() == "") {
$this->session->set_flashdata('create', 'create');
}
return;
}
change this:
$this->db->insert('exam_table', $data,$data1);
to this:
$this->db->insert('exam_table', $data);
$this->db->insert('exam_table', $data1);

Check if admin has logged in

If there's someone logged in, this code checks the sessions and I can successfully view the page, but now I'd like to check if the user is admin. I tried checking in the model by Below is what I have tried and its not working.
method that checks session and if its an admin
public function index()
{
$this->load->library('authlib');
$loggedin = $this->authlib->is_loggedin();
///$admin = $this->auth->admin();
if ($loggedin === false) {
$this->load->helper('url');
redirect('/auth/login');
}
if ($this->auth->admin() === false) {
$message ['msg'] = "You are not an admin!";
$this->load->view('homeview', $message);
}
else
{
$this->load->view('add_view');
}
}
Auth Controller
public function authenticate()
{
$username = $this->input->post('uname');
$password = $this->input->post('pword');
$user = $this->authlib->login($username,$password);
**>> $this->admin($username,$password); << passes the posted in values**
if ($user !== false) {
$this->load->view('homeview',array('name' => $user['name']));
}
else {
$data['errmsg'] = 'Unable to login - please try again';
$this->load->view('login_view',$data);
}
}
public function admin($username,$password){
//$this->load-model('usermodel');
$admin = $this->authlib->adminlib($username,$password);
if ($admin == false){
return false;
//if ($res->num_rows() != 1){
//return false;
}
}
library authlib
public function adminlib($user,$pwd)
{
return $this->ci->usermodel->chkadmn($user,$pwd);
}
the model
function chkadmn($username,$password)
{
$this->db-where(array('username' => $username,'password' => sha1($password)));
$res = $this->db->get('users',array('type'));
if ($res->num_rows() != 1) {
return false;
}
}
Made some changes and now I get "Call to undefined function where() in C:\xampp\htdocs\ecwm604\application\models\usermodel.php on line 54"
personally i will replace all your if statments and i would test it as shown:
public function index()
{
$this->load->library('authlib');
$loggedin = $this->authlib->is_loggedin();
///$admin = $this->auth->admin();
if (!$loggedin) {
$this->load->helper('url');
redirect('/auth/login');
}
if (!$this->auth->admin()) {
$message ['msg'] = "You are not an admin!";
$this->load->view('homeview', $message);
}
else
{
$this->load->view('add_view');
}
}
public function admin(){
$this->load-model('usermodel');
$admin = $this->usermodel->chkadmn($username,$password);
if (!$admin){
return false;
//if ($res->num_rows() !== 1){
//return false;
}
}
function chkadmn($username,$password)
{
$this->db-where(array('username' => $username,'password' => sha1($password)));
$res = $this->db->get('users',array('type'));
if ($res->num_rows() !== 1) {
return false;
}
}

Message Library , My_Controller and __remap issue

I am creating an application and handling common things in MY_Controller. I am using Message library to display common messages.
Here is MY_Controller.php:
<?php
class MY_Controller extends CI_Controller {
public $data = array();
public $view = TRUE;
public $theme = FALSE;
public $layout = 'default';
protected $redirect;
protected $models = array();
protected $controller_model;
protected $controller_class;
protected $controller_library;
protected $controller_name;
protected $partials = array(
'meta' => 'partials/meta',
'header' => 'partials/header',
'navigation' => 'partials/navigation',
'content' => 'partials/content',
'footer' => 'partials/footer'
);
public function __construct()
{
parent::__construct();
$this->output->enable_profiler(true);
$this->load->helper('inflector');
$this->load->helper('url');
$this->controller_class = $this->router->class;
if(count($this->models)>0)
{
foreach ($this->models as $model)
{
if (file_exists(APPPATH . 'models/' . $model . '.php'))
{
$this->controller_model = $model;
$this->load->model($model);
}
}
}else{
if (file_exists(APPPATH . 'models/' . $this->controller_model . '.php'))
{
$this->load->model($this->controller_model);
}
}
$this->controller_name = $this->router->fetch_class();
$this->action_name = $this->router->fetch_method();
}
public function _remap($method, $parameters)
{
if (method_exists($this, $method))
{
$this->run_filter('before', $parameters);
$return = call_user_func_array(array($this, $method),$parameters);
$this->run_filter('after', $parameters);
}else{
show_404();
}
if($this->theme === TRUE OR $this->theme === '')
{
$this->theme = 'default';
$this->template->set_theme($this->theme);
}else if(strlen($this->theme) > 0){
$this->template->set_theme($this->theme);
}else{
}
if($this->layout === TRUE OR $this->layout === '')
{
$this->layout = 'default';
$this->template->set_layout($this->layout);
}else if(strlen($this->layout) > 0){
$this->template->set_layout($this->layout);
}else{
}
if(isset($this->partials))
{
foreach($this->partials as $key => $value)
{
$this->template->set_partial($key,$value);
}
}
if(isset($this->data) AND count($this->data)>0)
{
foreach($this->data as $key => $value)
{
if(!is_object($value))
{
$this->template->set($key,$value);
}
}
}
if($this->view === TRUE OR $this->view === '')
{
if($this->parse == TRUE)
{
$parse_string = $this->template->build($this->router->method ,'' ,$this->parse);
echo $this->parse($parse_string);
}else{
$this->_call_content($this->router->method);
$this->template->build($this->router->method,array());
}
}else if(strlen($this->view) > 0){
if($this->parse == TRUE){
$parse_string = $this->template->build($this->router->method ,'' ,$this->parse);
echo $this->parse($parse_string);
}else{
$view = $this->view;
$this->_call_content($view);
$this->template->build($view,array());
}
}else{
$checkpoint = $this->session->flashdata('exit');
if($checkpoint){
exit();
}else{
$this->session->set_flashdata('exit',TRUE);
}
$this->redirect();
}
}
public function _call_content($view)
{
$value = $this->load->view($view,$this->data,TRUE);
$this->template->set('content',$value);
}
/* Common Controller Functions */
public function index()
{
$data[$this->controller_model] = $this->{$this->controller_model}->get_all();
$this->data = $data;
$this->view = TRUE;
if($this->input->is_ajax_request() || $this->session->flashdata('ajax')){
$this->layout = FALSE;
}else{
$this->layout = TRUE;
}
}
public function form()
{
if($this->input->is_ajax_request() OR !$this->input->is_ajax_request())
{
$this->load->helper('inflector');
$id = $this->uri->segment(4,0);
if($data = $this->input->post()){
$result = $this->{$this->controller_model}->validate($data);
if($result){
if($id > 0){
}else{
$this->{$this->controller_model}->insert($data);
}
$this->message->set('message','The page has been added successfully.');
$this->view = FALSE;
$this->layout = FALSE;
$this->redirect = "index";
}else{
$this->message->set('message','The Red fields are required');
}
}
$row = $this->{$this->controller_model}->where($id)->get();
$this->data[$module_name]= $row;
}
}
public function delete()
{
$id = $this->uri->segment(3,0);
if($id != 0){
$this->{$this->controller_model}->delete($id);
}
$this->view = FALSE;
$this->layout = FALSE;
}
public function redirect()
{
redirect($this->redirect);
}
public function call_post($data)
{
foreach($data as $key => $row){
$_POST[$key] = $row;
}
}
public function query()
{
echo $this->db->last_query();
}
public function go($data = '')
{
if(isset($data)){
echo '<pre>';
print_r($data);
}else{
echo '<pre>';
print_r($this->data);
}
}
}
/**/
As you can see i am using Phil Sturgeon's template library and i am handling the layout with Jamierumbelow's techniques.
When i set a message on form insertion failure its fine. I can display it in the _remap like this
echo $this->message->display();
In the controller its working finebut when i call it in the partial navigation it does not display the message. What can possibly be the problem. I have tried on the different places in the My_Controller. Its working fine but not in the partial or even i have tried it in the failed form i am loading again.
This is the message library i am using
https://github.com/jeroenvdgulik/codeigniter-message
Here i s my navigation partial
<nav>
<div id = "navigation">
<ul id="menubar">
<li>Home</li>
<li>Downloads</li>
<li>About Us</li>
</ul>
</div>
<div id="breadcrumb">
<div class="breadcrumbs">
<!-- Here i will pull breadcrumbs dynamically-->
</div>
<!--<h3>Dashboard</h3>-->
</div>
<br clear = "all"/>
<div id="message">
<?php
$data['message'] = $message ;
$this->load->view('messages/success',$data);?>
</div>
</nav>
The message library is using session might be flashdata so i think its loosing session data somehow. Although i am using sessions correctly autoloading it.
I have found the issue. It was very simple. I was using the base url in config file as empty
$config['base_url'] = '';
I have to change it like this
$config['base_url'] = 'http://localhost/myproject/';

Categories