Welcome Guest!

If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:

  • Receive Fast Advice
  • Learn Programming Languages
  • Get Professional Website Reviews
  • Quick Troubleshooting Assistance

> Array Help

This is a discussion on Array Help, within the PHP section. This forum and the thread "Array Help" are both part of the Frameworks category.

 
Reply to this topicStart new topic
> Array Help, for codeigniter
Antti
post Sep 6 2008, 10:50 AM
Post #1


Rapid Squeezer
Group Icon

Posts: 308
Joined: 15-February 08
From: Finland


I'm creating a form select with codeigniter's form_dropdown function and I need to dynamically create the options array for that function. So what I need is this:
CODE
$array = array("key1" => "value1", "key2" => "value2"...

I'm getting the keys and values from database so how do I do that? What I've done before is something like this
CODE
$query = $this->db->query($sql);
        
        if ($query->num_rows() > 0)
        {
            foreach ($query->result() as $row)
            {
               $data["news"][] = array(
                   "title" => $row->title,
                   "content" => word_limiter($row->content,10),
                   "created" => $row->created
               );
            }
        }

but if I now give $data["news"] to the form_dropdown, all it will give me back is a selectbox with values of "Array" since my array is an array of arrays.


--------------------
Go to the top of the page
 
+Quote Post
Jason
post Sep 6 2008, 11:31 AM
Post #2


Master of the Universe
Group Icon

Posts: 1,298
Joined: 15-February 08
From: London, England


Where are you doing this? In your model, view or controller?

I do the following.

Controller - Use model to query db -

$data['options'] = $this->model->getOptions();

I then call the view. Then perform what you have in the view. This avoids the problem of an array in an array.

CODE
if ($options->num_rows() > 0)
        {
            foreach ($options->result() as $row)
            {
               // Whatever the form code is here.
            }
        }


--------------------
Go to the top of the page
 
+Quote Post
Antti
post Sep 7 2008, 05:08 AM
Post #3


Rapid Squeezer
Group Icon

Posts: 308
Joined: 15-February 08
From: Finland


I do this in controller but did I understood you correctly that you do it like this
CODE
<select name="foobar">
<?php
if ($options->num_rows() > 0)
        {
            foreach ($options->result() as $row)
            {
               ?><option value="<?php echo $row->key1; ?>"><?php echo $row->value1; ?></option><?php
            }
        }
?>
</select>


I know I could do it like this but somehow doesn't sound so good idea to me.


--------------------
Go to the top of the page
 
+Quote Post
Jason
post Sep 7 2008, 06:42 AM
Post #4


Master of the Universe
Group Icon

Posts: 1,298
Joined: 15-February 08
From: London, England


I find my way is easier to work with in the future. In my way you only need to look in two places. The model to edit the database query and the view to edit what is being displayed. If there is no data manipulation then I see no reason why the controller needs to put the data in an array. It creates an unnecessary extra step.

If you want to use CI's form functions... many of which don't give you a great deal of benefit. Then you need to provide a bit more clarity..

CODE
foreach ($query->result() as $row)
            {
               $data["news"][] = array(
                   "title" => $row->title,
                   "content" => word_limiter($row->content,10),
                   "created" => $row->created
               );
            }


Why have you got 3 fields here?

Surely it should be.
CODE
foreach ($query->result() as $row)
            {
               $data["news"][$row->title] = ucwords($row->title);
            }


Then when you pass the $news array to the dropdown function it will generate a drop down.


--------------------
Go to the top of the page
 
+Quote Post
Antti
post Sep 7 2008, 06:53 AM
Post #5


Rapid Squeezer
Group Icon

Posts: 308
Joined: 15-February 08
From: Finland


I solved the problem with your way and it now works perfectly although I have another codeigniter problem with forms usage but more on that in another thread.


--------------------
Go to the top of the page
 
+Quote Post
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   0 thesealportalteam 188 29th February 2008 - 09:40 AM
Last post by: thesealportalteam
No New Posts   1 Jason 94 20th March 2008 - 09:05 AM
Last post by: Rakuli
No New Posts   2 Jason 247 7th April 2008 - 05:08 AM
Last post by: c010depunkk