Welcome Guest, Not a member yet? Register   Sign In
Problem with if else.
#1

[eluser]gazza7364[/eluser]
Hi

Not sure why this happening, I have a if else statement, which works if validation is true, but if the validation is false, the echo works, but then carries on as through the validation as if it was true. In other words the statement gets echoed, when the validation is false, then continues as if the the validation is true, but then shows errors because, the subect has no data.

The code is as follows.

Code:
function update($id = 0) {
settype($id, 'integer');
$criteria = array('photo_id' => $id);


$this->form_validation->set_rules('subject', 'subject', 'required|trim|xss_clean|max_length[255]');  

if ($this->form_validation->run() == FALSE) // validation hasn't been passed
  {

   echo 'Canot update without Title.  Please input Title.' . anchor('http://localhost/photos/edit/'.$id, 'Go Back and Update'  );
  
  }
  else // passed validation proceed

  {

  
  // build array for the model

  $photo_data = array(
      
         'subject' => set_value('subject'),
    );

  


   }
  
   if($this->photo_model->update($photo_data, $criteria) == true)
   {
   redirect('photos/index');   // goes to index page after update
   }

  }
Maybe can't see the wood for the trees.

Thanks for looking.
#2

[eluser]gazza7364[/eluser]
Hi, I can get round this by using
Code:
return FASLSE;
after the echo, but should I need to?
#3

[eluser]boltsabre[/eluser]
Well, when your form validation fails, it executes the code in the "if" part of your statement, but skips the part of your "else" statement... however, any code after the closing } of your if/else statement will get executed as well.

So, simple solution, just move all your "passed validation" code inside your else statement so it wont run unless validation passed!
#4

[eluser]gazza7364[/eluser]
[quote author="boltsabre" date="1333023977"]Well, when your form validation fails, it executes the code in the "if" part of your statement, but skips the part of your "else" statement... however, any code after the closing } of your if/else statement will get executed as well.

So, simple solution, just move all your "passed validation" code inside your else statement so it wont run unless validation passed![/quote]

Thanks, I can see where I was going wrong, since I have move the closing } for the else statement has suggested, everything is now working.

I just need another set of eyes to mull over the code.

Thanks again.




Theme © iAndrew 2016 - Forum software by © MyBB