Search This Blog

07 January 2019

Codeigniter Using CRUD

Codeigniter Using CRUD


Change The Configuration in Config File:

Open root folder/Application/Config-> Autoload.php 

$autoload['libraries'] = array('database','session');

Open root folder/Application/Config-> database.php


'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'Your DB Name',
'dbdriver' => 'mysqli',

Create  One  Controller File :

<?php
Class Crud extends CI_Controller
{
}  ?>

Create One Model File:

<?php
Class Crud extends CI_Model
{
}  ?>

Controller:

<?php
/**
 * 
 */
Class Crud extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('Crud_model');
$this->load->helper('url');
}
public function select(){
$result['data']=$this->Crud_model->select();
$this->load->view('select',$result);
}
public function insert(){
$this->load->view('index');
if($this->input->post('s')){
$name=$this->input->post('nme');
$gender=$this->input->post('gen');
$res=$this->input->post('res');
$config['upload_path']="./images";
$config['allowed_types']="png|jpg|gif";
$this->load->library('upload',$config);
$file="Image";
if($this->upload->do_upload($file)){
             $img=$this->upload->data('file_name');
$this->db->query("Insert into crud values(NULL,'$name','$gender','$res','$img')");
}else{
echo $this->upload->display_errors();
}
}
}


public function delete(){
$del=$this->input->get('del');
$this->Crud_model->delete($del);
redirect('/Crud/select');
}
public function show(){
$id=$this->input->get('up');
$data['result']=$this->Crud_model->show($id);
$this->load->view('show',$data);
if($this->input->post('sub')){
$name=$this->input->post('nme');
$gender=$this->input->post('gen');
$res=$this->input->post('res');
$config['upload_path']="images";
$config['allowed_types']="jpg|png|gif";
$this->load->library('upload'config);
$file="image";
if($this->upload->do_upload($file)){
$img=$this->upload->data('file_name');
$sql="update crud set name='$name',gender='$gender',result='$res',image='$img' where id='$id'";
$this->db->query($sql);
redirect('/Crud/select');
}
}
}



}



?>

Model File:

index.php

   <?php

   Class Crud_model extends CI_Model{

    public function select()
      {
$sql="select * from crud";
$query=$this->db->query($sql);
return $query->result();
    }

    public function delete($del){
   $sql="delete from crud where id='$del'";
$query=$this->db->query($sql);
   }

  public function show($id){
   $sql="select * from crud where id='$id'";
   $query=$this->db->query($sql);
   return $query->result();
  }

  }
  ?>

View File:

<!DOCTYPE html>
<html>
<head>
<title>Crud</title>
</head>
<body>
<table>

<form method="post" enctype="multipart/form-data">
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name();  ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">
<tr>
<td>Name</td><td><input type="text" name="nme" autocomplete="off" ></td>
</tr>
<tr>
<td>gender</td><td><input type="radio" name="gen" value="Male" autocomplete="off" >Male
        <input type="radio" name="gen" value="Female" autocomplete="off" >Female
</td>

</tr>
<tr>
<td>Result</td><td><select name="res"><option>Select Result</option><option>Pass</option><option>Fail</option></select></td>
</tr>
<tr>
<td>Image</td><td><input type="file" name="Image" ></td>
</tr>
</table>
<input type="submit" name="s" value="Insert">
</form>

</body>
</html>

Select.php

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table>
<tr><th>Name</th><th>gender</th><th>Result</th><th>Image</th></tr>
<?php foreach ($data as $row) { ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->gender; ?></td>
<td><?php echo $row->result; ?></td>
<td><img width="100px" height="100px" src="<?php echo base_url()."images/".$row->image; ?>"></td>
    <td><a href='<?php echo "show?up=".$row->id; ?>'>Update</a></td>
<td><a href='<?php echo "delete?del=".$row->id;  ?>'>delete</a></td>
</tr>
<?php } ?>
</table>
</body>
</html>

Update.php:

<!DOCTYPE html>
<html>
<head>
<title>Crud</title>
</head>
<body>
<table>

<form method="post" enctype="multipart/form-data">
<?php foreach ($result as $row) {
?>
<input type="hidden" value="<?php  echo $row->id; ?>">
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">
<tr>
<td>Name</td><td><input value="<?php echo $row->name ?>" type="text" name="nme" autocomplete="off" ></td>
</tr>
<tr>
<?php if($row->gender=="Male") {?>
<td>gender</td><td><input checked="true" type="radio" name="gen" value="Male" autocomplete="off" >Male
        <input type="radio" name="gen" value="Female" autocomplete="off" >Female
</td>
<?php }else{?>
<td>gender</td><td><input  type="radio" name="gen" value="Male" autocomplete="off" >Male
        <input type="radio" checked="true" name="gen" value="Female" autocomplete="off" >Female
</td>
<?php }?>
</tr><?php if($row->result=="Pass"){?>
<tr>
<td>Result</td><td><select name="res"><option>Select Result</option><option selected="true">Pass</option><option>Fail</option></select></td>
</tr><?php } else{ ?>
<tr>
<td>Result</td><td><select name="res"><option>Select Result</option><option>Pass</option><option selected="true">Fail</option></select></td>
<?php } ?>
<tr>
<td>Image</td><td><img width="200px" height="200px" src="<?php echo base_url().'images/'.$row->image;?>"></td>
</tr>
<tr>
<td>Select Image</td><td><input type="file" name="image"></td>
</tr>
</table>
<?php } ?>
<input type="submit" name="sub" value="Update">
</form>

</body>
</html>
---------------------------------------------------
Simple Steps:

$this->db->query($sql)->row //single record

// IF you Want to get More than Single Row use  (row)

single rows count 

$q=$this->db->query($sql)->row
count($q)

-----------------------------------------------------------------------

$this->db->query($sql)->rows // multiple records

// IF you Want to get More than Multiple Row use  (rows)

multiple rows Count

$q=$this->db->query($sql)->rows
$cnt=$q->num_rows();

Outputs:






27 July 2018

PHP using Shopping Cart

PHP Using Shopping Cart


Index.php:

  <!DOCTYPE html>
<html>
<head>
<style>
.overlay {
  background: #304352;
  opacity: 0.4;
  position: absolute;
 right: 0;
}
.overlay1 {
  background: #fff;
  opacity: 0.4;
  position: absolute;

  top: 0;
  left: 0;
  right: 0;

}
</style>
<title> Admin Panel </title>
</head>
<script src="js/jquery.js"> </script>
<link rel="stylesheet" type="text/css" href=" css/bootstrap.min.css">
<body>
<div class="container">
<div class="row">
<center> <h1> Shopping Cart </h1> </center>
<div class="col-md-12">
<ul type="none">
<?php $con=mysqli_connect("localhost","root","","test");
   $sql=" select * from product ";
   $res=$con->query($sql);
   while($row= mysqli_fetch_array($res)){
?>
<li >
<div class="col-md-3" >
<img width="100%" src="images/<?php echo $row['image']; ?>" >
<center>
<h4> <?php echo $row['pname']; ?> </h4>
<h4> <?php echo $row['pdesc']; ?> </h4>
<h4> <?php echo "$".$row['price']; ?> </h4>
<a href="index.php?id=<?php echo $row['id']; ?>" class="btn btn-danger"> Add cart </a>
</center>
</div>
</li><?php } ?>
</ul><br>
<h4><a href="show.php" target="_blank">Show cart</a></h4>
</div>
</div>
</div>
</body>
</html>
<?php
session_start();

if(empty($_SESSION['cart']))
$_SESSION['cart']=array();
if(isset($_GET['id'])){
array_push($_SESSION['cart'],@$_GET['id']);
}
?>

Show Product:
<?php
$con=mysqli_connect("localhost","root","","test");

