#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,689
    Rep Power
    171

    Help needed to build an array from post elements


    Hello; please help me make stop repeating myself, probably an array can do this easily:
    PHP Code:
    $data['fn'] = $this->input->post('fn') ? $this->input->post('fn') : "";    
                
    $data['ln'] = $this->input->post('ln') ? $this->input->post('ln') : "";    
                
    $data['ph'] = $this->input->post('ph') ? $this->input->post('ph') : "";    
                
    $data['e'] = $this->input->post('e') ? $this->input->post('e') : "";    
                
    $data['address'] = $this->input->post('address') ? $this->input->post('address') : ""
    Thank you
  2. #2
  3. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,303
    Rep Power
    9400
    1. As long as you have 5.3 you can abbreviate X?X:Y as X?:Y
    2. If you don't actually need a string in $data then you may not need the :"" and can just live with the false/null/whatever ->post() returns
    3. More arrays!
    PHP Code:
    foreach (array('fn''ln''ph''e''address') as $key) {
        
    $data[$key] = $this->input->post($key);
        
    // or $data[$key] = $this->input->post($key) ?: "";

  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,689
    Rep Power
    171
    Originally Posted by requinix
    PHP Code:
    foreach (array('fn''ln''ph''e''address') as $key) {
        
    $data[$key] = $this->input->post($key);

    Love your work

IMN logo majestic logo threadwatch logo seochat tools logo