Introduction
In this article I explain how to make a dynamic check box and list box using functions in PHP. This article is very simple and very useful in any website. First you will create a table in your database and insert records into it. Like this:
Example
To create a connection to your database save the following in a file "connect.php":
<?php
mysql_connect("localhost","root","")or die(mysql_error());
mysql_select_db("smart");
?>
Create a form for showing data and save it as "courses.html". Such as:
<html>
<head>
<title>Example of Dynamically Fetch Record From Database</title>
<style type="text/css">
<!--
.style1 {
font-family: Arial, Helvetica, sans-serif;
font-size: 16px;
color: #A90536;
}
.style4 {color: #0000CC}
-->
</style>
</head>
<body>
<table width="70%" border="2" align="center">
<tr>
<td colspan="4" bgcolor="#CCFFCC"><div align="center"><strong><span class="style1">Dynamically Fetch Record From Database</span></strong></div></td>
</tr>
<tr bgcolor="#FFCCCC">
<td width="26%"><span class="style4">Courses</span></td>
<td width="7%">
<select name="c_name" id="c_name">
<?php echo get_option_list("courses","c_id","c_name");?>
</select> </td>
<td width="30%"><span class="style4">Qualification</span></td>
<td width="37%">
<div style="height:100; overflow:scroll">
<?php echo get_check_list("courses","c_id","c_name","c_qual");?> </div> </td>
</tr>
<tr bgcolor="#FFCCCC">
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
</body>
</html>
Create a function for fetching data and save in the file "function.php". In this file I will create two very important functions for fetching data from your database. You can easily create a dynamic check box and list box. Both functions are:
<?php
##function for option list
function get_option_list($table,$col_id,$col_value,$sel=0)
{
$sql="select * from $table order by $col_value";
$rs=mysql_query("$sql")or die(mysql_error());
$option_list="<option value=0>Please select</option>";
while($data=mysql_fetch_assoc($rs))
{
$option_list.="<option value='$data[$col_id]'>$data[$col_value]</option>";
}
return $option_list;
}
##function for check list
function get_check_list($table,$col_id,$col_value,$name,$sel=0)
{
$sql="select * from $table order by $col_value";
$rs=mysql_query("$sql")or die(mysql_error());
$option_list="";
while($data=mysql_fetch_assoc($rs))
{
$option_list.="<input type='checkbox' name='$name' value='$data[$col_id]'>$data[$col_value]<br>";
}
return $option_list;
}
?>
This is your final code. This file includes your two script files "connect.php" and "function.php".
<?php
include_once('connect.php');
include_once('function.php');
?>
<html>
<head>
<title>Example of Dynamically Fetch Record From Database</title>
<style type="text/css">
<!--
.style1 {
font-family: Arial, Helvetica, sans-serif;
font-size: 16px;
color: #A90536;
}
.style4 {color: #0000CC}
-->
</style>
</head>
<body>
<table width="70%" border="2" align="center">
<tr>
<td colspan="4" bgcolor="#CCFFCC"><div align="center"><strong><span class="style1">Dynamically Fetch Record From Database</span></strong></div></td>
</tr>
<tr bgcolor="#FFCCCC">
<td width="26%"><span class="style4">Courses</span></td>
<td width="7%">
<select name="c_name" id="c_name">
//function calling for list box
<?php echo get_option_list("courses","c_id","c_name");?>
</select> </td>
<td width="30%"><span class="style4">Qualification</span></td>
<td width="37%">
<div style="height:100; overflow:scroll">
//function calling for check box
<?php echo get_check_list("courses","c_id","c_name","c_qual");?> </div> </td>
</tr>
<tr bgcolor="#FFCCCC">
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
</body>
</html>
Output