Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] Create dropdown menu from comma separated list of numbers


[SOLVED] Create dropdown menu from comma separated list of numbers

Reply
  #1 (permalink)  
Old 03-02-07, 03:14 PM
maya77 maya77 is offline
Newbie Coder
 
Join Date: Nov 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Create dropdown menu from comma separated list of numbers

Hello people,

I'm not too good with the PHP and need some code which will automatically create dropdown menu from comma separated list of numbers.

For example if I have this:
1,2,3,4,5,6,7,8,9
code needs to build menu with listed numbers as options. It will look like this:
1
2
3
4
5
6
7
8
9


When I change numbers in comma separated list then menu needs to change automatically to new options.

So if I change it to:
25,30,35,40,45,50
menu options changes to:
25
30
35
40
45
50


I need to put it later to HTML document.


Thanks in advance.
Reply With Quote
  #2 (permalink)  
Old 03-02-07, 03:38 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
PHP Code:

<?php

$options 
'25,30,35,40,45,50';
$options explode(','$options);

?>

<form name="form" method="post" action="">
<select name="dropdown">
<?php

foreach ($options AS $option)
{
    
?>
        <option value="<?php echo $option?>"><?php echo $option?></option>
    <?php
}
?>
</select>
</form>
Reply With Quote
  #3 (permalink)  
Old 03-03-07, 05:11 AM
maya77 maya77 is offline
Newbie Coder
 
Join Date: Nov 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you very much. Works fine!
Reply With Quote
  #4 (permalink)  
Old 01-21-09, 05:54 PM
mike_jandreau mike_jandreau is offline
Newbie Coder
 
Join Date: Jun 2006
Location: Boston
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Stumbled across this when looking for something similar.

If I wanted to replace the list of numbers with a comma separated item from a database, how would I go about that?

I tried the following, and it doesn't work:
PHP Code:

<?php

$con 
mysql_connect("localhost","igivetest""");
if (!
$con)
  {
  die(
'Could not connect: ' mysql_error());
  }

mysql_select_db("my_db"$con);
mysql_query("SELECT reg_values_userfield1 from igive_config");
while (
$row mysql_fetch_array($result)){
$option_list $row['option_list'];
}

mysql_close($con);
?>
<?php

$options 
$option_list;
$options explode(','$options);

?>

<form name="form" method="post" action="">
<select name="dropdown">
<?php

foreach ($options AS $option)
{
    
?>
        <option value="<?php echo $option?>"><?php echo $option?></option>
    <?php
}
?>
</select>
</form>
In this, my list of separated values is "reg_values_userfield1", in the database under igive_config

Any help is appreciated.
Reply With Quote
  #5 (permalink)  
Old 01-21-09, 11:53 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
I am assuming there is only one record in the igive_config table
and the column reg_values_userfield1 contains a comma delimited string of numbers.
ie: 1,2,3,4,5,6,7,8,9

Also, the option element's value will default to the option's text if a value isn't specified.
So you don't need to specify the value as $option and the text as $option.

The reason your code didn't work is because you didn't supply a resource identifier ($result) for the query.
And you are fetching the comma delimited string from reg_values_userfield1 and then you try to get the value of $row['option_list'] which doesn't exist.

This code should do what you want.
PHP Code:

<form name="form" method="post" action="">
<select name="dropdown">
<?php
$con 
mysql_connect("localhost","igivetest""") or die('Could not connect: ' mysql_error());
mysql_select_db("my_db"$con);
$result mysql_query("SELECT reg_values_userfield1 from igive_config");
$row mysql_fetch_assoc($result);
mysql_free_result($result);
mysql_close($con);
$options explode(',',$row['reg_values_userfield1']);
foreach(
$options AS $option)
{
 echo 
"<option>$option</option>";
 }
?>
</select>
</form>
Instead of using
$row = mysql_fetch_assoc($result);
you could also use
$row = mysql_fetch_row($result);
and this code would work.
PHP Code:

<form name="form" method="post" action="">
<select name="dropdown">
<?php
$con 
mysql_connect("localhost","igivetest""") or die('Could not connect: ' mysql_error());
mysql_select_db("my_db"$con);
$result mysql_query("SELECT reg_values_userfield1 from igive_config");
$row mysql_fetch_row($result);
mysql_free_result($result);
mysql_close($con);
$options explode(',',$row[0]);
foreach(
$options AS $option)
{
 echo 
"<option>$option</option>";
 }
?>
</select>
</form>
__________________
Jerry Broughton

Last edited by job0107; 01-22-09 at 12:08 AM.
Reply With Quote
  #6 (permalink)  
Old 01-22-09, 07:11 AM
mike_jandreau mike_jandreau is offline
Newbie Coder
 
