⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Pass value from one field to callback function of another field



archerwisdom
  • profile picture
  • Member

Posted 17 November 2013 - 12:47 PM

Hi,

 

If I have this:

 

$crud->callback_field('function_time_selector',array($this,'functime_callback'));

 

$crud->fields('function_address','contact_person','contact_no_1','contact_no_2','contact_no_3','fax',
                          'email','function_date', 'function_time', 'function_purpose');
 
function functime_callback($value = '', $primary_key = null)
    {

/*  some code to display the dropdown list*/

.

.

}

 

Question is: Can I pass the value of function_time to the  functime_callback ? I need the value from function_time to reproduce two dropdown list for function_time_selector field.


MitchFurn
  • profile picture
  • Member

Posted 18 November 2013 - 17:13 PM

Amazing data, Kudos. Also visit my site [url=https://www.facebook.com/bannamkhing1]คอกกั้นเด็ก[/url]

Amit Shah
  • profile picture
  • Member

Posted 21 November 2013 - 09:57 AM

Well... there is no way as for now that you can pass.. it the same way - u can still retrive the data from the table using the primary key to do the population,


archerwisdom
  • profile picture
  • Member

Posted 03 April 2014 - 16:02 PM

hi Amit,

 

Did you meant retrieve data inside the callback function?

 

how can we retrieve the data from the table within the callback function method?


Amit Shah
  • profile picture
  • Member

Posted 03 April 2014 - 18:11 PM

Yes,

 

when u do a callback - u see u are getting $primary_key

that - u can use it and retrieve the value from the row .. and build whatever output u want to


archerwisdom
  • profile picture
  • Member

Posted 04 April 2014 - 02:45 AM

I presume the answer is that, yes, it is possible to retrieve the data inside the callback_field function.

 

is there a code or function in CRUD to retrieve the data?


Amit Shah
  • profile picture
  • Member

Posted 05 April 2014 - 04:48 AM

Code example below

if($crud->getState() == 'edit') {     /// check state if it is edit
	$pk = $crud->getStateInfo()->primary_key;    //get the primary key
	$row = $this->cModel->getByField('users', 'id', $pk);    // get the record based on primary key
	$client_id = $row['client_id']; ... do something
}

Following is the sample code in a callback to get the primary key and work with the check

function email_check($email) {
		$primary_key = $this->uri->segment(4);       ///u need to check /set the statement right.. 
		$rows = $this->cModel->getAllFor('users', 'email', $email);
		if(!empty($primary_key) && is_numeric($primary_key))
		{
			if(count($rows)>0) {
				if($rows[0]['id'] != $primary_key) {
					$this->form_validation->set_message('email_check', 'The email - ' . $email . ' already is assigned to some other user');
					return false;
				}
			}
			return true;
		} else {
			if(count($rows)>0) {
				$this->form_validation->set_message('email_check', 'The email - ' . $email . ' already is assigned to some other user');
				return false;
			} else {
				return true;
			}
		}
	}

archerwisdom
  • profile picture
  • Member

Posted 16 April 2014 - 13:14 PM

hi amit,

 

do i have to write my own getByField method in model for the following code? 

$row = $this->cModel->getByField('users', 'id', $pk);

 

cause i couldn't find getByField in any files.


archerwisdom
  • profile picture
  • Member

Posted 16 April 2014 - 14:07 PM

suppose my control look like this: just part of the code:

<?php
    public function orders()
    {
            
        if (!$this->ion_auth->logged_in())
        {
                //redirect them to the login page
                redirect('auth/login', 'refresh');
        }
        elseif (!$this->ion_auth->is_admin()) //remove this elseif if you want to enable this for non-admins
        {
                //redirect them to the home page because they must be an administrator to view this
                return show_error('You must be an administrator to view this page.');
        }
        else
        {
            $crud = new grocery_CRUD();

            //$crud->set_relation('customerNumber','customers','{contactLastName} {contactFirstName}');
            //$crud->display_as('customerNumber','Customer');

            $crud->set_table('orders');
            $crud->set_theme('datatables');
            $crud->set_relation_n_n('disha',  'order_disha', 'disha', 'order_id', 'disha_id', '{disha.disha_id}   {disha_name}','priority');
            ...
            $crud->set_subject('Order');

            
            
            /*set rules*/
            $crud->set_rules('email', 'Email', 'trim|valid_email');
            $crud->set_rules('price_extra', 'Price Extra', 'decimal');
            
            $crud->display_as('function_address','Function Address')
            ->display_as('contact_person','Contact Person')
            ->display_as('contact_no_1','Phone 1')->display_as('contact_no_2','Phone 2')->display_as('contact_no_3','Phone 3')
            ....
            ->display_as('disha','Set A')->display_as('dishb','Set B')->display_as('dishc','Set C')->display_as('dishd','Set D')
            ->display_as('dishe','Set E')->display_as('alacart','Alacart');
            
            
            $crud->callback_field('function_time_selector',array($this,'functime_callback'));
              
            $crud->callback_field('price_extra',array($this,'price_extra_callback'));
            $crud->callback_field('function_purpose',array($this,'purpose_callback'));
            
            $crud->callback_column('function_date',array($this,'_date_callback'));

          
            $crud->columns('function_address','contact_person','contact_no_1','contact_no_2','contact_no_3','email','fax','price','function_date','remark');

            $crud->callback_field('price-dd',array($this,'price_callback'));
 
            if($crud->getState() == 'edit' || $crud->getState() == 'add'){
                
                $crud->field_type('function_time', 'hidden', 0);
                
                
                $crud->fields('function_address','contact_person','contact_no_1','contact_no_2','contact_no_3','fax',
                          'email','function_date', 'function_time', 'function_time_selector', 'function_purpose', 'purpose_other','pax', 'deposit', 'price-dd',
                           'price','price_extra', 'remark', 'disha', 'dishb', 'dishc', 'dishd', 'dishe', 'alacart',
                          
                          'staff_chef_1','staff_chef_1_ic','staff_chef_1_salary',
                          'staff_chef_2','staff_chef_2_ic','staff_chef_2_salary',
                          ...
                          'staff_waiter_15','staff_waiter_15_ic','staff_waiter_15_salary'
                          );
                
                $crud->callback_field('function_time',array($this,'set_functiontime_session_callback'));
               
                
                
            }else{
                $crud->fields('function_address','contact_person','contact_no_1','contact_no_2','contact_no_3','fax',
                          'email','function_date', 'function_time', 'function_purpose', 'purpose_other','pax', 'deposit', 'price-dd', 'price',
                          'price_extra', 'remark', 'disha', 'dishb', 'dishc', 'dishd', 'dishe', 'alacart',
                          
                          'staff_chef_1','staff_chef_1_ic','staff_chef_1_salary',
                          ...
                          'staff_waiter_15','staff_waiter_15_ic','staff_waiter_15_salary'
                          );
                
            }
            
           
            $crud->order_by('function_date','desc');

            //$crud->callback_update(array($this,'updatestaff_callback'));
            
            /*use for Page Title*/
            $this->state = $crud->getState();
            $this->load->vars( array( 'html_title' => $this->state." order") );
                
            $crud->unset_print();
            
            $crud->callback_before_update(array($this,'update_functiontime_callback'));
            $crud->callback_before_insert(array($this,'add_functiontime_callback'));
            
            $output = $crud->render();
 
          
            $this->_example_output($output);
            
        }
        
    }
    
    
     function price_callback($value = '', $primary_key = null)
    {

print_r($primary_key);
        if($this->state == "read"){
            
            return '<div id="field-price" class="readonly_label">'.$value.'</div>';
        
        }else{
            
            return '
            <select id="field-price" name="price">
                <option value="24.80" '.($value == 24.80 ? "selected" : "").'>Set A - RM24.80</option>
                <option value="25.80" '.($value == 25.80 ? "selected" : "").'>Set B - RM25.80</option>
                <option value="28.80" '.($value == 28.80 ? "selected" : "").'>Set C - RM28.80</option>
                <option value="30.80" '.($value == 30.80 ? "selected" : "").'>Set D - RM30.80</option>
                <option value="26.80" '.($value == 26.80 ? "selected" : "").'>Set E - RM26.80</option>
                <option value="" '.($value == "" ? "selected" : "").'>Other</option>
            </select>

            ';
        }
    }
    

    
?>

in my price_callback(), i manage to print the primary_key value.

but what i want to achieve is getting the price value fm db and match it with the option value in the dropdown list so that it will display the option value as selected, based on the value in db table and it should happen when I enter Edit record mode. what should i do to get the price value from the db ?

 

hope this code will give clearer pic of my question.