How to neglect "|" in Codeigniter

Hi I'm new to PHP and Codeigniter.

This is my controller code. Here I add '|' to each value. If I have no value in name3 and value3 then I do not add '|' the character. How can I check and insert it?

controller

       function event()
       {
        $event_name=$this->input->post('e_name'); 
        $unit1=$this->input->post('unit1');
        $unit2=$this->input->post('unit2');
        $unit3=$this->input->post('unit3');
        $unit4=$this->input->post('unit4');
        $value1=$this->input->post('value1');
        $v_unit1=$value1.'|'.$unit1;

        $name2=$this->input->post('name2');
        $value2=$this->input->post('value2');
        $v_unit2=$value2.'|'.$unit2;

        $name3=$this->input->post('name3');
        $value3=$this->input->post('value3');
        $v_unit3=$value3.'|'.$unit3;

        $name4=$this->input->post('name4');
        $value4=$this->input->post('value4');
        $v_unit4=$value4.'|'.$unit4;

        $event_data=array(
           'event_name'=>$event_name,
           'name1'=>$name1,
           'value1'=>$v_unit1,
           'name2'=>$name2,
           'value2'=>$v_unit2,
           'name3'=>$name3,
           'value3'=>$v_unit3,
           'name4'=>$name4,
           'value4'=>$v_unit4,
       );

       $this->home_model->insert_event($event_data);
       }

      

In my release, I get '|' if I have not entered the data. I want a Null value to be entered into the database.

If the value exists, the data will be in this format: eg. 22 as value and kg as unit, it should be formatted as 22|kg

, if no value is entered I get '|'. It is my problem.

+3


source to share


1 answer


Just do it using !empty

like

$v_unit1 = (!empty($unit1) && !empty($value1)) ? $value1.'|'.$unit1 : '';

      



This will check if the variable isset

along with it is not empty

too

0


source







All Articles