By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,287 Members | 1,544 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,287 IT Pros & Developers. It's quick & easy.

Disscussion about filling combo box from database in php

P: 22
Hi to all,
I am punit.i m working in php.As i m fresher in php .i am using combo box for selection of data & making Report .please tell me that how i fill the combo box dynamically.so please suggest me the code for filling combo dynamically in php
using database as sql.
Thanks
Punit
Jul 24 '07 #1
Share this Question
Share on Google+
5 Replies


nathj
Expert 100+
P: 938
Hi to all,
I am punit.i m working in php.As i m fresher in php .i am using combo box for selection of data & making Report .please tell me that how i fill the combo box dynamically.so please suggest me the code for filling combo dynamically in php
using database as sql.
Thanks
Punit
I do something like this, oce I have got the data in an associative array. For help on that please read the article on data abstraction layers.

Expand|Select|Wrap|Line Numbers
  1. // NOTE: $laResults is the associative array returned via data query
  2. echo "<select name='list1' id='list1'>;
  3.         foreach($laResults as $lcDataLine)
  4.         {               
  5.             echo "<option value=" ."'" . $lcDataLine['ID'] . "'" ;
  6.             if($lcDataLine['isDefault'])
  7. // used to indicate a default selection, this is an item in my database
  8.             {
  9.                 echo "selected='selected'";
  10.             }
  11.             echo ">" . $lcDataLine['description'] ."</option>";
  12.         }
  13.         echo "</select>";
  14.  
I hope this helps. It works for me, wven producing a country codes drop down list of all the countries in the world in no time at all.

Have a go and if you get stuck give me a shout.
nathj
Jul 24 '07 #2

P: 22
Hi Nathj,
I read your reply send it for filling combobox.
Thanks to you .But i have one problem as i am working in on two form .In one form i create one textbox & combobox incombobox i fill it manually by database feild & in textbox i use ot give entry . i have to do that i take the input from textbox & select the data from database according to combox feild for this i code for first form is :
<form name="search" method="post" action="select.php">
Seach for: <input type="text" name="find" /> in
<select name="field" id="field" >
<option value="1" >FirstName</option>
<option value="2" >LastName</option>
<option value="3" >Age</option>
</select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="select" />
</form>
& for searching form i code it as:

<?php
if ($searching =="yes")
{
echo "<h2>Results</h2><p>";

//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}

$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="projectdb"; // Database name
$tbl_name="person"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// We preform a bit of filtering
$find = $_POST['find'];
$find = trim ($find);

$ddl = $_POST['field'];

// Retrieve data from database
if($ddl = 'FirstName')
{
$sql="SELECT * FROM $tbl_name where FirstName LIKE '%$find%'";
$result=mysql_query($sql);
}
elseif($ddl = 'LastName')
{
$sql="SELECT * FROM $tbl_name where LastName LIKE '%$find%'";
$result=mysql_query($sql);
}
else
{
$sql="SELECT * FROM $tbl_name where Age LIKE '%$find%'";
$result=mysql_query($sql);
}
// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result))
{
?>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td width="30%"><? echo $rows['FirstName']; ?></td>
<td width="30%"><? echo $rows['LastName']; ?></td>
<td width="30%"><? echo $rows['Age']; ?></td>
</tr>
</table>
<?
// close while loop
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($result);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>

it is not executing both else loop.as we cut the else loop it run fine but as use else loop it gives error & also for switch statement.
Please go through this code and suggest me .As i am new for this php so please suggest me.
Thanks
punit
I do something like this, oce I have got the data in an associative array. For help on that please read the article on data abstraction layers.

Expand|Select|Wrap|Line Numbers
  1. // NOTE: $laResults is the associative array returned via data query
  2. echo "<select name='list1' id='list1'>;
  3.         foreach($laResults as $lcDataLine)
  4.         {               
  5.             echo "<option value=" ."'" . $lcDataLine['ID'] . "'" ;
  6.             if($lcDataLine['isDefault'])
  7. // used to indicate a default selection, this is an item in my database
  8.             {
  9.                 echo "selected='selected'";
  10.             }
  11.             echo ">" . $lcDataLine['description'] ."</option>";
  12.         }
  13.         echo "</select>";
  14.  
I hope this helps. It works for me, wven producing a country codes drop down list of all the countries in the world in no time at all.

Have a go and if you get stuck give me a shout.
nathj
Jul 27 '07 #3

nathj
Expert 100+
P: 938
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. ....
  3. // Retrieve data from database 
  4. if($ddl = 'FirstName')
  5. {
  6. $sql="SELECT * FROM $tbl_name where FirstName LIKE '%$find%'";
  7. $result=mysql_query($sql);
  8. }
  9. elseif($ddl = 'LastName')
  10. {
  11. $sql="SELECT * FROM $tbl_name where LastName LIKE '%$find%'";
  12. $result=mysql_query($sql);
  13. }
  14. else
  15. {
  16. $sql="SELECT * FROM $tbl_name where Age LIKE '%$find%'";
  17. $result=mysql_query($sql);
  18. }
  19. ....
  20. ?> 
  21.  
Hi ,

I would try replacing the code snippet I quoted above with:

Expand|Select|Wrap|Line Numbers
  1. switch ($ddl)
  2. {
  3.  case "1": // first name
  4.    $sql="SELECT * FROM $tbl_name where FirstName LIKE '$find'";
  5.   break;
  6.  case "2": // last name
  7.    $sql="SELECT * FROM $tbl_name where LastName LIKE '$find'";
  8.   break ;
  9.  case "3":// age
  10.    $sql="SELECT * FROM $tbl_name where Age LIKE '$find'";
  11.   break;
  12. }
  13.  
  14. if !empty($sql)
  15. {
  16.   $result=mysql_query($sql);
  17. }
  18. // if there is a result etc - you can carry on with the rest of your code.
  19. }
  20.  
This is waht I would do. Now I'm not saying this will work first time but it's a slicker structure that you should be able to play around with a bit easier.

If you are not getting the desired results try putting 'echo' lines in the code to indeicate where you are. That way you will get output on the screen indicating where you are at.

Cheers
nathj
Jul 27 '07 #4

P: 22
Thanks Nathj it works
Punit
Hi ,

I would try replacing the code snippet I quoted above with:

Expand|Select|Wrap|Line Numbers
  1. switch ($ddl)
  2. {
  3.  case "1": // first name
  4.    $sql="SELECT * FROM $tbl_name where FirstName LIKE '$find'";
  5.   break;
  6.  case "2": // last name
  7.    $sql="SELECT * FROM $tbl_name where LastName LIKE '$find'";
  8.   break ;
  9.  case "3":// age
  10.    $sql="SELECT * FROM $tbl_name where Age LIKE '$find'";
  11.   break;
  12. }
  13.  
  14. if !empty($sql)
  15. {
  16.   $result=mysql_query($sql);
  17. }
  18. // if there is a result etc - you can carry on with the rest of your code.
  19. }
  20.  
This is waht I would do. Now I'm not saying this will work first time but it's a slicker structure that you should be able to play around with a bit easier.

If you are not getting the desired results try putting 'echo' lines in the code to indeicate where you are. That way you will get output on the screen indicating where you are at.

Cheers
nathj
Jul 27 '07 #5

nathj
Expert 100+
P: 938
Thanks Nathj it works
Punit
That's great! All the best with the rest of your project, and remember if you have any more questions just post back here.

nathj
Jul 27 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.