PHP Classes

Simple Students CRUD: Perform CRUD operations on a students MySQL table

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 454 All time: 6,147 This week: 38Up
Version License PHP version Categories
students-crud 1.0.0Freely Distributable5PHP 5, Databases
Description 

Author

This class can perform CRUD operations on a students MySQL table.

It connects to a MySQL database using PDO and can perform all basic CRUD operations.

The class can add a new student with name and age, update a given student with its id, name and age, delete a student with a given id, and get all the students information.

Picture of Jesus Tovar
Name: Jesus Tovar <contact>
Classes: 1 package by
Country: Mexico Mexico

Example

<?php
include_once ('students.core.php');
/**
 * student fn
 *
 * @package crud
 * @author tovarj4
 * @copyright 2016
 * @version $Id$
 * @access public
 */
$_student = new student();
if (isset(
$_POST['action']))
{
    if (
$_POST['action'] == 0)
    {
//All Students
       
$arrStudents = $_student->getAll();
       
$len = count($arrStudents);
       
$tr = "";

        for (
$i = 0; $i < $len; $i++)
        {

            if (
$i % 2 == 0)
            {
               
$tr .= "<tr class='active'>";
            } else
            {
               
$tr .= "<tr class='success'>";
            }

           
$tr .= "<td>" . $arrStudents[$i]['id_student'] . "</td>";
           
$tr .= "<td>" . $arrStudents[$i]['student_name'] . "</td>";
           
$tr .= "<td>" . $arrStudents[$i]['student_age'] . "</td>";
           
$tr .= "<td><a href='javascript:student.loadToEdit(" . $arrStudents[$i]['id_student'] .
               
")' class='btn btn-warning'>Edit/Delete</a></td>";
           
$tr .= "</tr>";
        }

        if (
$_student->error)
        {
            echo
$tr;
        } else
        {
            echo
" Error al consultar" . $_student->qry;
        }
    }

    if (
$_POST['action'] == 1)
    {
//add Student


       
$student_name = $_POST['name'];
       
$student_age = $_POST['age'];

       
$_student->add($student_name, $student_age);

        if (
$_student->error)
        {
           
$arrJson = array("error" => 'true');
        } else
        {
           
$arrJson = array("error" => 'false');
        }
        echo
json_encode($arrJson);

    }
    if (
$_POST['action'] == 2)
    {
//edit Student

       
$id_student = $_POST['id'];
       
$student_name = $_POST['name'];
       
$student_age = $_POST['age'];

       
$_student->update($id_student, $student_name, $student_age);

        if (
$_student->error == 'true')
        {
           
$arrJson = array("error" => 'true');
        } else
        {
           
$arrJson = array("error" => 'false');
        }
        echo
json_encode($arrJson);


    }
    if (
$_POST['action'] == 3)
    {
//delete Student

       
$id_student = $_POST['id'];

       
$_student->delete($id_student);

        if (
$_student->error == 'true')
        {
           
$arrJson = array("error" => 'true', "errDesc" => $_student->qry);

        } else
        {
           
$arrJson = array("error" => 'false', "errDesc" => "");
        }
        echo
json_encode($arrJson);


    }
    if (
$_POST['action'] == 4)
    {
//get single student

       
$id_student = $_POST['id'];

       
$student = $_student->getSingle($id_student);

        if (
$_student->error == 'true')
        {
            echo
json_encode(array("error" => $_student->error, "qry" => $_student->qry));
        } else
        {

            echo
json_encode($student);
        }


    }
}

/* if ($_GET['action']==4) { //get single student

$id_student = $_GET['id'];

$student = $_student->getSingle($id_student);


if($_student->error=='true'){
echo json_encode( array("error"=>$_student->error,"qry"=>$_student->qry));
} else {

echo json_encode($student);
}
}*/

?>


Details

phpCrud

simple crud with php, mariaDB, jquery ajax calls and bootstrap, also use the sweet-alert.js(https://t4t5.github.io/sweetalert/) library to show messages


  Files folder image Files (16)  
File Role Description
Files folder imageclass (2 files, 1 directory)
Files folder imagecss (4 files)
Files folder imagefonts (1 file)
Files folder imagejs (4 files)
Files folder imageMySqlDatabase (1 file)
Accessible without login Plain text file .htaccess Data Auxiliary data
Accessible without login Plain text file index.html Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (16)  /  class  
File Role Description
Files folder imagecore (1 file)
  Plain text file students.core.php Class Class source
  Accessible without login Plain text file students.fn.php Example Example script

  Files folder image Files (16)  /  class  /  core  
File Role Description
  Accessible without login Plain text file db.inc Conf. Configuration script

  Files folder image Files (16)  /  css  
File Role Description
  Accessible without login Plain text file bootstrap-theme.min.css Data Auxiliary data
  Accessible without login Plain text file bootstrap.min.css Data Auxiliary data
  Accessible without login Plain text file style.css Data Auxiliary data
  Accessible without login Plain text file sweet-alert.css Data Auxiliary data

  Files folder image Files (16)  /  fonts  
File Role Description
  Accessible without login Plain text file glyphicons-halflings-regular.svg Data Auxiliary data

  Files folder image Files (16)  /  js  
File Role Description
  Accessible without login Plain text file bootstrap.min.js Data Auxiliary data
  Accessible without login Plain text file jquery.min.js Data Auxiliary data
  Accessible without login Plain text file scripts.js Data Auxiliary data
  Accessible without login Plain text file sweet-alert.js Data Auxiliary data

  Files folder image Files (16)  /  MySqlDatabase  
File Role Description
  Accessible without login Plain text file school.sql Data Auxiliary data

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:454
This week:0
All time:6,147
This week:38Up