⚠ 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

dropdown without set_relation, but from different table



Ron

Ron
  • profile picture
  • Member

Posted 02 April 2014 - 15:40 PM

How do I create a dropdown without having a relationship to a field. I can't use

 

$crud->set_relation.....

 

I do want to use something like this....

$crud->field_type('Field_Name','dropdown',
            array('1' => 'option_1', '2' => 'option_2,'3' => 'option_3 , '4' => 'option_4'));

but from a table, for example "tbl_Test" and create a list that is not related to any other fields.

 

Thank you for helping.

 

 


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 02 April 2014 - 18:26 PM

well you gave solution for yourself...

that is the way you have to do it.. what you need to do is select the entries / rows u want to put up into the dropdown and set it as array (key=>value) to the dropdown.


Ron

Ron
  • profile picture
  • Member

Posted 02 April 2014 - 19:13 PM

$this -> db -> select('Field_Name');
$this -> db -> from('tbl_Name');
$query = $this -> db -> get();

foreach ($query->result() as $row)
                {
                    $array = $query->result_array();
                }
            
$crud->field_type('Field_Name2','dropdown', array('1' => 'abc', '2' => 'bcs','3' => 'fff' , '4' => 'rrrr'));

Sorry, still a beginner in PHP. How the first 4 lines look? If those work, what is the syntax to get the array in the last row of code. ...'dropdown', $array); ?


Ron

Ron
  • profile picture
  • Member

Posted 02 April 2014 - 19:15 PM

It actually compiled. But I am getting a bunch of Array, Array, Array in the dropdown.


Ron

Ron
  • profile picture
  • Member

Posted 02 April 2014 - 19:15 PM

$this -> db -> select('Carrier_SCAC');
         $this -> db -> from('carrier');
         $query = $this -> db -> get();

foreach ($query->result() as $row)
{
$array = $query->result_array();
}

$crud->field_type('TT_Carrier','dropdown', $array);

Little more help?


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 02 April 2014 - 20:16 PM

Don't think you presenting the output in the required mechanism..

 

This should work .. likely.. didnt test but the code logically should work

$this -> db -> select('id, Carrier_SCAC');
         $this -> db -> from('carrier');
         $query = $this -> db -> get();
$finalArray = array();
foreach ($query->result() as $row)
{
	$finalArray[$row['id']]=$row['Carrier_SCAC'];
}

$crud->field_type('TT_Carrier','dropdown', $finalArray);

Ron

Ron
  • profile picture
  • Member

Posted 02 April 2014 - 20:29 PM

$this -> db -> select('ID, Carrier_SCAC');
$this -> db -> from('carrier');
$query = $this -> db -> get();
$finalArray = array();
foreach ($query -> result() as $row)
{
//$finalArray[$row['ID']] = $row['Carrier_SCAC'];
$finalArray = $query->result_array();

}

$crud->field_type('TT_Carrier','dropdown', $finalArray);

 

 

It doesn't compile if I leave the commented section in there.


Ron

Ron
  • profile picture
  • Member

Posted 04 April 2014 - 15:28 PM

Didn't understand the set_relation....solution

 

$crud->set_relation('TT_Carrier','users', 'Vendor',array('admin' => 'N'));


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 05 April 2014 - 04:44 AM

What is the problem going with the set relation function .. 1st point..

 

secondly in yr code above... u using this - $finalArray = $query->result_array();

.....this is always going to give trouble.. cuz the output of that if u print in .. is going to be all the result sets for the query fired... in number of rows returned. so that 1 is never gona work out...


Ron

Ron
  • profile picture
  • Member

Posted 07 April 2014 - 12:23 PM

I am using set_relation now.


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 08 April 2014 - 11:56 AM

great.. happy to help you


glopezr

glopezr
  • profile picture
  • Member

Posted 12 June 2015 - 09:39 AM

Thanks to all, as set_relation don't work when you use a table from other DB, I used this code to make the dropdown.
 
$crud = new grocery_CRUD();
$crud->set_table('testtable');
$crud->set_subject('TEST');

$crud->columns('id_sup',...............................................);

$DB1 = $this->load->database('otherdb', TRUE);

$query=$DB1->query("SELECT id, CONCAT (id,' - ',name) as fullname FROM supertabla WHERE activo=1 ORDER BY name ASC;");

$arr_ids = array();

foreach ($query->result() as $row)
{
    $arr_ids[$row->id]=$row->fullname;
}

$crud->field_type('id_sup','dropdown', $arr_ids);