Tuesday, 18 February 2014

Dynamic Check box inserting and updating using implode and explode functions

<form  method="post">
Hobbies:
    <?php
include('connect.php');
$sql=mysql_query("SELECT * FROM olx_area ");
$cnt=mysql_num_rows($sql);

while($fcat=mysql_fetch_array($sql))
{?>
            <input type="checkbox" name="hobbies[]" value="<?php echo $fcat['area_name'];?>"><?php echo $fcat['area_name'];?><br>
            <?php }?>
           
<input type="submit" name="sub" value="save">
</form>
<?php
if(isset($_POST['sub']))
{
$nhobbies=$_POST['hobbies'];
$imp=implode(", ",$nhobbies);
$sqlq = mysql_query("insert into form values('','$imp')");
}
?>

<form method="post">
enter id :<input type="text" name="id">
<input type="submit" name="edit" value="edit">
</form>
<?php
include('connect.php');
if(isset($_POST['edit']))
{
$eid=$_POST['id'];
$qwe=mysql_query("SELECT * FROM form WHERE id=$eid" );
$fet=mysql_fetch_array($qwe);

$string="$fet[hobbies]";
echo $string;
$array=explode(', ', $string);
$result = count($array);
$res=$result-1;
echo"Total counting="; echo $result;echo "<br>";
 for($i=0;$i<=$res;$i++)
 {
 echo "$array[$i]";
 }

 
         


$sql=mysql_query("SELECT * FROM olx_area ");



while($fcat=mysql_fetch_array($sql))
{?>
            <input type="checkbox" name="hobbies[]" value="<?php echo $fcat['area_name'];?>"
           
            <?php for($i=0;$i<=$res;$i++)
{
if($array[$i]==$fcat['area_name'])
{
echo "checked";
}

}
?>
         
            >
<?php echo $fcat['area_name'];?><br>
            <?php }
 }          
?>

No comments:

Post a Comment