Friday, July 22, 2011

Make Drop Down Menu From PHP Function

We use be writing a Drop Down with HTML...But I have found simple way to make it short...

Here the older one (HTML):-

<select name="P_State" class="textinput" id="P_State" onChange="display_district11(this.value)" tabindex=5> <option value="">--Please Select--</option>
<?php
do {
?>
<option value="<?php echo $row_state['CD_State']?>"<?php if (!(strcmp($row_state['CD_State'], $row_ins['CD_State']))) {echo "selected=\"selected\"";} ?>><?php echo $row_state['Desc_State']?></option>
<?php
} while ($row_state = mysql_fetch_assoc($state));
$rows = mysql_num_rows($state);
if($rows > 0) {
mysql_data_seek($state, 0);
$row_state = mysql_fetch_assoc($state);
}
?>
</select>


Here is the PHP code that I use...You just need to call this function only when you need to use the down drop..short isn't it?

<?php

$PP_State = '';
$js = "display_dis(this.value,'PP_District','panel_addr','8')";
 State_db('PP_State', 'PP_State', '7', $PP_State, $js);?>

The Code below, you need to put to some place that you can call it...
example : - Create a new page, and other page that you need the Drop Down just include this page...easy...

<?php
function State_db($name, $id, $tab, $exist, $java)
{
$sql = 'SELECT CD_State, Desc_State FROM LT_State ORDER BY Desc_State ASC';
$result = mysql_query($sql);
echo '<select name="'.$name.'" id="'.$id.'" class="textinput" tabindex="'.$tab.'" onchange="'.$java.'">';
echo '<option value="">Please Select</option>';
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$cd = $row['CD_State'];
$desc = $row['Desc_State'];
$selected = ($cd==$exist)?'selected':'';
echo '<option value="'.$cd.'" '.$selected.'>'.$desc.'</option>';
}
echo '</select>';

}
?>

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...