Friday, 14 February 2014

Dynamic Check box inserting and updating using substr & for each function

<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'];
$nselected_hobbies= "";
foreach($nhobbies as $nhobbie)
{
$nselected_hobbies .= $nhobbie . ", ";
}
$nselected_hobbies = substr($nselected_hobbies,0,-2);
$sqlq = mysql_query("insert into form values('','$nselected_hobbies')");

}
?>

<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