943,691 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1156
  • PHP RSS
Dec 19th, 2007
0

dynamic dropdown

Expand Post »
hi

how to polute my drop down using the database content...........
Similar Threads
Reputation Points: 19
Solved Threads: 5
Junior Poster
lydia21 is offline Offline
183 posts
since Nov 2007
Dec 19th, 2007
0

Re: dynamic dropdown

polute ? What do you mean by that ?
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Dec 19th, 2007
1

Re: dynamic dropdown

try this code it works for me

PHP Syntax (Toggle Plain Text)
  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>
Reputation Points: 11
Solved Threads: 2
Light Poster
tirivamwe is offline Offline
36 posts
since Oct 2005
Dec 19th, 2007
0

Re: dynamic dropdown

sorry how to retrieve the table content and display in a dropdown
Reputation Points: 19
Solved Threads: 5
Junior Poster
lydia21 is offline Offline
183 posts
since Nov 2007
Dec 19th, 2007
0

Re: dynamic dropdown

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
Reputation Points: 11
Solved Threads: 2
Light Poster
tirivamwe is offline Offline
36 posts
since Oct 2005
Dec 19th, 2007
0

Re: dynamic dropdown

thanks
Reputation Points: 19
Solved Threads: 5
Junior Poster
lydia21 is offline Offline
183 posts
since Nov 2007
Dec 19th, 2007
0

Re: dynamic dropdown

You can do something like tirivamwe has mentioned. Or you can use a function so that you can reuse the code whenever you want.
php Syntax (Toggle Plain Text)
  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>
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Dec 19th, 2007
0

Re: dynamic dropdown

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>
Reputation Points: 19
Solved Threads: 5
Junior Poster
lydia21 is offline Offline
183 posts
since Nov 2007
Dec 19th, 2007
0

Re: dynamic dropdown

Click to Expand / Collapse  Quote originally posted by lydia21 ...
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.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Dec 19th, 2007
0

Re: dynamic dropdown

sorry.......

thanks
Reputation Points: 19
Solved Threads: 5
Junior Poster
lydia21 is offline Offline
183 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: upload files
Next Thread in PHP Forum Timeline: Access hidden values by javascript





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC