PHP Classes

File: functions.php

Recommend this page to a friend!
  Classes of Mehmet Kidiman  >  Developbat PHP CRUD  >  functions.php  >  Download  
File: functions.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Developbat PHP CRUD
Show pages to manage table records using Bootstrap
Author: By
Last change:
Date: 1 year ago
Size: 5,523 bytes
 

Contents

Class file image Download
<?php
ini_set
('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

function
randomString()
{
   
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
   
$randstring = '';
    for (
$i = 0; $i < 10; $i++) {
       
$randstring = $characters[rand(0, strlen($characters))];
    }
    return
$randstring;
}

function
getPrimaryKey($connect, $table)
{
   
$columns = showColums($connect, $table);
   
$countColumn = count($columns);
    for (
$x = 0; $x < $countColumn; $x++) {
        if (
$columns[$x][3] == 'PRI') {
            return
$columns[$x][0];
        }
    }
}

function
showTables($connect, $showTables)
{

    if (!empty(
$showTables)) {
        return
$showTables;
    } else {

       
$sql = "SHOW TABLES";
       
$statement = $connect->prepare($sql);
       
$statement->execute();
        return
$tables = $statement->fetchAll(PDO::FETCH_NUM);
       
//return $tables;
   
}


}

function
showColums($connect, $tableName)
{

   
$sql = "SHOW COLUMNS from $tableName ";
   
$statement = $connect->prepare($sql);
   
$statement->execute();
   
$tables = $statement->fetchAll(PDO::FETCH_NUM);
    return
$tables;

}

function
showRows($connect, $tableName)
{
   
$sql = "Select * from $tableName ";
   
$statement = $connect->prepare($sql);
   
$statement->execute();
   
$tables = $statement->fetchAll(PDO::FETCH_NUM);
    return
$tables;

}

function
countRows($connect, $tableName)
{
   
$sql = "Select * from $tableName ";
   
$statement = $connect->prepare($sql);
   
$statement->execute();
   
$count = $statement->rowCount();
    return
$count;

}

function
showOneRow($connect, $tableName, $columnId)
{
   
$column = showColums($connect, $tableName);
   
$firstColumun = $column[0][0];

   
$sql = "Select * from $tableName Where $firstColumun = $columnId ";
   
$statement = $connect->prepare($sql);
   
$statement->execute();
   
$tables = $statement->fetchAll(PDO::FETCH_NUM);
    return
$tables;

}

function
insertData($connect, $post)
{

   
$postArray[] = $post;
   
$arrayKeys = array_keys($_POST);
   
$tableNamePost = $postArray[0]['table_name_order'];
   
$queryArray = [];

    foreach (
$arrayKeys as $key) {

        if (
$key == 'table_name_order' || $key == 'table_name_order_id' || $key == 'action') {

        } else {
           
$queryArray[$key] = $_POST[$key];
        }
    }

    foreach (
$queryArray as $k => $v) {
       
$prep[':' . $k] = $v;
    }
    try {
       
$sth = $connect->prepare("INSERT INTO $tableNamePost ( " . implode(', ', array_keys($queryArray)) . ") VALUES (" . implode(', ', array_keys($prep)) . ")");
       
$res = $sth->execute($prep);

        if (isset(
$res)) {
            echo
$output = '<b>' . $tableNamePost . '</b> Added ';

            echo
'<script type="text/javascript">',
           
'reload();',
           
'</script>';

        } else {
            echo
$output = '<b>' . $tableNamePost . '</b> Not Added !! ';
        }
    } catch (
PDOException $e) {
        echo
$e->getMessage();
    }
}

function
inputType($row)
{
    switch (
$row) {
        case
'int':
           
$inputType = 'number';
            break;
        case
'varchar':
           
$inputType = 'text';
            break;
        case
'text':
           
$inputType = 'text';
            break;
        case
'decimal':
           
$inputType = 'number';
            break;
        case
'float':
           
$inputType = 'number';
            break;
        case
'enum':
           
$inputType = 'option';
            break;
        case
'date':
           
$inputType = 'date';
            break;
        default:
           
$inputType = 'text';
    }
    return
$inputType;
}

function
getRelatedRow($connect, $relatedTable, $relatedRow, $relatedWith, $getRow)
{

   
$query = $connect->prepare("SELECT * FROM $relatedTable WHERE $relatedRow = $relatedWith ");
   
$query->execute();
   
$result = $query->fetch();
    return
$result[$getRow];

}

function
getRelatedRowOptions($connect, $relatedTable, $relatedRow, $relatedWith, $getRow)
{

   
$option = '';
   
$query = $connect->prepare("SELECT DISTINCT $getRow FROM $relatedTable");
   
$query->execute();
   
$results = $query->fetchAll();
    foreach (
$results as $result) {
       
$option .= '<option value="' . $result[$getRow] . '">' . $result[$getRow] . '<option>';
    }

    return
$option;

}

function
updateData($connect, $post)
{

   
$arrayKeys = array_keys($post);
   
$tableNamePost = $post['table_name_order'];
   
$tableIdPost = $post['table_name_order_id'];
   
$primeryKeyName = getPrimaryKey($connect, $tableNamePost);

    foreach (
$arrayKeys as $key) {
        if (
$key == 'table_name_order' || $key == 'table_name_order_id' || $key == $primeryKeyName || $key == 'action') {

        } else {

           
$query = "
        UPDATE
$tableNamePost
        set
$key = :$key
        WHERE
$primeryKeyName = :$primeryKeyName
        "
;
           
$statement = $connect->prepare($query);
           
$statement->execute(
                array(
                   
':' . $key => $_POST[$key],
                   
':' . $primeryKeyName => $tableIdPost,

                )
            );
           
$result = $statement->fetchAll();
            if (isset(
$result)) {
               
$output = '<b>' . $tableNamePost . '</b> table <i>' . $tableIdPost . '<i> record Updated ';
            } else {
               
$output = '<b>' . $tableNamePost . '</b> table <i>' . $tableIdPost . '<i> record Not Updated !! ';
            }

        }

    }
    echo
$output;
}

For more information send a message to info at phpclasses dot org.