Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

DB populated drop-down lists in form

edited September 2013 in Apps

In a newly created by CRUD generator app I would like to show some drop-down list populated with data from other tables. For example, in the app articles, I would like to allow the user to choose type, category and units of measure. In the add.php handler I recover arrays needed to populate the drop-down list by using /lib/Functions.php, but I do not know how to pass them to the view, and how to loop through them. Thanks for help.

Comments

  • Johnny posted this for me when I had a similar question: https://gist.github.com/jbroadway/6286438

  • Mi handler looks like

    $this->require_acl ('admin', 'articles');
    
    $page->layout = 'admin';
    $page->title = __ ('Add Article');
    
    require_once ('apps/arttypes/lib/Functions.php');
    
    $form = new Form ('post', $this);
    
    $form->arttypes = arttype_list_all ();
    $form->artcats = artcat_list_all ();
    $form->ums= um_list_all ();
    
    echo $form->handle (function ($form) {
        // Create and save a new article 
        $article = new articles\Article (array (
            'id' => $_POST['id'], 
            'title' => $_POST['title'], 
            'description' => $_POST['description'], 
            'arttype' => $_POST['arttype'], 
            'artcat' => $_POST['artcat'], 
            'images' => $_POST['images'], 
            ...
            'um' => $_POST['um'], 
            'price' => $_POST['price'], 
            'supplier' => $_POST['supplier'], 
            'eta' => $_POST['eta'], 
            'notes' => $_POST['notes'] 
        ));
        $article->put ();
    
        if ($article->error) {
            // Failed to save
            $form->controller->add_notification (__ ('Unable to save article.'));
            return false;
        }
    
        // Save a version of the article 
        Versions::add ($article);
    
        // Notify the user and redirect on success
        $form->controller->add_notification (__ ('Article added.'));
        $form->controller->redirect ('/articles/admin');
    });
    

    I have to pass three arrays to the view and I have no "echo $tpl->render". Thanks.

  • I think you have to pass them to $form->data, ie $form->data['arttypes'] = arttype_list_all ();

  • Thanks. Using $form->data['arttypes'] = arttype_list_all (); I can access the array from the view but with {% foreach arttypes as val, label %} I get ""Invalid argument supplied for foreach()".

    With {% foreach arttypes %} or {% foreach arttypes as key, value %} I have tried in various ways, but in the best case I get an empty drop-down list (with the right number of elements).

  • Some progress.

    git example return data like

    Array ( [1ST] => First [2ND] => Second [3RD] => Third ) 
    

    and this way I can build select with:

    <select name="arttype">
        {% foreach arttypes as val, label %}
            <option value="{{val}}"{% if arttypeselected == $data->val %} selected{% end %}>{{label}}
        {% end %}
    </select>
    

    I store the current selected with:

        $form->data[arttypeselected] =  $_POST['arttype'];
    

    Is that the right way?

    But how can do it if returned data are like below?

    Array ( [0] => stdClass Object ( [key] => 1ST [value] => First ) [1] => stdClass Object ( [key] => 2ND [value] => Second ) [2] => stdClass Object ( [key] => 3RD [value] => Third ) ) 
    

    Thanks for help.

  • Re: your first comment (Invalid argument supplied for foreach()): You can wrap the foreach loop in {% if arttypes %}{% endif %}.

    Re: your second comment: I'm still a little confused about when the Model class returns objects and when arrays. Could you do something like this?

    $first_choice = $returned_data[0]->value
    
  • First: then I had only to verify that arttypes was not empty?

    Second: please forget it, I am confused me too (maybe because I'm working 16/24).

    Thanks.

  • First: yes, I think so. Try it!

Sign In or Register to comment.