<?php if(isset($_SESSION['msg']) ){?>
<div class="alert alert-info">                
<h4>Info!</h4>
<?php echo $_SESSION['msg']; unset($_SESSION['msg']); ?>
</div> <?php }  ?> 
 <?php
 if(isset($_REQUEST['submit']))
 {
    $state_id             = strtoupper(mysql_real_escape_string($_POST['state_id']));
    $town_name            = ucwords(strtolower(mysql_real_escape_string(trim($_POST['town_name']))));
    $sql                  = mysql_query("select * from `tbl_town` where `state_id`='$state_id' and `town_name`='$town_name' ")or die(mysql_error());
    if(mysql_num_rows($sql)>0)
    {
        $_SESSION['msg']   = 'Project Name Already Exists!!!!!! ';
    }
    else
    {
        $_SESSION['msg']   = 'Project Name Added Sucessfully!!';
        mysql_query("insert into `tbl_town` set `state_id`='$state_id', `town_name`='$town_name' ")or die(mysql_error());
    }
    header("location:home.php?page=town_master");   
 }
 if(isset($_REQUEST['update']))
 {
    $id                      = $_POST['id'];
    $state_id                = strtoupper(mysql_real_escape_string($_POST['state_id']));
    $town_name               = ucwords(strtolower(mysql_real_escape_string($_POST['town_name'])));
    $sql                     = mysql_query("select * from `tbl_town` where `id`!='$id' and `state_id`='$state_id' and `town_name`='$town_name' ")or die(mysql_error());
    if(mysql_num_rows($sql)>0)
    {
        $_SESSION['msg']   = 'Project name Already Exists!!!!!! ';
    }
    else
    {
        $_SESSION['msg']   = 'Project Name Update Sucessfully!!';
        mysql_query("update `tbl_town` set `state_id`='$state_id', `town_name`='$town_name' where `id`='$id' ")or die(mysql_error());
    }
    header("location:home.php?page=town_master");    
 }
 ?>           
<div class="row-fluid" >
    <div class="span12">
        <div class="head clearfix">
            <div class="isw-ok"></div>
            <h1>Project Master</h1>
        </div>
        <div class="block-fluid">
            <form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
              <fieldset>
              <?php
               if(isset($_REQUEST['page_id']))
               {
                   $id            =       $_REQUEST['page_id'];
                   $modify_menu   =       mysql_query("SELECT * FROM `tbl_town` WHERE `id` = '$id'");
                   $show_modify   =       mysql_fetch_array($modify_menu);
               }
              ?>
              <input type="hidden" name="id" placeholder="Page Name" <?php if(isset($_REQUEST['page_id'])){?> required value="<?php echo $show_modify['id'];  ?>"<?php } ?> id="txt_name" autocomplete="off" />
                     <div class="row-form clearfix">
                        <div class="span3">
                            <label>State</label>
                            <select name="state_id" onchange="get_staff_name(this.value);" required>
                            <option value="">Select State</option>
                            <?php 
                            $sql_st = mysql_query("SELECT * FROM `state` ORDER BY `state_name` ASC ")or die(mysql_error());
                            while($row_st= mysql_fetch_array($sql_st))
                            {
                                $type    = "";
                                if(isset($show_modify['state_id']) && ($show_modify['state_id']==$row_st['id']))
                                {
                                    $type = "selected";
                                }
                            echo "<option value='".$row_st['id']."' $type>".$row_st['state_name']."</option>";
                            }
                            ?>
                        </select>
                        </div>
                        <div class="span3">
                            <label>Project Name</label>
                            <input type="text" name="town_name" placeholder="Project Name" <?php if(isset($_REQUEST['page_id'])){?>value="<?php echo $show_modify['town_name'];  ?>"<?php } ?> autocomplete="off" id="town_name" onkeyup="capital_letter('town_name');" required/>
                        </div>
                    <div class="span3">
                        <label>&nbsp;</label>
                        <input type="submit" tabindex="3" name="<?php if(isset($_REQUEST['page_id'])){echo "update"; }else{ echo "submit";}?>" class="btn btn-block" value="<?php if(isset($_REQUEST['page_id'])){ echo "Update"; }else{ echo "Save";}?>"/>
                        </div>
                        </div>
              </fieldset>
            </form>  
        </div> 
    </div>
</div> 
<div class="row-fluid" >
    <div class="span12">    
        <div class="head clearfix">
        <div class="isw-ok"></div>
        <h1>Project Master List</h1>
     </div>   
        <div class="block-fluid">
            <table cellpadding="0" cellspacing="0" width="100%" class="table" id="example">
                <thead>
                    <tr>
                        <th>S.No.</th>
                        <th>State Name</th>
                        <th>Project Name</th>
                        <th>Modify</th>
                    </tr>
                </thead>                 
                <tbody>
                    <?php
                    $i=1;
                    $menu       = mysql_query( "SELECT * FROM `tbl_town` where 1 order by `town_name` ASC ")or die(mysql_error());
                    while($show_menu = mysql_fetch_array($menu)) 
                    {
                    ?>  
                        <tr id="tr_num_<?php echo $i;?>">
                        <td><?php echo $i++; ?></td>
                        <td ><?php echo state($show_menu['state_id']); ?></td>
                        <td><?php echo $show_menu['town_name']; ?></td> 
                        <td class="center" style="text-align:center">
                            <a href="home.php?page=town_master&page_id=<?php echo $show_menu['id']; ?>"><img src="images/edit_new.png"  border="0" style="width: 20px;"/></a>
                        </td>      
                    </tr>
                   <?php } ?>                
                </tbody>            
            </table> 
        </div>    
   </div>
   </div> 
<script>
function capital_letter(id)
{
    var arr           = $('#'+id).val().split(' ');
    var result        = "";
    for (var x=0; x<arr.length; x++)
        result+=arr[x].substring(0,1).toUpperCase()+arr[x].substring(1)+' ';
    $('#'+id).val(result.substring(0, result.length-1));
}
$(document).ready(function() {
    var table = $('#example').DataTable( {
        "order": [[ 2, "asc" ]],
        "pageLength": 50,stateSave: true,
        fixedHeader: {
            header: true,
            footer: true
        }
    } );
     table.on( 'order.dt search.dt', function () {
        table.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
            cell.innerHTML = i+1;
        } );
    } ).draw(); } );
    </script>   
