CodeIgniter Forums

Full Version: Advice or help accessing object items.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello!

I am fairly new to Codeigniter and have come up against something that is alluding me.  Perhaps it's the time of day, or that my code isn't as elegant as it could be.

Summary of problem.

I have a view that has a dependent dynamic dropdown.  When you select from first dropdown, a java function makes an ajax call to a controller function which uses a model to pull options for the second dropdown from a DB.

When I SAVE a DB record to reflect the chosen options it saves the "value" of the <option> tag.  When I LOAD that record I get the value back, but I actually need the NAME, not the value.

So, I'm trying to include a translator that will catch the data at the controller as it comes from the model to the view, translate the value into the name and then include the name in the object being passed to the view. 

What I need to do is access one specific element of the object.  I'd rather not create an array, parse the object into the array, and then use the array element to pass back into the model to retrieve the name.

Controller:
Code:
       function loadRMA(){             //Function to load the RMA data from the DB
           $data['rmaRecordMode'] = "view";                            //Just view the record data
           $data['rmaNumberToGet'] = $this->input->post('GetThisRmaNum');
           $this->load->model('rma_model');
           $data['assemblies'] = $this->rma_model->getAssembly();      //Get Assemblies from DB
           $data['rmaData'] = $this->rma_model->getRMAData();          //Get specific RMA data
            
            //NEED TO GET ELEMENTn OF rmaData and pass it to $this->rma_model->translateDropDown(n, rmaData value n)            
            $this->load->view('includes/rma_header', $data);
            $this->load->view('rma_home', $data);
            $this->load->view('includes/footer');
       }
MODEL:
Code:
   public function translateDropDown($type, $id){  //Function to translate the dropdown saved index value to its name value
       if($type == 'category'){
           $fieldList='category';
           $table='fx_categorycomponentsymptom';
           $fieldName='id';
           $orderByField='category';            
       }else if($type == 'component'){
           $fieldList='component';
           $table='fx_categorycomponentsymptom';
           $fieldName='id';
           $orderByField='component';            
       }else if($type == 'symptom'){
           $fieldList='symptom';
           $table='fx_categorycomponentsymptom';
           $fieldName='id';
           $orderByField='symptom';                
       }
       
       $this->db->select($fieldList);
       $this->db->from($table);
       $this->db->where($fieldName, $id);
       $this->db->order_by($orderByField, 'asc');
       $query=$this->db->get();
       return $query;    
       
   }//end translateDropDown

    public function getRMAData(){   //FUNCTION TO PULL RMA DATA FROM SB TABLE
       $this->db->select('*');
       $this->db->from('rma');
       $this->db->where('rmaNumber', $this->input->post('GetThisRmaNum'));
       $query = $this->db->get();
       return $query->result();
   }

Any thoughts and/or suggestions?
Once you get the posted value, how do you save it if you don't know the name of the dropdown element in your form?
The selected value in a dropdown belongs to the name of the <select> ... </select> stucture.
This part needs some more explanation, please also share the code of your view.

Besides that, your function translateDropDown can be a lot shorter:
PHP Code:
public function translateDropDown($type$id){  

        
$table 'fx_categorycomponents' $type;
        
$this->db->select($type);
        
$this->db->from($table);
        
$this->db->where('id'$id);
        
$this->db->order_by($type'asc');
        
$query=$this->db->get();
        return 
$query;    
        
    }
//end translateDropDown 
(03-18-2016, 06:27 AM)Wouter60 Wrote: [ -> ]Once you get the posted value, how do you save it if you don't know the name of the dropdown element in your form?
The selected value in a dropdown belongs to the name of the <select> ... </select> stucture.
This part needs some more explanation, please also share the code of your view.

Besides that, your function translateDropDown can be a lot shorter:
PHP Code:
  public function translateDropDown($type$id){  

        $table 
'fx_categorycomponents' $type;
 
       $this->db->select($type);
 
       $this->db->from($table);
 
       $this->db->where('id'$id);
 
       $this->db->order_by($type'asc');
 
       $query=$this->db->get();
 
       return $query   
        
    
}//end translateDropDown 

Thanks for the reply Wouter60.  I like your code reduction, I implemented it the way I did as there is a looming potential that the dropdown elements won't all be contained in one table, but given that, and your advice, I think I can use your reduction with a slight alteration.  Much appreciated.

As to the main issue.  I probably did a poor job of communicating my issue/request.  I know the name of the <select> element, but I'm looking to translate the option text.  <option value="1">THIS TEXT</option>.   The option value "1" is saved in the DB, so when I recall this I get "1" back, but need to translate that into the appropriate "THIS TEXT" for display (in a html table, not in a dropdown).


Relevent View Code:

Code:
        <select name="componentDropDown" onchange="selectSymptom(this.options[this.selectedIndex].value)">
                   <option value="-1">Select component</option>
                   <?php foreach($list->result() as $listElement){ ?>
                   <option value="<?php echo $listElement->id; ?>"> <?php echo $listElement->component; ?></option>
                   <?php } ?>
               </select>
               <?php } ?>
           </td>
       </tr>
       <tr>
           <td>WWI Reported Failure Mode:</td>
           <td colspan="2">
               <?php if($rmaRecordMode == "view"){echo $rmaData[0]->wwiReportedFailMode;} else { ?>
               <select name="symptomDropDown" id="symptomDropDown">
                       <option value="-1">Select symptom</option>
               </select>
        <?php } ?>


Controller Code to Save DD Values back to DB:

Code:
       function saveMode(){
           $doThis = $this->input->post('submit');                 //Catch whether Save or Cancel was hit.
           $this->load->model('rma_model');
           $data['rmaData'] = $this->rma_model->getRMAData();          //Get specific RMA data - Needed in whole method scope
           
           if($doThis == "Cancel"){
               $data['rmaRecordMode'] = "view";
               $data['rmaNumberToGet'] = $this->input->post('GetThisRmaNum');
               $data['assemblies'] = $this->rma_model->getAssembly();      //Get Assemblies from DB
               //$data['rmaData'] = $this->rma_model->getRMAData();  //Moved this out of the IF.
               $this->load->view('includes/rma_header', $data);
               $this->load->view('rma_home', $data);
               $this->load->view('includes/footer');
           } else {
               $pushData = array(
                   'rmaReceived' => $this->input->post('rmaRec'),
                   'rmaNumber' => $this->input->post('GetThisRmaNum'),                     //BYPASS??
                   'rmaAssembly' => $this->input->post('rmaAssembly'),
                   'rmaComponent' => $this->input->post('rmaComponent'),
                   'dateOfInstall' => $this->input->post('dateOfInstall'),
                   'dateOfReplacement' => $this->input->post('dateOfReplacement'),
                   'wwiReportedComponent' => $this->input->post('componentDropDown'),
                   'wwiReportedFailMode' => $this->input->post('symptomDropDown'),
                   'wwiComment' => $this->input->post('wwiComment'),
                   'failedSerial' => $this->input->post('failedSerial'),
                   'replacementSerial' => $this->input->post('replacementSerial')
               );
               $this->rma_model->updateRecord($pushData);
               $this->loadRMA();   //Update Record done.  Now reload it and display in view mode.
           }
       }