Welcome Guest, Not a member yet? Register   Sign In
Load drop down box from database
#1

[eluser]maddtechwf[/eluser]
I have written some code to read item's from my db and then use them to populate my drop down box on my view. When I add the code and try it, my page just shows to be blank. I don't get any errors on the page what so every. Can anyone help me with this.

Model
Code:
//Get Gategories
        function get_categories($category_type)   //the $category_type is a string value passed from controller
        {
            $return = array();
            
            //DB Connection
            $this->db->select('ID,Name');
            $this->db->from('Category');
            $this->db->where('Type='.$category_type);
            $category_items = $this->db-get();
            
            if ( $cagetory_items > 0)
            {
                foreach ( $category_items->results() as $item)
                {
                    array_push($return, $item);
                }
            }
            return $return;
        }

Controller
Code:
$data['example_Categories'] = $this->map_model->get_categories("test123");

View
Code:
<select>
                    &lt;?php
                        $ucat = $map['example_Categories'];
                        
                        foreach ( $ucat as $item )
                        {
                            echo "<option name=".$item-&gt;Name." value=".$item-&gt;ID.">".$item->Name."</option>";
                        }
                    ?&gt;
                </select>
#2

[eluser]TheFuzzy0ne[/eluser]
Have you checked the source code out for the page? There may be an error hidden within.

Check that you don't have any closing PHP tags at the end of PHP files. It's safe to omit them, and recommended, because trailing whitespace can cause problems without your output (i.e a white page).

Also, a much easier way of generating a dropdown menu is to load the form_helper and then substitute the view code above with:
Code:
echo form_dropdown('item_name', $options, )

The code you have has several mark-up errors, which may also explain your problem.
#3

[eluser]maddtechwf[/eluser]
Thanks for the markup. I will change my view code in just a minute. I went through and commented stuff out one by one to see where I was having my issue and it's actually in my controller when I call my model. What have I done wrong there?

Controller
Code:
$data['example_Categories'] = $this->map_model->get_categories("example");

Model
Code:
//Get Gategories
        function get_categories($category_type)
        {
            $return = array();
            
            //DB Connection
            $this->db->select('ID,Name');
            $this->db->from('Category');
            $this->db->where('Type='.$category_type);
            $category_items = $this->db-get();
            
            if ( $cagetory_items > 0)
            {
                foreach ( $category_items->results() as $item)
                {
                    array_push($return, $item);
                }
            }
            return $return;
        }
#4

[eluser]maddtechwf[/eluser]
Okay, so I've got my model and controller working great now. My only issue is my view. This is the error message I get on the screen. "Severity: 4096, Message: Object of class stdClass could not be converted to string, Filename: helpers/form_helper.php, Line Number: 352".

Here is my revised code for my controller, view, and model

Model:
Code:
//Get Gategories
        function get_categories($cat_type)
        {
            $return = array();
            
            //DB Connection
            $this->db->select('ID,Name');
            $this->db->from('Category');
            $this->db->where('Type', $cat_type);
            $category_items = $this->db->get();
            
            if ( $category_items->num_rows() > 0)
            {
                foreach ( $category_items->result() as $item )
                {
                    array_push($return, $item);
                }
            }
            return $return;
        }

Controller
Code:
$data['Univ_Categories'] = $this->map_model->get_categories('University');
        $data['City_Categories'] = $this->map_model->get_categories('City');

View
Code:
&lt;?php echo form_dropdown('univ-cat', $MSU_Categories); ?&gt;
#5

[eluser]TheFuzzy0ne[/eluser]
You're pushing objects into the array, and the form helper expects strings.

You probably want to do something like this:
Code:
if ( $category_items->num_rows() > 0)
{
    foreach ( $category_items->result() as $item )
    {
        $return[$item->ID] = $item->Name;
    }
}

Hope this helps.
#6

[eluser]maddtechwf[/eluser]
Awesome thanks for that. That fixed everything.




Theme © iAndrew 2016 - Forum software by © MyBB