0

I have a database table named Categories. I want to create a form dropdown containing all the categories in the db. My solution is to create an associative array and add it in the second parameter of the form_dropdown() function. My result is an unwanted multidimensional array.

Model:

function list_categories()
    {
        $user_id = $this->tank_auth->get_user_id();

        $this->db->where('user_id', $user_id);
        $this->db->order_by("date_created", "desc");
        $query = $this->db->get('categories');
        return $query;      
    }

View:

//create array
$categories = array();

if ($query->num_rows() > 0) 
{
     $categories = $query->result_array();
}

//create dropdown
echo form_dropdown('category', $categories, $date_created); //selected value based date created

The code gives a multidimensional array

Array ( 
[0] => Array ( [cat_id] => 27 [user_id] => 3 [cat_title] => Some Title [status] => 0 [date_created] => 2012-06-22 18:48:14 ) 

[1] => Array ( [cat_id] => 24 [user_id] => 3 [cat_title] => Title [status] => 0 [date_created] => 2012-06-20 19:37:47 ) 

[2] => Array ( [cat_id] => 23 [user_id] => 3 [cat_title] => Another Title [status] => 0 [date_created] => 2012-06-20 18:25:45 ) 

etc...

How can I replace the result above with an Associative Array where the ID key is the category id and value the category title?

Example:

$categories = array(
    "23" => "some title",
    "14" => "another title",
);

4 Answers 4

5

SOLUTION WITH AN EXAMPLE:

$categories=array(
'0'=>array("cat_id"=>"27","cat_title"=>"some title"),
'1'=>array("cat_id"=>"24","cat_title"=>"Title"),
'2'=>array("cat_id"=>"23","cat_title"=>"Another Title"),
);

   foreach($categories as $catID=>$categoriesData){
        $finalArray[$categoriesData["cat_id"]]=$categoriesData;
   }

print_r($finalArray);


/*
OUTPUT

Array
(
    [27] => Array
        (
            [cat_id] => 27
            [cat_title] => some title
        )

    [24] => Array
        (
            [cat_id] => 24
            [cat_title] => Title
        )

    [23] => Array
        (
            [cat_id] => 23
            [cat_title] => Another Title
        )

)
*/
Sign up to request clarification or add additional context in comments.

Comments

1

Do something like:

foreach($categories as $category){
    $category_array[$category['cat_id']] = $category['cat_title'];
}

$categories = $category_array;

This will assign the category ID as the array key, and the title as the value, for use in the dropdown.

Comments

0

You can use _list() function in the html_helper to make nested list from multi-dimensional arrays

Comments

0

Use this simple method,

$query = $this->db->get_where('table', array('table_id' => $id));

        $queryArr = $query->result();        

        foreach ($queryArr[0] as $key=>$val)
        {
            $row[$key]=$val;
        }

print_r($row); //this will give associative array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.