if(!isset($_SESSION['cart']) or $_SESSION['cart']==NULL)
echo " No product";
else{
$id=implode(',',$_SESSION['cart']);
$sql=" select * from product where id in($id) ";
$res=$con->query($sql);
echo "<table class='table table-hover'><tr>
<th> Id </th> <th> name </th> <th> Description </th><th> Image </th> <th> Quantity </th> <th> price </th>
</tr>";
$price=0;
while($row=mysqli_fetch_array($res)){
$price+=$row['price']*$row['quantity'];
echo "

<tr>
<td> ".$row['id']." </td>
<td> ".$row['pname']." </td>
<td> ".$row['pdesc']." </td>
<td> <img  width='18%' src='images/".$row['image']."' </td>
<td> <input style='width:50px;' value='".$row['quantity']."' type='text' onkeyup='inc(".$row['id'].")' name='qty[]' id='qty".$row['id']."'></td>
<td id='s".$row['id']."'>".$row['price']*$row['quantity']." </td>
<td> <a href='?rm=".$row['id']."'> Remove </a> </td>
</tr>
";



}echo " <tr>
<td colspan='5'>
Total
</td>
<td> ".$price." </td>
</tr>

</table> <br>";

echo " <h4> <a href='?all'> remove all </a> </h4>";

}

Remove Product :


<?php     //Remove All
session_start();
if(isset($_GET['all'])){
unset($_SESSION['cart']);
}


else if (isset($_GET['rm'])){  //Remove Particular cart
foreach ($_SESSION['cart'] as $key => $pro) {
if($pro==$_GET['rm']){
unset($_SESSION['cart'][$key]);
}

}
}
?>

Increase Quantity:

<script type="text/javascript">
function inc(e){
var qty=document.getElementById('qty'+e).value;
$.post("inc.php",{id:e,qty:qty},function(data){
document.getElementById('s'+e).innerHTML=data;
});
}

</script>


<?php
session_start();
$con=mysqli_connect("localhost","root","","test");
if(isset($_POST['id'])){
$id=$_POST['id'];
$qty=$_POST['qty'];
$sql=" update product set quantity='$qty' where id='$id' ";
$con->query($sql);
$sql=" select * from product where id='$id' ";
$res=$con->query($sql);
while($row=mysqli_fetch_array($res)){
$price=$row['quantity']*$row['price'];
echo $_SESSION['price']=$price;
}
}

?>

# 3 Files 

1.index.php
2.show.php
3.inc.php


Output:
















26 July 2018

Javascript Validation

JavaScript Form Validation

<!DOCTYPE html>
<html>
<head>
<title>Javascript Validation</title>
</head>
<style type="text/css">
.inpt {
    padding: 10px;
    width: 290px;
    border-radius: 4px;
    border: 1px solid #a59c9c;
    font-size: 16px;
    box-sizing: content-box;
}
input[type="button"] {
    padding: 8px;
    width: 8%;
    border-radius: 4px;
    border: 1px solid #c5bfbf;
    margin-left: 4%;
}
</style>
<script type="text/javascript">
var frm=document.getElementById('frm');
var sub=document.getElementById('sub');

 function empty(e){
        user = document.getElementById("unm").value;
    pwd = document.getElementById("pass").value;
 
    if(user=="" && pwd==""){
        alert('username and password is Empty');
      event.preventDefault();
     
    }else{
     
    }
}
function ckdate(){
    var dt=document.getElementById('dt').value;
    var today = new Date();
    var y = today.getFullYear();                     
    var m = ("0" + (today.getMonth() + 1)).slice(-2);
    var dy = ("0" + today.getDate()).slice(-2); 
    var cur=y+"-"+m+"-"+dy;
     if(cur < dt){
       alert('NOT select Future date');
       event.preventDefault();
       document.getElementById('dt').value='';
    }else{
       
    }
}



