CodeIgniter Forums
check value in $_POST variable - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: check value in $_POST variable (/showthread.php?tid=23159)



check value in $_POST variable - El Forum - 10-02-2009

[eluser][email protected][/eluser]
I want to some idea to make code simplifly more than this
In the controller I have a code
if($this->input->post('name')!='')
{
$data = array(
'name' => $this->input->post('name'),
'number' => $this->session->userdata('number')
);
}
if($this->input->post('number')!='')
{
$data = array(
'name' => $this->session->userdata('name'),
'number' => $this->input->post('number'),

);
}

.....
when I have many index in array $_POST I must write if function more and more
Can anyone tell me to make code simplifly more than that to check entry value may by interation or anything I have a loose.
Thank you


check value in $_POST variable - El Forum - 10-02-2009

[eluser]Phil Sturgeon[/eluser]
You can use the ternary syntax for this.

Code:
$data = array(
  'name' => $this->input->post('name') != '' ? $this->input->post('name') $this->session->userdata('name'),
  'number' => $this->input->post('number') != '' ? $this->input->post('number') $this->session->userdata('number')            
);



check value in $_POST variable - El Forum - 10-02-2009

[eluser][email protected][/eluser]
I met some error with unexpected with T_VARIABLE


check value in $_POST variable - El Forum - 10-02-2009

[eluser]BrianDHall[/eluser]
Phil's code got eaten by the code system. Between the calls to $this->input and $this->session there should be a colon (Smile.

So: 'name' => $this->input->post('name') != '' ? $this->input->post('name') : $this->session->userdata('name')


I actually didn't know ternary syntax could be used in an array definition like this, which is a pretty cool.


check value in $_POST variable - El Forum - 10-02-2009

[eluser][email protected][/eluser]
Thank alot with you pretty cool code,
Actually, my code is duty I want to make clean code for do that, I think with array filter with loop but don't no much?
I use jeditable with controller and model,if I have more database fileds the code can be confuse and more line
Can people help me to design for do that?


check value in $_POST variable - El Forum - 10-03-2009

[eluser]Colin Williams[/eluser]
People here seem to really despise IF ELSE control structures. I don't get it.