onibaku
onibaku

Reputation: 35

Autopopulating a dropdown menu by inputing numbes into a field using php

I'm new to php and html in general, so please forgive me for any stupid utterances. I'm trying to auto populate a drop down menu after entering a number into a blank field.

I looked around on google, but I couldn't find much on auto population for PHP only submitting a form. Right now I have an SQL query. When I want to output the query result it can displayed as the the key or the value of the array:

(in my view)

<?php

                      foreach($data as $key)
                      {
                         print_r($key['name']); 
                         echo '<br/>';
             print_r($key['id']);
             echo <br/>;


                      } ?>

I want to essentially say if pick the 1st key the 1st value will be displayed. I'm assuming I need some sort of if statement possibly, not sure of what else.

Using FuelPHP for my backend

My Model

public static function get_results()
    {
        $result = DB::query('select substring(cat.item_class_cd,1,1) as id, cat.class_name as name
        from MASTER_DB.MS_CATEGORY cat
        where length(cat.item_class_cd) = 1')->execute();
        // doesn't work

        return $result;
        //return ['id' => 1, 'name' => 'test']
    }

My Controller

public function action_index()
    {
        $model = new Model_Shelf();
        $data = $model->get_results();
        // create the layout view
        $view = View::forge('shelf/index');

        // assign global variables so all views have access to them
        /* $view->data = $result; */


        //assign views as variables, lazy rendering
        $view->head = View::forge('common/head');
        $view->header = View::forge('common/header');
        $view->content = View::forge('common/content', array('data'=>$data));
        $view->footer = View::forge('common/footer');

        // return the view object to the Request
        //return $view;
        return Response::forge($view);
    }

If you need any clarification let me know. I hope I was clear enough ><

Edit: Clarification

I'm currently looking at https://fuelphp.com/docs/classes/input.html but I'm confused. How exactly are you supposed to use post($index = null, $default = null) ? Is this supposed to go in my view?

<label for="category">Category</label>
<form action="content.php" method="post">
<input type="text" name="category" <?php Input::post($data['id']) ?>><br/>

data['id'] is the id number for the category and data['name'] is supposed to be the name of the category.

<div class=" form-group">
<select class="form-control form-control-sm">
<?php
Input::get(data['id']);
foreach($data as $option){
<option>$option['name']</option>
}
?>
</select>

My code might be a bit confusing. What I'm trying to do is that if 0 though 6 is selected it will populate the drop down with the category name for the selected category ID number.

Upvotes: 0

Views: 94

Answers (2)

Franklyn Kleiton
Franklyn Kleiton

Reputation: 51

Maybe I didn't understand the problem very well, but i think that if you alredy got all the data that you using the tip that LancelHunt show above, you only need print the results with a foreach inside the dropdown menu, where the echoing would be done inside the tag that handles dropdown items.

Upvotes: 0

LanceHunter
LanceHunter

Reputation: 24

If you need search in your database - use LIKE condition. For example: SELECT * FROM some_table WHERE your_field LIKE '%2%' (where 2 - number you try to find). % needs to make condition: any number before 2 and any number after. So it can find for example: 132, 231 etc.

Upvotes: 1

Related Questions