Welcome Guest, Not a member yet? Register   Sign In
check value in $_POST variable
#1

[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
#2

[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')            
);
#3

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

[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.
#5

[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?
#6

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




Theme © iAndrew 2016 - Forum software by © MyBB