dynamic dropdown

Reply

Join Date: Nov 2007
Posts: 183
Reputation: lydia21 is an unknown quantity at this point 
Solved Threads: 5
lydia21 lydia21 is offline Offline
Junior Poster

dynamic dropdown

 
0
  #1
Dec 19th, 2007
hi

how to polute my drop down using the database content...........
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: dynamic dropdown

 
0
  #2
Dec 19th, 2007
polute ? What do you mean by that ?
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 36
Reputation: tirivamwe is an unknown quantity at this point 
Solved Threads: 2
tirivamwe's Avatar
tirivamwe tirivamwe is offline Offline
Light Poster

Re: dynamic dropdown

 
1
  #3
Dec 19th, 2007
try this code it works for me

  1.  
  2. <?php
  3. $host="localhost"; // Host name
  4. $username="root"; // Mysql username
  5. $password=""; // Mysql password
  6. $db_name="forum"; // Database name
  7. $tbl_name="test_mysql"; // Table name
  8.  
  9. // Connect to server and select databse.
  10. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  11. mysql_select_db("$db_name")or die("cannot select DB");
  12.  
  13. $sql="SELECT * FROM test_mysql";
  14. $result=mysql_query($sql);
  15.  
  16. ?>
  17.  
  18. <form name="form1" method="post" action="">
  19. <select name="Names" id="Names">
  20. <?php
  21.  
  22. while($rows=mysql_fetch_array($result, MYSQL_ASSOC))
  23.  
  24. {
  25.  
  26.  
  27.  
  28. echo"<option>". $rows['name']."</option>"; //to display names from database in field called name
  29.  
  30.  
  31. }
  32.  
  33. mysql_close();
  34. ?>
  35.  
  36.  
  37. </select>
  38. </form>
If you find this useful you can add to my reputation
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 183
Reputation: lydia21 is an unknown quantity at this point 
Solved Threads: 5
lydia21 lydia21 is offline Offline
Junior Poster

Re: dynamic dropdown

 
0
  #4
Dec 19th, 2007
sorry how to retrieve the table content and display in a dropdown
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 36
Reputation: tirivamwe is an unknown quantity at this point 
Solved Threads: 2
tirivamwe's Avatar
tirivamwe tirivamwe is offline Offline
Light Poster

Re: dynamic dropdown

 
0
  #5
Dec 19th, 2007
can you please be specific.

Do you want to put content from database into drop down or from a table. If from a table where is the content coming from
If you find this useful you can add to my reputation
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 183
Reputation: lydia21 is an unknown quantity at this point 
Solved Threads: 5
lydia21 lydia21 is offline Offline
Junior Poster

Re: dynamic dropdown

 
0
  #6
Dec 19th, 2007
thanks
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: dynamic dropdown

 
0
  #7
Dec 19th, 2007
You can do something like tirivamwe has mentioned. Or you can use a function so that you can reuse the code whenever you want.
  1. <?php
  2. $conn=mysql_connect("localhost","root");
  3. mysql_select_db("test");
  4. function dropdown($colname,$tablename){
  5. $query="select ".$colname." from ".$tablename;
  6. $result=mysql_query($query);
  7. $option="";
  8. while($row=mysql_fetch_array($result)){
  9. $option.="<option value=".$row[0].">".$row[0]."</option>";
  10. }
  11. return $option;
  12. }
  13. ?>
  14. <html>
  15. <body>
  16. <form>
  17. <select name='test'>
  18. <?php echo $options=dropdown("Column_name","Table_name");
  19. //the column name of table_name which you want to display in the options
  20. ?>
  21. </select>
  22. <select name='test1'>
  23. <?php echo $options=dropdown("Column_name1","Table_name2");
  24. ?>
  25. </select>
  26. </form>
  27. </body>
  28. </html>
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 183
Reputation: lydia21 is an unknown quantity at this point 
Solved Threads: 5
lydia21 lydia21 is offline Offline
Junior Poster

Re: dynamic dropdown

 
0
  #8
Dec 19th, 2007
hi
this works but how to get the dropwown value in the next page..
i tried with $id=$_REQUEST['id']; but this is not working
<form method="get" action="sort2.php">
<?php
$hostname = "localhost";
$username = "";
$password = "";
$dbid = "";
$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die("unable to connect");



$sql="SELECT id,name FROM personal";
$result=mysql_query($sql);

?>
<table>



<tr>
<td>Splendor Id</td><td><select name="Names">
<?php

while($rows=mysql_fetch_array($result))

{
echo"<option value=".$rows[0].">". $rows[1]."</option>";
}

mysql_close();
?>


</select></td>
</tr>
<tr>
<td>From</td><td><input type="text" name="from"></td>
<td>To</td><td><input type="text" name="to"></td>
<td><input type="submit" name="submit" value="submit"></td></tr>

</table>
</form>
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: dynamic dropdown

 
0
  #9
Dec 19th, 2007
Originally Posted by lydia21 View Post
hi
this works but how to get the dropwown value in the next page..
i tried with $id=$_REQUEST['id']; but this is not working
<form method="get" action="sort2.php">
<?php
$hostname = "localhost";
$username = "";
$password = "";
$dbid = "";
$link=mysql_connect($hostname, $username, $password);
mysql_select_db($dbid) or die("unable to connect");



$sql="SELECT id,name FROM personal";
$result=mysql_query($sql);

?>
<table>



<tr>
<td>Splendor Id</td><td><select name="Names">
<?php

while($rows=mysql_fetch_array($result))

{
echo"<option value=".$rows[0].">". $rows[1]."</option>";
}

mysql_close();
?>


</select></td>
</tr>
<tr>
<td>From</td><td><input type="text" name="from"></td>
<td>To</td><td><input type="text" name="to"></td>
<td><input type="submit" name="submit" value="submit"></td></tr>

</table>
</form>
$id=$_REQUEST['id'] should be $id=$_REQUEST['Names']

And Please, next time you post your code, place it within [ code] ... [ / code] tags !
Last edited by nav33n; Dec 19th, 2007 at 7:10 am.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 183
Reputation: lydia21 is an unknown quantity at this point 
Solved Threads: 5
lydia21 lydia21 is offline Offline
Junior Poster

Re: dynamic dropdown

 
0
  #10
Dec 19th, 2007
sorry.......

thanks
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC