PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 300,404 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,617 people online right now. Registration is fast and FREE... Join Now!




phpcake(populate dropdownlist from table)

 

phpcake(populate dropdownlist from table)

pretender19

29 Jun, 2009 - 06:18 PM
Post #1

New D.I.C Head
*

Joined: 23 Dec, 2008
Posts: 36


My Contributions
the code in my views folder views/posts/index.thtml
CODE
<?php


//echo var $tags = array('selectstart');


foreach ($posts as $post):
echo $form->select('chuvaness', array('id'=>$post['Post']['id']),null,null,false);
endforeach;

//echo var $tags = array('selectend');
?>


the code in my post controller
CODE

<?php


class PostsController extends AppController
{
    var $name = 'Posts';

    function index()
    {
        $this->set('posts', $this->Post->find('all'));

    }





}


?>



my code displays many dropdown depending on how many the rows my table have,,
currently it have 3 ,so it displays 3 dropwdowns, each dropdown option value = each row..

example,,

rows are , 1.anna ,, 2.mary ,,3.john
dropdowns would be,
dropdown1 = anna
dropdown2= mary
dropdown3=john

which is incorrect because what im trying to do is to display one dropdown having 3 options value,

please,,im stuck here,, can anyone help me here,, thank you in advance...

User is offlineProfile CardPM
+Quote Post


noorahmad

RE: Phpcake(populate Dropdownlist From Table)

29 Jun, 2009 - 07:46 PM
Post #2

Webmaster
Group Icon

Joined: 12 Mar, 2009
Posts: 2,018



Thanked: 125 times
Dream Kudos: 1350
My Contributions
Are you talking about this
php
<select name="posts" id="posts">
<?php
while($fetch = mysql_fetch_assoc($query))
{
?>
<option value="<?php echo $fetch['col1']?>"><?php echo $fetch['col2']?></option>
<?php
}
?>
</select>


This post has been edited by noorahmad: 29 Jun, 2009 - 08:02 PM
User is online!Profile CardPM
+Quote Post

pretender19

RE: Phpcake(populate Dropdownlist From Table)

29 Jun, 2009 - 08:14 PM
Post #3

New D.I.C Head
*

Joined: 23 Dec, 2008
Posts: 36


My Contributions
QUOTE(noorahmad @ 29 Jun, 2009 - 07:46 PM) *

Are you talking about this
php
<select name="posts" id="posts">
<?php
while($fetch = mysql_fetch_assoc($query))
{
?>
<option value="<?php echo $fetch['col1']?>"><?php echo $fetch['col2']?></option>
<?php
}
?>
</select>




thank you,, but what i need is for cakephp,
User is offlineProfile CardPM
+Quote Post

CTphpnwb

RE: Phpcake(populate Dropdownlist From Table)

29 Jun, 2009 - 09:01 PM
Post #4

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,065



Thanked: 151 times
Dream Kudos: 100
Expert In: PHP

My Contributions
I don't have Cake, but according to this:
http://book.cakephp.org/view/204/Form-Elem...pecific-Methods
it looks to me like your loop is creating the options. The expected way to do this is to use:
CODE
foreach($posts as $post)
{
  $myoptions[] = $post;
}
echo $form->select('chuvaness', $myoptions, null, null, false);


This post has been edited by CTphpnwb: 29 Jun, 2009 - 09:02 PM
User is offlineProfile CardPM
+Quote Post

pretender19

RE: Phpcake(populate Dropdownlist From Table)

29 Jun, 2009 - 10:06 PM
Post #5

New D.I.C Head
*

Joined: 23 Dec, 2008
Posts: 36


My Contributions
QUOTE(CTphpnwb @ 29 Jun, 2009 - 09:01 PM) *

I don't have Cake, but according to this:
http://book.cakephp.org/view/204/Form-Elem...pecific-Methods
it looks to me like your loop is creating the options. The expected way to do this is to use:
CODE
foreach($posts as $post)
{
  $myoptions[] = $post;
}
echo $form->select('chuvaness', $myoptions, null, null, false);




this is a great help for me,, thank you,,,, the code above display the title,, what if i want to have the value of the option to be the "id" of each title..
User is offlineProfile CardPM
+Quote Post

CTphpnwb

RE: Phpcake(populate Dropdownlist From Table)

30 Jun, 2009 - 07:01 AM
Post #6

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,065



Thanked: 151 times
Dream Kudos: 100
Expert In: PHP

My Contributions
Then you'd have to put those in the $myoptions array.
User is offlineProfile CardPM
+Quote Post

pretender19

RE: Phpcake(populate Dropdownlist From Table)

30 Jun, 2009 - 08:11 PM
Post #7

New D.I.C Head
*

Joined: 23 Dec, 2008
Posts: 36


My Contributions
QUOTE(CTphpnwb @ 30 Jun, 2009 - 07:01 AM) *

Then you'd have to put those in the $myoptions array.




here's what i want to do
example.

<option value "id">title</option>

User is offlineProfile CardPM
+Quote Post

noorahmad

RE: Phpcake(populate Dropdownlist From Table)

30 Jun, 2009 - 10:02 PM
Post #8

Webmaster
Group Icon

Joined: 12 Mar, 2009
Posts: 2,018



Thanked: 125 times
Dream Kudos: 1350
My Contributions
you are missing equal
<option value="id">title</option>
User is online!Profile CardPM
+Quote Post

pretender19

RE: Phpcake(populate Dropdownlist From Table)

28 Jul, 2009 - 09:30 PM
Post #9

New D.I.C Head
*

Joined: 23 Dec, 2008
Posts: 36


My Contributions
QUOTE(noorahmad @ 30 Jun, 2009 - 10:02 PM) *

you are missing equal
<option value="id">title</option>



oh,, that's not the problem. my problem is what will be the value of the "id".
User is offlineProfile CardPM
+Quote Post

CTphpnwb

RE: Phpcake(populate Dropdownlist From Table)

29 Jul, 2009 - 04:26 AM
Post #10

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,065



Thanked: 151 times
Dream Kudos: 100
Expert In: PHP

My Contributions
From the link I provided, looking at:
QUOTE
8.3.5.17 select
EditView just this sectionComments (2)History
select(string $fieldName, array $options, mixed $selected, array $attributes, boolean $showEmpty)

Creates a select element, populated with the items in $options, with the option specified by $selected shown as selected by default. Set $showEmpty to false if you do not want an empty select option to be displayed.

Plain Text View
<?php$options=array('M'=>'Male','F'=>'Female');echo $form->select('gender',$options)?>
Will output:

<select name="data[User][gender]" id="UserGender">
<option value=""></option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>


tells me that something like:
CODE
$options = array("id_value"=>"title");
echo $form->select('chuvaness', $options);

should work.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 10:34PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month