creating simple combo list box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jarow
Forum Commoner
Posts: 83
Joined: Tue Jan 28, 2003 2:58 am

creating simple combo list box

Post by jarow »

Very new to PHP and would like to create a combo box in which:

1. I have set an intial value (e.g. "mollusca") and
2. the user has the capability of adding additional values to the list.

any help would be greatly appreciated.

Thanks
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Please keep all questions relating to the same topic in the same thread. It means things don't get confused and those trying to help can see better how the problem is progressing.

I've locked the previous thread:
viewtopic.php?t=5994
and directed those viewing it to here if they want to help.

Mac
User avatar
puckeye
Forum Contributor
Posts: 105
Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:

Post by puckeye »

Hi Jarow,

What you want to do can be done in a few ways...

The first that I usually use is to query the database before printing the select box.

The query would search for all the values grouping them so you have a single row for each values.

Exemple:

Code: Select all

$query = "SELECT Categories FROM tbl_name GROUP BY Categories ORDER BY Categories asc";
$result = mysql_query($query) OR DIE (mysql_error()."<BR>".$query);
This will return a result set with all the categories ordered alphanumerically.

From there you can use the following piece of code to build your select box:

Code: Select all

?>
<SELECT NAME="Categories" SIZE="1">
<?
while ($row = mysql_fetch_array($result))
&#123;
?>
<OPTION VALUE="<?=$row&#1111;"Categories"];?>"><?=$row&#1111;"Categories"];?></OPTION>

<?
&#125;
?>
</SELECT>
Your category is not in the selection?<BR>
Enter it here: <INPUT TYPE="text" NAME="New_Category">
This will build you the select box and a textbox for new categories.

If you want to make an edit page where you'd have to insert a SELECTED keyword into a particular OPTION you simply need to add this piece of code

Code: Select all

if ($row&#1111;"Categories"] == $Old_Categories):
     $select = " SELECTED";
else:
     $select = "";
endif;
Then you write the OPTION like this:

Code: Select all

<OPTION VALUE="<?=$row&#1111;"Categories"];?>" <?=$select?>><?=$row&#1111;"Categories"];?></OPTION>

The second way I'd do this would be to create a txt file and update this file every time there's a new category.

Each line would hold one category. Then using the file() you retrieve an array of the categories and use the above code replacing the $row = ... by list($key, $value) = each ($Cat_txt)) where $Cat_txt would be the result of the file() function. Of course in the while loop you replace $row["Categories"] by $value...
jarow
Forum Commoner
Posts: 83
Joined: Tue Jan 28, 2003 2:58 am

what does new category refer to?

Post by jarow »

THanks Michel for your reply.

I have a question for you. First I have replaced your "Categories" with "clase" the field from my table. The INPUT NAME for the list box is also "clase". However, what I don´t understand is what to put for the INPUT NAME for the text box. In your code you put "New_Category". Is this new category a new field I need to add to the database?

The following is the code I am using:

<?php
$query = "SELECT clase FROM faeu GROUP BY
clase ORDER BY clase asc";
$result = mysql_query($query) OR DIE
(mysql_error()."<BR>".$query); ?>
<SELECT NAME="clase" SIZE="1">
<? while ($row = mysql_fetch_array($result))
{
?>
<OPTION VALUE="<?=$row["clase"];?>">
<?=$row["clase"];?>
</OPTION>
<?
}
?>
</SELECT>
our category is not in the selection?<BR>
Enter it here:
<INPUT NAME=???? TYPE="text">
?>

I have another question about the edit page but I will leave that for another time.

Many thanks

Jim
User avatar
puckeye
Forum Contributor
Posts: 105
Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:

Re: what does new category refer to?

Post by puckeye »

jarow wrote:THanks Michel for your reply.

I have a question for you. First I have replaced your "Categories" with "clase" the field from my table. The INPUT NAME for the list box is also "clase". However, what I don´t understand is what to put for the INPUT NAME for the text box. In your code you put "New_Category". Is this new category a new field I need to add to the database?
No need to create a new field.

In the page where you'll point your form you check to see if the user selected a "clase" from the select box if not you check for a value from the New_Category field... The text box will be used to "add" a new value to your select box.

So in the INSERT query you'll use the "clase" select box value OR the New_Category text box value which ever has a value. If none of those has a value you'll need to give out an error message and reprint the form...

jarow wrote: The following is the code I am using:

<?php
$query = "SELECT clase FROM faeu GROUP BY
clase ORDER BY clase asc";
$result = mysql_query($query) OR DIE
(mysql_error()."<BR>".$query); ?>
<SELECT NAME="clase" SIZE="1">
<? while ($row = mysql_fetch_array($result))
{
?>
<OPTION VALUE="<?=$row["clase"];?>">
<?=$row["clase"];?>
</OPTION>
<?
}
?>
</SELECT>
Your category is not in the selection?<BR>
Enter it here:
<INPUT NAME=???? TYPE="text">
?>

I have another question about the edit page but I will leave that for another time.

Many thanks

Jim
Glad to be of help...
jarow
Forum Commoner
Posts: 83
Joined: Tue Jan 28, 2003 2:58 am

values from one field to another

Post by jarow »

Michel,

thanks again for your help. I´m afraid ny knowledge is so basic that I didn´t understand much about your insert query, etc.

This is the basic problem. When I add an item in the textbox that item is entered in the database ("clase" field). Upon refreshing that new item appears in my select box. However, if I select an item from the select box it does not generate a message to the database. In other words, what I am missing is code saying something like if the TEXTBOX is empty, the "clase" field should gets its value from the selected item in the SELECT BOX. And I am not sure how to do that.

Any ideas?

Many thanks

Jim
User avatar
puckeye
Forum Contributor
Posts: 105
Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:

Re: values from one field to another

Post by puckeye »

jarow wrote: This is the basic problem. When I add an item in the textbox that item is entered in the database ("clase" field). Upon refreshing that new item appears in my select box. However, if I select an item from the select box it does not generate a message to the database. In other words, what I am missing is code saying something like if the TEXTBOX is empty, the "clase" field should gets its value from the selected item in the SELECT BOX. And I am not sure how to do that.
This is quite simple.

First check if your select box "clase" is empty:

Code: Select all

<?php
if (empty($clase))
?>
if clase is empty (meaning no value was selected) you check to see if New_Category (I now call i new_clase) is empty.

Code: Select all

<?php
if (empty($clase))
{
    if (empty($new_clase))
    {
        print "ERROR";    //No clase selected and no new_clase provided
    }
    else
    {
        $clase = $new_clase;   //  We'll use new_clase to insert into the db
    }
}
?>
You end up by inserting your form result as usual... If the user didn't select a value in the clase select box then the server checks if the user filled a value in the new_clase (Remeber I renamed New_Category to better fit your code) and assigns that value to the $clase variable. If no value was provided for new_clase then you print an error...
Post Reply