⚠ 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

Problem with array and field_type "enum"



chocovo

chocovo
  • profile picture
  • Member

Posted 30 April 2015 - 16:23 PM

Hello,

 

I'm having a problem using field_type.

 

In the documentation it is said that we have to write the values like this:

$crud->field_type('status','enum',array('active','private','spam','deleted'));

 

I would like to get the values from the Data base then I wrote a function in model to obtain the values:

public function select_values($val=1) {
        $sql = "SELECT id, name FROM table_nav WHERE val='$val'";
        $query = $this->db->query($sql);
        $result = '';
            foreach($query->result() AS $row) {
                $result .= "'".$row->nav_name."',";
            }
            $result= substr($result, 0, -1);
        return $result;
    }

And in the controler I have this:

$arrayvalues = $this->Model_content->select_values();
$this->grocery_crud->field_type('nav_parent','enum',array($arrayvalues));

If I do an echo of $arrayvalues, I have this:

'active','private','spam','deleted'

 

If I change my code to...

$this->grocery_crud->field_type('nav_parent','enum',array('active','private','spam','deleted'));

... all work fine (image2) but if I change it to...

$this->grocery_crud->field_type('nav_parent','enum',array($arrayvalues));

... it doesn't work (image1).

 

 

Do you kwow why does it happen?

And how to solve it?

 

Thanks a lot.

Best


titu

titu
  • profile picture
  • Member

Posted 30 April 2015 - 16:45 PM

Well this is more of a PHP knowledge issue.

 

instead of concatinating to a string try adding the option to an array:

 foreach($query->result() AS $row) {
                $result[] = $row->nav_name;
            }

and delete this line:

 $result= substr($result, 0, -1);

and on the field type:

$this->grocery_crud->field_type('nav_parent','enum',$arrayvalues);

chocovo

chocovo
  • profile picture
  • Member

Posted 30 April 2015 - 21:19 PM

Hello,

 

Thank you for the help.

 

I am new in PHP and have a lot to learn and improve yet.

Litle by litle. ;)

 

Best