function val(e){
var fname=document.getElementById('fname').value;
var lname=document.getElementById('lname').value;
var email=document.getElementById('email').value;
var pwd=document.getElementById('pwd').value;
var cpwd=document.getElementById('cpwd').value;
var pno=document.getElementById('pno').value;





if(fname==""){
document.getElementById('e1').innerHTML="Enter Your First Name";
document.getElementById('e1').style.color="red";
}else{
document.getElementById('e1').innerHTML="";
}
if (lname=="") {document.getElementById('e2').innerHTML="Enter Your Last Name";
document.getElementById('e2').style.color="red";}
else{
document.getElementById('e2').innerHTML="";
}
if(email==""){document.getElementById('e3').innerHTML="Enter Your Email";
document.getElementById('e3').style.color="red";}
else{
document.getElementById('e3').innerHTML="";
}
if(pwd==""){document.getElementById('e4').innerHTML="Enter Your Password";
document.getElementById('e4').style.color="red";}
else{
document.getElementById('e4').innerHTML="";
}
if(cpwd==""){document.getElementById('e5').innerHTML="Enter Your Confirm Password";
document.getElementById('e5').style.color="red";}
else{
document.getElementById('e5').innerHTML="";
}
if(pno==""){document.getElementById('e6').innerHTML="Enter Your phone Number";
document.getElementById('e6').style.color="red";}
else{
document.getElementById('e6').innerHTML="";
}

}

function fnm(){
var fname=document.getElementById('fname').value;
if(fname.length > 10){
document.getElementById('e1').innerHTML="The Character is less then 10";
document.getElementById('e1').style.color="red";
}else{
document.getElementById('e1').innerHTML="";
}
}
function lnm(){
var lname=document.getElementById('lname').value;
if(lname.length > 10){
document.getElementById('e2').innerHTML="The Character is less then 10";
document.getElementById('e2').style.color="red";
}else{
document.getElementById('e2').innerHTML="";
}
}

function passwd(){
var pwd=document.getElementById('pwd').value;
if(pwd.length < 8){
document.getElementById('e4').innerHTML="Password is greater then 8";
document.getElementById('e4').style.color="red";
}else{
document.getElementById('e4').innerHTML="";
}
}
function cnfrm(){
var pwd=document.getElementById('pwd').value;
var cpwd=document.getElementById('cpwd').value;
if(pwd==cpwd){
 document.getElementById('e5').innerHTML="";
}else{
     document.getElementById('e5').innerHTML="Password is NOT Mached";
document.getElementById('e5').style.color="red";
//document.getElementById('cpwd').value='';
document.getElementById('cpwd').focus();
}
}

function phone(){
var pno=document.getElementById('pno').value;
if(pno.length > 10){
document.getElementById('e6').innerHTML="Phone Number is max 10";
document.getElementById('e6').style.color="red";
}else{
document.getElementById('e6').innerHTML="";
}
}


</script>
<body>
<center><h1>Javascript Form Validation</h1></center>
<center>
<form id="frm" >
<table>
<tr><td>First Name</td>
<td><input class="inpt" onkeyup="fnm()"  placeholder="Enter First name" type="text" id="fname"></td>
<td><span id="e1"></span></td></tr>
<tr><td>Last Name</td>
<td><input class="inpt" onkeyup="lnm()" placeholder="Enter Last name" type="text" id="lname"></td>
<td><span id="e2"></span></td</tr>
<tr><td>Email</td>
<td><input class="inpt" placeholder="Enter Email" type="text" id="email"></td>
<td><span id="e3"></span></td</tr>
<tr><td>Password</td>
<td><input class="inpt" onkeyup="passwd()" placeholder="Enter Password" type="password" id="pwd"></td>
<td><span id="e4"></span></td</tr>
<tr><td>Confirm Password</td>
<td><input class="inpt" onchange="cnfrm()" placeholder="Enter Confirm Passowrd" type="password" id="cpwd"></td>
<td><span id="e5"></span></td</tr>
<tr><td>Phone Number</td>
<td><input class="inpt" onkeyup="phone()" placeholder="Enter Phone Number" type="text" id="pno"></td>
<td><span id="e6"></span></td</tr>
</table><br>
<input type="button" id="sub" onclick="val(event)"  value="check">
</form>
</center>

</body>
</html>






Jquery or Javascript Start Exam Time

 <script> function startTimer() {      var date = "<?php echo $date ?>"; // dynamic date      var time = "<?...