Search This Blog

07 April 2018

Laravel Using CRUD Operations

LARAVEL USING CRUD

Steps:

1)switch our project then  run the server using this-> cmd then php artsian serve

2) /resource/views/your viewfile name  save as filname.blade.php

3)go to Routes page app/http/routes.php open the file and then routes the viewpage

Route::get('your parameter to view page user defined', function () {
    return view('your view file name ');
});

(or)

Create Controller File using this cmd php artisan make:controller your controller name --resource

open your controller file index() method to implement

public function index(){

return view('your view file name');
}


then open your routes page

Route::get('your parameter to view page user defined','your cotroller name@your method');

EX:
Route::get('signup','firstcontroller@index');

then submit your  form Throw the Exception TokenMismatchException
go to your view file

<form method="post" action="">
{{ csrf_field() }} ---> put this code after form tag

then submit your values are show

go to your controller store method

public function store(Request $req){

return $req->all(); ----> it will display all
(or)
return $req->your specify name();
ex:
return $req->name; -----> it will display only name

}

then go to your .env file to configure

DB_DATABASE=your database name
DB_USERNAME=your username
DB_PASSWORD=your password


Create Model

php artisan make:model model name -m
then go to your APP/database/migration your modelname table
public function up()
    {
Schema::create('your table name is alredy defined ', function (Blueprint $table) {

            $table->your datatype('your column name')
            $table->your datatype('your column name')
            $table->your datatype('your column name')
             $table->your datatype('your column name')
                });
                }

then migrate your table
php artisan migrate
check your database your tables is created or not
go to your model file
put this code
$fillable=['Your posted values name','Your posted values name','Your posted values name'];
ex:
$data=['reg','name','mak'];
then go to your controller file store method
put the import your model
ex:
use App\your modelname;
write coding for insert database
public function store(Request $req){
create object for your  model
public function store(Request $req)
        {
        $test=new yourmodelname;
        $test->regno=$req->regno;
        $test->name=$req->name;
        $test->mark=$req->mark;
         if($test->save()){return redirect()->back();}else{}
           
        }
}
then run your file check the database data are insert or not

// Display Your Record
go to  your controller file
put this code
public function index()
        {
$samename['samename']=yourmodelname::all();--->it will display all records in array format
        return view('your view file',$samename);
        }
next  go your view file
<TABLE border='1' cellpadding='8'>
<tr><th>Regno</th><th>Name</th><th>Mark</th><th>Delete</th></tr>
@foreach($samename as $row)
<tr>
<td>{{ $row['regno'] }}</td>
<td>{{ $row['name'] }}</td>
<td>{{ $row['mark'] }}</td>
<td><a href="/register/{{ $row['id'] }}">delete</a></td>
</tr>
@endforeach
</TABLE><br>

//delete

go controller file delete method
public function delete($id)
    {
                $del=your model name::find($id);
        $del->delete();
                return redirect()->back();
    }

//Update  The record
go to Route File
Route::get('yourview file name/{pass parameter ex id }','controllername@function name');

Route::post('view name','controllername@function name');

Coding:

Route.php:

   <?php


Route::get('/', function () {
    return view('welcome');
});
Route::get('first','firstcontroller@index');
Route::post('first','firstcontroller@store');

Route::get('crud','crudctrl@index');
Route::post('crud','crudctrl@store');
Route::get('update/{id}','crudctrl@show');
Route::post('upd','crudctrl@update');
Route::get('crud/{id}','crudctrl@delete');

Route::get('login','crudctrl@loginpage');
Route::post('login','crudctrl@login');

View File :

<html>
<head>
<title>CRUD Operation Using Laravel</title>
<link rel="stylesheet" type="text/css" href="{{ ('css/bootstrap.min.css') }}">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-3">
</div>
<div class="col-md-5 login-sec">
<h2 class="text-center">Signup</h2>
<form method="post" action="">
{{ csrf_field() }}
<div class="form-group">
<label for="exampleInputEmail1"  class="text-uppercase">First Name</label>
    <input type="text" class="form-control" name="fname" placeholder="" >
    <label for="exampleInputEmail1" class="text-uppercase">Last Name</label>
      <input type="text" class="form-control" name="lname" placeholder="" >
</div>
<div class="form-group">
<label class="text-uppercase">Email</label>
<input type="text" name="email" class="form-control">
</div>
<div class="form-group">
<label class="text-uppercase">Uname</label>
<input type="text" name="uname"  class="form-control">
</div>
<div class="form-group">
<label class="text-uppercase">Password</label>
<input type="password" class="form-control" name="pwd">
</div>
<center><input type="submit" value="Insert" class="btn btn-success"></center>
</form>
</div>
</div>
<table class='table table-responsive'>
<tr class='info'><th>Fname</th><th>Lname</th><th>Email</th><th>Uname</th><th>Password</th><th>Update</th><th>Delete</th></tr>
@foreach($records as $row)
<tr>
<td>{{ $row['fname'] }}</td>
<td>{{ $row['lname'] }}</td>
<td>{{ $row['email'] }}</td>
<td>{{ $row['uname'] }}</td>
<td>{{ $row['pwd'] }}</td>
<td><a href="/update/{{ $row['id'] }}"><button class='btn btn-warning'>Update</button></a></td>
<td><a href="/crud/{{ $row['id'] }}"><button class='btn btn-danger'>Delete</button></a></td>
</tr>
@endforeach
</table>
<center> {{ $rows->links()  }}
</center>
</div>
</body>
</html>

