⚠ 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

Change Available Fields Based on Another Fields Selection



Hiro

Hiro
  • profile picture
  • Member

Posted 26 May 2012 - 04:19 AM

Here is the situation, I have a Quality Assurance database that I am working on. My current fields are

Task Name: (Datatype: VARCHAR)

Task Category: (Datatype: INT (its a referenced ID to another database)) So it matches the Cat_ID to Category Name

Task Description: (Datatype: Text)

IMG_Upload 1: (Datatype: Text) Upload Field 1

IMG_Upload 2: (Datatype: Text) Upload Field 2

Here is what I need, when the user selects the Task Category it needs to change the IMG_Upload Fields so that in the case of Cat_ID 1 only IMG_Upload 1: is shown

and ID2 = IMG_Upload 2: only.

I currently have it set up where when you add a new entry it does not show the upload fields so that when I edit the entry I take the uri_string which is /main/QADatabase/edit/1

The Function then proceeds to delete all non-integers from the string reducing it to 1 (Which is the ID of the QATask, I have a model setup to get the cat_id based on the QATask_ID which returns 2 (in my current database).

I have a switch statement setup in the controller function that changes the edit_fields like so:


switch((int)$category)
{
case 1:
$crud->edit_fields('cust_id', 'category_id', 'task_description','img_upload1');
break;
case 2:
$crud->edit_fields('cust_id', 'category_id', 'task_description','img_upload2');
break;
default:
$crud->edit_fields('cust_id', 'category_id', 'task_description');
break;
}


This seems to work, however it only shows either case 1 or 2 depending on which one I load first unless I clear my history. I think the problem is once render() is run it does not run the code again to get the new uri_string().

Is there an easier way to go about doing this.

Here is the Controller Function:

public function QADatabase() {

$crud = new grocery_CRUD();

$crud->set_theme('datatables');
$crud->set_table('qa_database');
$crud->set_subject('QA Database');

$this->load->model('qa_model');

$category = '';

$str = uri_string();
$num = '';
for( $i=0,$c=strlen($str); $i<$c; $i++ )
if ( is_numeric($str[$i]) )
$num .= $str[$i];

if($num != '')
$category = $this->qa_model->getCategory($num);


$crud->add_fields('cust_id', 'category_id', 'task_description');
switch((int)$category)
{
case 1:
$crud->edit_fields('cust_id', 'category_id', 'task_description','img_upload1');
break;
case 2:
$crud->edit_fields('cust_id', 'category_id', 'task_description','img_upload2');
break;
default:
$crud->edit_fields('cust_id', 'category_id', 'task_description');
break;

}

$crud->required_fields('cust_id');
$crud->set_relation('category_id','qa_category','cat_name');

$crud->display_as('cust_id', 'Customer ID')
->display_as('category_id', 'Task Category')
->display_as('task_description', 'Task Description')
->display_as('img_upload1', 'IMG Upload 1')
->display_as('img_upload2', 'IMG Upload 2');

$crud->set_field_upload('img_upload1','assets/qa-images/upload1');
$crud->set_field_upload('img_upload2','assets/qa-images/upload2');

$output = $crud->render(); $this->view('grocery_CRUD', $output, 'main_user_management');
}