Welcome Guest, Not a member yet? Register   Sign In
Why Does my Flashdata Not Work as Expected
#1

[eluser]vincej[/eluser]
Hi - I'm sure I'm doing something dumb. But I can't see it. My Flashdata message should appear when there is nothing in the DB table which meets the query. However, it only appears if I select the controller twice in succession. The First time I select it, my Flashdata message is ignored even though there is nothing in the DB to report - UGH !

I have checked my braces adn it all looks good, but obviously it isn't - can anyone see where I am going wrong ? Many Thanks !

Model:
Code:
function completesale () {
$sql = "
SELECT picking,sale
FROM `confirmedorder`
WHERE picking = 'finished'
AND sale = 'open'
";
$Q=$this->db->query($sql);  
  if($Q->num_rows()> 0){
  $completesale = $Q->result_array();
  return $completesale;  
  }
else $this->session->set_flashdata('payment_error','No Orders Waiting Payment');
}

View:

Code:
if ($this->session->flashdata('payment_error')){

echo "<p class='message'>". $this->session->flashdata('payment_error')."</p>";
}

#2

[eluser]cideveloper[/eluser]
Flash data is only available for the next server request

I assume the view file you listed is being called in the same server request.
#3

[eluser]weboap[/eluser]
ok start by

Code:
}
else {   //<<<<<<<- adding this brace

       $this->session->set_flashdata('payment_error','No Orders Waiting Payment');
}


can you post the relevant part of the controller?
#4

[eluser]vincej[/eluser]
Sure - Here is the controller:

Code:
function completesale () {
$data['title'] = 'CountryLanes | Complete Sale';
$data['completesale'] = $this->MPos->completesale();
$customerid = ($data['completesale']['0']['CustomerID']);
$data['customerfullname'] = $this->MCustomer->customerfullname($customerid);
$data['main'] = 'admin_completesale';
$this->load->vars($data);
$this->load->view('dashboard');
}
#5

[eluser]InsiteFX[/eluser]
First do what weboap said above, if still doe's not show up then you may need to use the below!

Code:
$this->session->set_flashdata('payment_error','No Orders Waiting Payment');

// If you find that you need to preserve a flashdata variable through an additional request,
// you can do so using the keep_flashdata() function.
$this->session->keep_flashdata('payment_error');
#6

[eluser]weboap[/eluser]
can you return a false if case nothing found in db,
then set the flash data in the controller(im guessing as @cideveloper said you got some call to the server in between that is clearing the flash data, maybe some ajax request... )
try and post back plz
#7

[eluser]vincej[/eluser]
I think cideveloper prorbaly has it right. My controller makes a second server call:

Code:
$data['customerfullname'] = $this->MCustomer->customerfullname($customerid);

However I could not make my flashdata work so in the end I took the 'lazy' way out and simply put this in my view:

Code:
if (empty($completesale)){

echo "<p class='message'> No Accounts Waiting Payment</p>";
}
#8

[eluser]cideveloper[/eluser]
[quote author="vincej" date="1337031559"]I think cideveloper prorbaly has it right. My controller makes a second server call:

Code:
$data['customerfullname'] = $this->MCustomer->customerfullname($customerid);

However I could not make my flashdata work so in the end I took the 'lazy' way out and simply put this in my view:

Code:
if (empty($completesale)){

echo "<p class='message'> No Accounts Waiting Payment</p>";
}
[/quote]

I think everyone has taken what I said in the wrong way. The reason it is not working is that flash data is used to persist data to another page on redirect, then kill that data. Your "lazy" method is actually correct in that you are not redirecting to another page so you dont actually need flash data. Your "second server call" has nothing to do with it. take for example the below.

Controller
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class FD extends CI_Controller {

function __construct(){
  //Contructor function
  parent::__construct();
  
  //$this->output->enable_profiler(TRUE);
}

function index() {
  $data['payment_id'] = $this->completesale();
  if($data['payment_id']) {
   $this->load->vars($data);
   $this->load->view('test/test1');
  } else {
   redirect('fd/secondpage');
  }
}

function secondpage() {
  $data = array (
   'payment_id' => ''
  );
  $this->load->vars($data);
  $this->load->view('test/test1');
}

function completesale() {
  $db_result = false;
  if($db_result) {
   return $db_result;
  } else {
   $this->session->set_flashdata('payment_error','No Orders Waiting Payment');
   return false;
  }
}

}

View
Code:
&lt;?php

echo 'Hello World<br />' . chr(10);

if ($this->session->flashdata('payment_error')){
echo "<p class='message'>". $this->session->flashdata('payment_error')."</p>";
} else {
echo $payment_id . '<br />' . chr(10);
}
?&gt;

Not the best example but proves the point. When I said second call I meant redirect.
#9

[eluser]vincej[/eluser]
Cheers CI - your'e a star. I learned something new today.




Theme © iAndrew 2016 - Forum software by © MyBB