Recover string in database in a dynamic form_dropdown

Asked

Viewed 526 times

3

I am trying to retrieve from the database a string in a field dropdown of a form on a page of update, my view goes below.

$option = array(NULL => 'Selecione uma categoria');

foreach ($categories_list->result() as $category):

$option[$category->categoryID] = $category->categoryTitle;

endforeach;

echo form_dropdown('categoryname', $option, set_value('categoryname', $post->categoryTitle));

However, the dropdown does not display the database string. Could someone help me?

2 answers

4

From what I see in the code of your question, the only thing incorrect is the use of set_value(), if you remove it should work as expected.

Note: the set_value() serves to set the value of a regular input or text box. Does not work with check boxes.

You can learn more at Codeigniter: User’s Guide.

It’s almost the end of the page.


In your controller you’ll have something like:

$categoriesList = $this->db->get('categories')->result();

Then you can generate your own select with the help of helper form_dropdown:

$options = array(null => "Selecione uma categoria");

foreach ($categoriesList as $cat) {
    $options[$cat->categoryID]=$cat->categoryTitle;
}

echo form_dropdown('categoryname', $options, $post->categoryTitle);
  • Hello Zuul, it really didn’t work. There is no way to display dorpdown with the saved option in the bank.

  • @Downbeat ?? It has to work, when you query something in the database you get one array, the foreach reads every entry of array and build another array in the way that the helper form_dropdown hope to receive. Not to be working, the only thing I can think of is that what you get from the comic book is not what you expected...

  • I’m sorry, maybe I’m having a hard time formulating the question correctly. I need to display the option selected by the user and saved in the database on a page that must return all fields of the form to perform a possible update of the data. With the other fields I did so and it worked:echo form_input(array('name' => 'postname'), set_value('postname', ucwords($post->postTitle)));

1


I discovered the problem that was located on this line:

$option[$category->categoryID] = $category->categoryTitle;

Which I corrected by swapping for:

$option[$category->categoryTitle] = $category->categoryTitle;

And it’s already working with the code:

echo form_dropdown('categoryname', $option, $post->categoryTitle); 

In the selection box did not have the Ids as value of each option but the text that is equal to the label displayed to the user.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.