CodeIgniter flashdata - codeigniter

I'm running into a problem I just can't seem to fix. I'm working on kind of a CMS kind of thing. And one of the functions is making image slideshows.
In one controller, I check if a slideshow with a certain ID exists, and if it exists, it should take the data and work with it, otherwise, set an error message (CodeIgniter flashdata) and redirect to the homepage.
The code I use for this is the following:
if($this->Mslideshow->slideshowExists($showid) === FALSE){
echo 'I\'m getting here';
$this->session->set_flashdata('error',$showid);
redirect('admin/index','refresh');
}else{
echo 'Slideshow exists';
}
And the code of the slideshowExists() function is:
public function slideshowExists($showid)
{
$this->db->where('id',$showid)
->limit(1);
$query = $this->db->get('slideshows');
if($query->num_rows() < 1){
return FALSE;
}
$this->currentquery = $query;
return TRUE;
}
And the problem is this, very strange actually. If the result I get back is FALSE, everything goes as planned. Error message gets set and redirect goes to 'admin/index'.
But if what I get back is TRUE, then the stange thing happens. I do get an echo with 'Slideshow exists', but I also get the error message.
I've tried everything, deleted cookies. Reset all sessions etc.
And even stranger is that when I tried to put the $showid I use to check into the error message, all of a sudden $showid is ' img '. While everywhere else is '1' or '2'...
Hope someone can help. Thanks!
=====EDIT=====
I tried to edit the code and simplify it. Right now I have this code in my Controller:
public function slideshow($showid){
$query = $this->db->where('id',$showid)->get('slideshows');
if($query->num_rows() < 1){
$this->session->set_flashdata('error','Slideshow doesn\'t exist.');
redirect('admin/index','refresh');
}
$data['page'] = 'slideshow';
$data['title'] = 'Slideshows';
$this->scripts->load_scripts(array());
$this->scripts->load_functions(array());
$this->load->view('admin/dashboard_template.php',$data);
}
When I run this with a $showid, that doesn't exist, I get the error message after being redirected to 'admin/index'. When I use a $showid that does exist, I do get the error, but no redirect but just the rest of the code.

I think you have to read your flash data in your view:
$error = $this->session->flashdata('error');
var_dump($error);
or in your Controller:
$error = $this->session->flashdata('error');
if(isset($error)) {
var_dump($error);
}
Also you can read this question: CodeIgniter "flashdata" doesn't work

you can get the flash data by following way
$error = $this->session->flashdata('error');
if($error)
{
echo $error;
}

You can try
if($this->session->flashdata('msg') != "")
This works for me.

Related

issue with making script to delete table with laravel controller

I am trying to make script with which I can delete table with single link hitting. for that I have make below code
Route::get('delete_table','HomeController#delete_table')->name('delete_table');
public function delete_table(){
$delete = DB::table('test')->delete();
if($delete){
echo 'success';
}else{
echo 'error';
}
}
but when I hit this route in my browser I have received error from else condition but I don't know why it is not working.
can anybody help me in this?

handling database error with ajax request codeigniter

I am intentionally making error i.e. using one of the coulmn's name wrong to learn how to handle the error with ajax call with codeigniter.
Last Comment in controller function at the end is my question/problem
My AJX Code is following. I am using ajaxform plugin. It shows me all response from controller perfectly but problem is only I am unable to get response from model in controller while using ajax call that is little weird
$('#myForm').ajaxForm
({
success: function(responseText)
{
if(responseText != "1")
$('#feedback').html(responseText);
}
});
Following is snapshot of exact error which i can see in console but unable to get in controller and hence in view. It describes complete error i.e unknown column in given query, but i captured only upper portion.
My model function is below
public function updateItem($id, $data, $tbl)
{
$this->db->where('id', $id);
$r = $this->db->update($tbl, $data);
if($r)
return $r;
else
{
$r = $this->db->_error_message();
return $r;
}
}
My controller function code
public function upadteme()
{
$r = $this->ajax_model->updateItem($uid, $data, 'users');
echo $r." -- "; // Unable to get this echo working
//when using ajaxcall (calling controller through ajax) otherwise fine
}
It looks like the class will not populate _error_message if db_debug is on which it appears to be by default.
In config/database.php, set
$db['default']['db_debug'] = FALSE;
and try again.

change any rule in codeigniter to match function