Update view file:

<html>
<head>
<link rel="stylesheet" type="text/css" href="{{ asset('css/bootstrap.min.css') }}">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-5 login-sec">
<h2 class="text-center">Update</h2>
<form method="post" action="/upd">
{{ csrf_field() }}
<input type="hidden" value="{{$sow['id'] }}" name="id">
<div class="form-group">
<label for="exampleInputEmail1"  class="text-uppercase">First Name</label>
    <input type="text" value="{{$sow['fname'] }}" class="form-control" name="fname" placeholder="" >
    <label for="exampleInputEmail1" class="text-uppercase">Last Name</label>
      <input type="text" value="{{$sow['lname'] }}" class="form-control" name="lname" placeholder="" >
</div>
<div class="form-group">
<label class="text-uppercase">Email</label>
<input type="text" value="{{$sow['email'] }}" name="email" class="form-control">
</div>
<div class="form-group">
<label class="text-uppercase">Uname</label>
<input type="text" value="{{$sow['uname'] }}" name="uname"  class="form-control">
</div>
<div class="form-group">
<label class="text-uppercase">Password</label>
<input type="password" value="{{$sow['pwd'] }}" class="form-control" name="pwd">
</div>
<input type="submit" value="update" class="btn btn-success">
</form>
</div>
</div>
</div>
</body>
</html>

Model file: 

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class crudopn extends Model
{
    protected $datas=['fname','lname','email','uname','pwd'];
}

Controller file:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\crudopn;
use DB;

class crudctrl extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $records['records']=crudopn::all();
        
        return view('crudopr',$records);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function loginpage()
    {
        return view('login');
    }




    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $inst=new crudopn;
        $inst->fname=$request->fname;
        $inst->lname=$request->lname;
        $inst->email=$request->email;
        $inst->uname=$request->uname;
        $inst->pwd=$request->pwd;
        if($inst->save()){return redirect()->back();}else{}
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
     $sow['sow']=crudopn::find($id);
     return view('update',$sow);



    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function login(Request $req)
    {
        $uname=$req->input('uname');
        $passwd=$req->input('passwd');
        $login=DB::table('crudopns')->where(['uname'=>$uname,'pwd'=>$passwd])->get();
        $cnt=count($login);
        if($cnt==1){
            echo "<script>window.location.href='http://localhost:8000/crud';</script>";

        }else{
echo "<script>alert('Your Username or Password is Wrong');window.location='http://localhost:8000/login';</script>";

        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $req)
    {
        //$up=crudopn::find($req->id);
        $updates=crudopn::find($req->id);

        $updates->fname=$req->fname;
        $updates->lname=$req->lname;
        $updates->email=$req->email;
        $updates->uname=$req->uname;
        $updates->pwd=$req->pwd;

        

        if($updates->save()){echo "<script>window.location.href='http://localhost:8000/crud';</script>";}else{}
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        $del=crudopn::find($id);
        $del->delete();
        return redirect()->back();
    }
}

Outputs:












07 March 2018

PHP Using Login Page

PHP USING LOGIN PAGE

1 1)   Signup the User Profile

   2)   Next to Login the your username and password put it Textbox 



<html>
<head></head>
// if you want any addition style sheet or online link add this 
<title> login page using php and sign up page </title>
<style>
.col1{
          position:relative;
          width:100%;
          height:100%;
       background-color:white;
      color: black;
}

.col2{
position: absolute;
    width: 40%;
    height: 55%;
    background-color: #F2F2F2;
    margin-left: 28%;
    margin-top: 8%; 
         
}
.inpt{
  padding: 12px;
   width:55%;
    border-radius: 4px;
    margin-left: 24%;
    border: 1px solid gray;
    padding-top:1px;
    marigin-top:1px;
   margin-left:1px;
margin-right:2px;
         
}
.btn{
    margin-left: 40%;
    border-radius: 4px;
    border: none;
    width: 16%;
    height: 10%;
    color:white;
    background-color: #6C7DD0;
}
</style>
<body> // body of the div
<div class="col1">
          <div class="col2">
          <br>
          <br>
          <br>  // form element starting this block
                   <form method="post" action="" enctype="multipart/form-data">
         <input type="text"  name=" uname " placeholder=" Enter Your username " class=" inpt "><br>
               <br>
                    <input type=" password "  name=" pwd " id=" pwd " placeholder=" Enter Your Password " class=" inpt "><br>
               <br>
                    <input type=" submit " onclick=' check(); ' value="login" name=" sub " class=" btn ">
                   </form>
          </div>
