For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > April 2004 > Re: [PHP-DB] Drop-down box in php









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PHP-DB] Drop-down box in php
Torsten Lange

2004-04-20, 1:31 pm

I use html forms with select field size 1 and the option values and/or
what is supposed to appear in the box. This you populate by your DB
query, maybe using an array and a loop (from 0 to ..).

Torsten

andy amol schrieb:

>hi,
> I would like to know how to create and populate drop down boxes in php.
>I want the value to be populated from database.
>What I am try to do is to provide the forign key value as combo box option, so that I do not have to check for referential integrity.
>
>Also if you can help me with a date validation program I will be grateful.
>
>Thanks in advance.
>
>
>
>
>
>---------------------------------
>Do you Yahoo!?
>Yahoo! Photos: High-quality 4x6 digital prints for 25¢
>
>
>

Andras Got

2004-04-20, 1:32 pm

You mean <select></select> html dropdown-s?

print '<select name="sg">';
$r = mysql_query("SELECT id, field1, fieldn FROM table WHERE fieldn = 'sg'");
while($RESULT = mysql_fetch_array($)) {
print '<option value="'.$RESULT['id'].'">'.$RESULT['fieldn']'.</option>';
}
print '</select>';

Validating: just check the $_POST['sg'] value, against is_numeric(), then you can do a SELECT, to
check whether the id exists.

You can find some more examples, on php net manual pages

andrej


andy amol wrote:

> hi,
> I would like to know how to create and populate drop down boxes in php.
> I want the value to be populated from database.
> What I am try to do is to provide the forign key value as combo box option, so that I do not have to check for referential integrity.
>
> Also if you can help me with a date validation program I will be grateful.
>
> Thanks in advance.
>
>
>
>
>
> ---------------------------------
> Do you Yahoo!?
> Yahoo! Photos: High-quality 4x6 digital prints for 25¢

W. D.

2004-04-20, 1:32 pm

At 10:53 4/20/2004, andy amol, wrote:
>hi,
>=20
>Also if you can help me with a date validation program I will be grateful.


You could use a Regular Expression:
http://tinyurl.com/yr6up
http://www.Google.com/search?q=3Dre...+site%3Aphp.net

Start Here to Find It Fast!=99 ->=
http://www.US-Webmasters.com/best-start-page/
$8.77 Domain Names -> http://domains.us-webmasters.com/
John W. Holmes

2004-04-20, 1:32 pm

From: "andy amol" <andy_amol_2003@yahoo.com>

> I would like to know how to create and populate drop down boxes in php.
> I want the value to be populated from database.
> What I am try to do is to provide the forign key value as combo box

option, so that I do not have to check for referential integrity.

You still have to check. Just because you provide a discreet number of
options in a <select> box doesn't mean that's really all the user can choose
from. There are many ways to manipulate the data.

That being said, just create a loop as you draw items from your database.

<?php
echo '<select name="something" size="1">';
$sql = "SELECT name FROM products WHERE ...";
$result = query($sql);
while($row = fetch_assoc($result))
{ echo "<option value=\"{$row['name']}\">{$row['name']}</option>\n"; }
echo "</select>";

I don't know what database you're using, so query() and fetch_assoc() are
generic.

---John Holmes...
Robert Sossomon

2004-04-20, 2:31 pm

If it is a MySWL database, the following code works great

<?
$sql =3D "select choice, description from views";
$result =3D mysql_query($sql) or die(mysql_error());

$viewing =3D "";
$viewing .=3D "<select name=3D\"view_code\">\n";

while ($view_list =3D mysql_fetch_array($result))
{
$view_code =3D $view_list[choice];
$view_desc =3D stripslashes($view_list[description]);

$viewing .=3D "<option value=3D\"$view_code\">$view_desc</option>\n";
}

$viewing .=3D "</select>";
?>

Of course you'll have to change it to fit your needs, but I just have a
while loop that gets the information and then dumps each line it gets
out to a temp variable. On the pages that include this one is where the
displaying comes out.

HTH,
Robert

-----Original Message-----
From: andy amol [mailto:andy_amol_2003@yahoo.com]=20
Sent: Tuesday, April 20, 2004 11:53 AM
To: David Robley; php-db@lists.php.net
Subject: [PHP-DB] Drop-down box in php


hi,
I would like to know how to create and populate drop down boxes in
php. I want the value to be populated from database. What I am try to do
is to provide the forign key value as combo box option, so that I do not
have to check for referential integrity.
=20
Also if you can help me with a date validation program I will be
grateful.
=20
Thanks in advance.




=09
---------------------------------
Do you Yahoo!?
Yahoo! Photos: High-quality 4x6 digital prints for 25=A2
Andy Amol

2004-04-21, 1:30 am

hi,
I am using the following code, but it is not populating my script. If you can help I would be grateful.
I am using mysql as database.
<?
$sql = "SELECT course_id FROM course";
$sql_result = mysql_query($sql)
or die("Couldn't execute query.");
while ($row = mysql_fetch_array($sql_result)) {
$type = $row["course_id"];
$typedesc =$row["dept_id"];
$option_block .= "<OPTION value=\"$type\">$typedesc</OPTION>";
}
?>
<SELECT name="selecttype" id="selecttype">
<? echo "$option_block"; ?>
</SELECT>

thanks.

"John W. Holmes" <holmes072000@charter.net> wrote:
From: "andy amol"

> I would like to know how to create and populate drop down boxes in php.
> I want the value to be populated from database.
> What I am try to do is to provide the forign key value as combo box

option, so that I do not have to check for referential integrity.

You still have to check. Just because you provide a discreet number of
options in a box doesn't mean that's really all the user can choosefrom. There are many ways to manipulate the data.That being said, just create a loop as you draw items from your database.echo '';
$sql = "SELECT name FROM products WHERE ...";
$result = query($sql);
while($row = fetch_assoc($result))
{ echo "{$row['name']}\n"; }
echo "";

I don't know what database you're using, so query() and fetch_assoc() are
generic.

---John Holmes...

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


---------------------------------
Do you Yahoo!?
Yahoo! Photos: High-quality 4x6 digital prints for 25¢
John W. Holmes

2004-04-21, 3:32 pm

From: andy amol

> I am using the following code, but it is not populating my script. If

you can help I would be grateful.
> I am using mysql as database.
> <?
> $sql = "SELECT course_id FROM course";
> $sql_result = mysql_query($sql)
> or die("Couldn't execute query.");
> while ($row = mysql_fetch_array($sql_result)) {
> $type = $row["course_id"];
> $typedesc =$row["dept_id"];
> $option_block .= "<OPTION value=\"$type\">$typedesc</OPTION>";


You're using ["dept_id"], but not selecting that column in your query.

---John Holmes...
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com