Join Date: Jun 2006
Location: Boston
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Jerry,

That didn't seem to work, though. Either examples of code you provided just resulted in an empty option area.

I'll try playing around with it to see if I can get it to work, thanks for pointing me in the right direction.
Reply With Quote
  #7 (permalink)  
Old 01-22-09, 08:44 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by mike_jandreau View Post
Thanks Jerry,

That didn't seem to work, though. Either examples of code you provided just resulted in an empty option area.

I'll try playing around with it to see if I can get it to work, thanks for pointing me in the right direction.
When I wrote the code, I created a table in my database called igive_config
and in that table I created one column called reg_values_userfield1.

And then I created one record in that table with this data in it "1,2,3,4,5,6,7,8,9".
The data is in there exactly as it is written without the quotes.

You do the same and it will work.

To change the values in the dropdown box, you just change the comma delimited string
in that one record.
ie: "10,11,12,13,14,15,16,17,18"

Do you understand?
__________________
Jerry Broughton
Reply With Quote
  #8 (permalink)  
Old 01-22-09, 08:46 AM
mike_jandreau mike_jandreau is offline
Newbie Coder
 
Join Date: Jun 2006
Location: Boston
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
I tried, I failed.

I apparently had the query wrong before, but I fixed that. If I run the query in the database, I get back the data from that field that I want (the comma separated data), but not for this dropdown code.

Here's what I tried:
PHP Code:

<form name="form" method="post" action="rolecall.php">

<select name="q">
<?php
$con 
mysql_connect("localhost","""") or die('Could not connect: ' mysql_error());
mysql_select_db("my_db"$con);
$result mysql_query("SELECT config_value from igive_config WHERE config_name='reg_values_userfield1'");
$row mysql_fetch_row($result);
mysql_free_result($result);
mysql_close($con);
$options explode(',',$row[config_value]);
foreach(
$options AS $option)
{
 echo 
"<option>$option</option>";
 }
?>
</select>
</form>
And all I get back is:
HTML Code:
<form name="form" method="post" action="rolecall.php">
<select name="q">
<option></option></select>
</form>
Seems like the $option bit isn't doing what it's supposed to. Any further help is appreciated.
Reply With Quote
  #9 (permalink)  
Old 01-22-09, 08:59 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by mike_jandreau View Post
I tried, I failed.

I apparently had the query wrong before, but I fixed that. If I run the query in the database, I get back the data from that field that I want (the comma separated data), but not for this dropdown code.

Here's what I tried:
PHP Code:

<form name="form" method="post" action="rolecall.php">
<select name="q">
<?php
$con 
mysql_connect("localhost","""") or die('Could not connect: ' mysql_error());
mysql_select_db("my_db"$con);
$result mysql_query("SELECT config_value from igive_config WHERE config_name='reg_values_userfield1'");
$row mysql_fetch_row($result);
mysql_free_result($result);
mysql_close($con);
$options explode(',',$row[config_value]);
foreach(
$options AS $option)
{
 echo 
"<option>$option</option>";
 }
?>
</select>
</form>
And all I get back is:
HTML Code:
<form name="form" method="post" action="rolecall.php">
<select name="q">
<option></option></select>
</form>
Seems like the $option bit isn't doing what it's supposed to. Any further help is appreciated.
Take a look at this line of code:
PHP Code:

$result mysql_query("SELECT config_value from igive_config WHERE config_name='reg_values_userfield1'"); 

This query says, select the config_value column from the igive_config table where the config_name column equals the value "reg_values_userfield1".

Obviously there is no value "reg_values_userfield1" in the config_name column.

Try again.
__________________
Jerry Broughton
Reply With Quote
  #10 (permalink)  
Old 01-22-09, 09:11 AM
mike_jandreau mike_jandreau is offline
Newbie Coder
 
Join Date: Jun 2006
Location: Boston
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by job0107 View Post
Obviously there is no value "reg_values_userfield1" in the config_name column.

Try again.
I thought that as, well. But if I run that same exact query in phpMyAdmin, I get the exact data I'm looking for. I'm confused why the php query doesn't pull that bit from the database.

PHP Code:

SELECT config_value

FROM igive_config
WHERE config_name 
'reg_values_userfield1'
LIMIT 0 30 
Gives me the attached image as a result, which is the data I want. Any suggestions?
Attached Images
File Type: jpg results.jpg (8.4 KB, 734 views)
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
comma separated email list problems.... Alith7 PHP 16 10-16-06 11:24 AM
Xml / Dom / Css Mark_SC.SE JavaScript 0 06-29-05 08:05 AM


All times are GMT -5. The time now is 05:57 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.