</div>
</body>
</html>
<?php
session_start(); // session starting 
// connect to the database
$con=mysqli_connect("localhost","root","","test"); 
// check the submit button is clicked or not 
if(isset($_POST['sub'])){
// get the post values using $_POST[''] method
$uname=$_POST['uname'];
$pwd=$_POST['pwd'];
$_SESSION['uname']=$uname;
$_SESSION['time']=time()+(30*60);
$sql=" select * from login where uname='$uname' and pwd='$pwd' ";
$res=$con->query($sql);
//counr the number of rows in the table
$cnt=mysqli_num_rows($res);
// check if  the record is no or yes
if($cnt==1){
echo "<script>alert('login sucessfully');window.location.href='logsuccess.php';</script>";
}else{
echo " <script> alert (' Your username or Password is wrong '); </script>";    
}
}
else{
echo " the user is not click the the submit button ";

}
?>

After Login:

<?php

// session start in this block
session_start();
// connect to the database 
$con=mysqli_connect("localhost","root","","test");
// print the logged user and get the session variable
echo "WELCOME MR".$_SESSION['uname']."<br>";
// the user is inactivity or something else automatically logout the session  
$now=time();
if($now > $_SESSION['time']){
          echo "<script> window.location.href=' loginpage.php '; </script> ";
          session_destroy();
}else{
   echo " user is active or working fine " ;
          //your statements;
}

Output:








03 March 2018

PHP using Dynamic Pagination

PHP USING DYNAMIC  PAGINATION


What is pagination?

Pagination is Easy to Navigate one page to another page and you want navigate specify page to select

<html>
<head></head>
<style>
.para {
 letter-spacing: 1px;
      font-size: 2.5vw;
    line-height: 2.7vw;
          font-family: grad,serif;
width: 280px;
}

#home ,
#contact p, footer p {
  color: #f9f9f9;
}

.btn {
  border:none;
  border-radius: 4px;
  transition: all 0.4s ease-in-out;
}

.btn:focus {
  background: #88b991;
  border-color: transparent;
}

.btn-success {
  background: #e44c65;
  font-weight: 300;
  letter-spacing: 2px;
  padding: 14px 32px;
  margin-top: 26px;
}

.btn-success:hover {
  background: #222;
}

a {
  color: #fff;
  -webkit-transition: 0.5s;
  -o-transition: 0.5s;
  transition: 0.5s;
  text-decoration: none !important;
}
a:hover, a:active, a:focus {
  color: #000;
  outline: none;
}

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

*:before,
*:after {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}



/*---------------------------------------
    All Section style              
-----------------------------------------*/
.oline{
          margin-top: 94vh;
          font-size:20px;
      
  }
.section-title {
  padding-bottom: 62px;
}

#about .section-title {
  padding-top: 42px;
}

#about .section-title h1 {
  margin-bottom: 0px;
  padding-bottom: 0px;
}

#about,
#gallery,
#contact {
  padding-top: 0px;
  padding-bottom: 120px;
  position: relative;
}

#gallery,
#contact, footer {
  text-align: center;
}

.overlay {
  background: #304352;
  opacity: 0.4;
  position: absolute;
  width: 100%;
  height: 0vh;
  top: 0;
  left: 0;
  right: 0;

}
</style>
<body>
<form id ='fm' method ="post" action ="">
<table>
<tr> <td> name </td> <td> <input type="text" pattern="[a-z]{0,8}" id="name" name="nm"> </td> </tr>
<tr> <td> mark </td> <td> <input type="text" name="mk"></td></tr>
<tr> <td> result </td> <td> <input type="text" name="res"></td></tr>
</table>
<input onclick='chk();' type="submit" name="sub" value="insert">
</form>
</body>
</html>
<?php
$con=mysqli_connect("localhost","root","","test");
if(isset($_POST['sub'])){
$nm=$_POST['nm'];
$mk=$_POST['mk'];
$re=$_POST['res'];

$sql="insert into mark values('$nm','$mk','$re')";
if($con->query($sql)){}
}

error_reporting(0);  // Hide the Errors
$page =$_GET['page'];

if($page=='' || $page==0){
$pg =1;
}
else{
$pg =($page*4)-4;
}



echo "
<table border ='2' cellpadding ='6' cellspacing='4'>
<tr> <th> Name </th> <th> Mark </th> <th> Result </th> </tr>
<tr>
";
$sql=" select * from mark LIMIT $pg,4 ";
$res=$con->query($sql);
while($row=mysqli_fetch_assoc($res)){
echo "<td>".$row['name']."</td>";
echo "<td>".$row['mark']."</td>";
echo "<td>".$row['result']."</td>";
echo "</tr>";
}
echo "</table>"."<br>";

$sql=" select * from mark ";
$res=$con->query($sql);
$cnt=mysqli_num_rows($res);
$cont=$cnt/4;
$cont1=ceil($cont);
for($i=1;$i<=$cont1;$i++){
?>
<li type="none" style="float:left;border:1px solid gray;padding-left:1%">
<a style="text-decoration:none;" href='paginate.php?page=<?php echo $i;?>'><?php echo $i; ?></a></li>
<?php
}

?>

Output :

Before Insert Record

After Insert Record










Jquery or Javascript Start Exam Time

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