I have set up my routes.php to suit the nature of my site. Unfortunately, there is a problem with my last route i.e.:
$route['(:any)'] = "profile/profile_guest/$1";
If the username password name passed is correct, for e.g. domain.com/username, it will load his/her data. if not, it loads the page with errors (because failure to retrieve data with non-existent user in database). This is my problem! I want to avoid this error showing.
Is there anyway I could avoid this error from happening? None of the echoes seems to be printing or the redirect neither is working. Don't know why! it is as if the code inside this method is not executing and the view is loaded instead. below is part of the profile_guest function:
public function profile_guest($username)
{
$username = $this->uri->segment(1);
//echo "Hello " . $username;
redirect('main', 'refresh');
if($username != '')
{
/* echo "<h1>HELLO WORLD SOMETHING</h1>"; */
It's hard to say without seeing the rest of the code.
Maybe you need to check the value before running the query:
// user_model
function get_user($username = NULL){
if($username){
return $this->db->query(...
}else{
return false;
}
}
Then check that the query returned anything before loading the view
if($this->user_model->get_user($username){
//show the page
}else{
echo "no user found";
}

Infinite loop error when using header in observer

I'm just trying to set up terms and conditions page after a successful login. All seems to to working well. I am getting the post data through the form which I have placed in CMS Page with identifier 'general-conditions'. The problem is only in the
header('Location: '.Mage::getUrl('general-conditions'));
If I comment out this line.. page loads properly but if I don't it gets caught in infinite loop.
Could anyone please help me, that's the only thing left and I've spent a lot of time on it.
Thanks in advance.
<?php
Class Rik_Terms_Model_Observer{
public function checkGeneralTerms(){
if (Mage::helper('customer')->isLoggedIn()) {
$id = Mage::getModel('customer/session')->getId();
$customer = Mage::getModel('customer/customer')->load($id);
$customerData = $customer->getData();
$groupId = Mage::helper('customer')->getCustomer()->getGroupId();
$groupName = Mage::getModel('customer/group')->load($groupId)->getCode();
$storeId = Mage::app()->getStore()->getId();
if($customer->getGeneralTerms()=='0'){
$pageIdentifier = Mage::getModel('cms/page')->checkIdentifier("general-conditions", $storeId);
if ($pageIdentifier){
header('Location: '.Mage::getUrl('general-conditions')); // problem
die();
}
}
}
}
}
Found later, it was actually conflicting with extended version of my CMS Module.

Redirect to show_404 in Codeigniter from a partial view

I am using the HMVC pattern in CodeIgniter. I have a controller that loads 2 modules via modules::run() function and a parameter is passed to each module.
If either module cannot match the passed paramter I want to call show_404(). It works, but it loads the full HTML of the error page within my existing template so the HTML breaks and looks terrible. I think I want it to redirect to the error page so it doesn't run the 2nd module. Is there some way to do that and not change the URL?
Is it possible to just redirect to show_404() from the module without changing the URL?
Here is an over simplified example of what's going on:
www.example.com/users/profile/usernamehere
The url calls this function in the users controller:
function profile($username)
{
echo modules::run('user/details', $username);
echo modules::run('user/friends', $username);
}
Which run these modules, which find out if user exists or not:
function details($username)
{
$details = lookup_user($username);
if($details == 'User Not Found')
show_404(); // This seems to display within the template when what I want is for it to redirect
else
$this->load->view('details', $details);
}
function friends($username)
{
$details = lookup_user($username);
if($friends == 'User Not Found')
show_404(); // Redundant, I know, just added for this example
else
$this->load->view('friends', $friends);
}
I imagine there is just a better way to go at it, but I am not seeing it. Any ideas?
You could throw an exception if there was an error in a submodule and catch this in your controller where you would do show_404() then.
Controller:
function profile($username)
{
try{
$out = modules::run('user/details', $username);
$out .= modules::run('user/friends', $username);
echo $out;
}
catch(Exception $e){
show_404();
}
}
Submodule:
function details($username)
{
$details = lookup_user($username);
if($details == 'User Not Found')
throw new Exception();
else
// Added third parameter as true to be able to return the data, instead of outputting it directly.
return $this->load->view('details', $details,true);
}
function friends($username)
{
$details = lookup_user($username);
if($friends == 'User Not Found')
throw new Exception();
else
return $this->load->view('friends', $friends,true);
}
You can use this function to redirect 404 not found page.
if ( ! file_exists('application/search/'.$page.'.php')) {
show_404(); // redirect to 404 page
}
its very simple , i solved the problem
please controler name's first letter must be capital e.g
A controller with
pages should be Pages
and also save cotroler file with same name Pages.php not pages.php
also same for model class
enjoy

Resources