PHP Classes

PHP Database Tables Class: Store and retrieve class objects in database table

Recommend this page to a friend!
  Info   View files Example   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 114 This week: 3All time: 9,571 This week: 42Up
Version License PHP version Categories
vojjin 1.1Freeware7Databases, Tools, Design Patterns, PHP 7, P...
Description 

Author

This package can store and retrieve class objects in a database table.

It provides a base class that can perform the regular operations to insert, update, delete and select queries in a SQL database.

Application classes can extend the base class to store and retrieve their objects by defining some variables that determine how to map a class object into a given database table record.

Picture of Vojin Petrovic
  Performance   Level  
Name: Vojin Petrovic <contact>
Classes: 1 package by
Country: Serbia Serbia
Age: 58
All time rank: 436814 in Serbia Serbia
Week rank: 51 Up2 in Serbia Serbia Up

Example

<?php

use vojjin\mysql\MySql;
use
vojjin\tables\Article;
use
vojjin\tables\Writer;
use
vojjin\utilities\DateClass;
use
vojjin\utilities\Utility;

include
__DIR__ . DIRECTORY_SEPARATOR . "config.php";
$db = new MySql();


/**** Sample storing one writer ****/
$writer = new Writer();
$writer->name = "John Doe";
$writer->email = "john@doe.com";
$writer->phone = "555-2225";
$writer->dateofbirth = DateClass::stringToDate("1966-01-31");
$writer->address = ["city" => "Belgrade", "street" => "Glavna 42", "zip" => 11080];
$writer_id = $writer->insert();


/**** Sample changing writer data ****/
$writer = new Writer($writer_id);
$writer->phone = "222-5555";
$writer->update(["phone"]);

/**** Sample storing one article ****/
$article = new Article();
$article->title = "New article title";
$article->content = '<h1>Article title</h1><p>Lorem ipsum ....</p>';
$article->pagetitle = "Title";
$article->pagedesc = "Descrption of lorem ipsum page";
$article->url = "/sample-article";
$article->article_date = time();
$article->tags = ["lorem ipsum", "sample"];
$article->is_home = false;
$article->writer_id = $writer_id;
$article_id = $article->insert();

/**** Sample changing article data ****/
$article = new Article($article_id);
$article->tags[] = "new tag 1";
$article->tags[] = "new tag 2";
$article->is_home = true;
$writer->update(["tags", "is_home"]);

/**** Sample deleting article with id $article_id ****/
$article = new Article($article_id);
$article->delete();


/**** Sample reading POST or GET variables and changing article writer_id value
 **** for example https://example.com/action.php?id=3&writer_id=5
 ****/
// reading POST variable "id" and checking it's integer value
$id = Utility::getAnyInt("id");
// loading article with id $id
$article = new Article($id);
// if loaded id have value (fond in database) do some action
if ($article->id > 0) {
   
// reading POST variable "writer_id" and checking it's integer value
   
$writer_id = Utility::getAnyInt("writer_id");
    if (
$writer_id > 0) {
       
$writer = new Writer($writer_id);
        if (
$writer->id > 0) {
           
//if writer with id $writer_id is found, do some action
           
$article->writer_id = $writer_id;
           
$article->update(["writer_id"]);
        }
    }
}



Details

# PHP-Database-Tables-Class --------------------- OOP Database class for PHP MySQL connection and insert, update, delete and select query for easier manipulating with tables ### CONFIG File All database configurations are stored in seperate config.php file, which can be kept off document root. ### TABLE CLASSES Whole idea is to create separate class for each table in the database and to manipulate data with ease. e.g. **class Article extends MySqlHelper {** **public $id = 0;** **public $title = "";** **public function __construct(int $id = 0) {** **$this->table = "articles";** **$this->primaryKeyColumn = "id";** **$this->columns = ["id" => MySqlHelper::type_int,"title" => MySqlHelper::type_str];** **$this->loadRegular($id);** **}** **}** ### LOADING SINGLE ROW FROM A TABLE AND ASSIGN VALUES e.g: **$article = new Article(3);** this will load row with id 3 in article class and convert all values appropriately. $article->id and $article->title in this case will be set **$article->title = "New title";** //changing class value **$article->update();** //updating article class values to table or **$article->update(["title"]);** //updating just title value ### CUSTOM DATABASE CONNECTION AND QUERIES, IF NEEDED Database class loads configuration file and establishes a MySQLi connect in constructor so just creating an instance of class connects you with database. e.g: **$db = new MySql();** // would connect to database There are plenty of query types, reading whole tables, single rows, array of single column values, updating and deleting rows with or without where clause, counting row numbers, etc. e.g: **$db->insert("tablename",["column1","column2","column3"],[$value1,$value2,$value3]);** //inserting values in table **$db->update("tablename",["column1","column2"],[$newvalue1,$newvalue2],$id);** // updating two columns to row with primar key value $id **$db->updateWhere("tablename",["column1"],[$newvalue1],"id>?",[3]);** // updating column `column1` with value $newvalue1 to rows with `id` greater than 3 **$db->deleteWhere("tablename","id>?",[3]);** // deleting all rows with `id` greater than 3 Examples of Use: --------------- index.php has a sample loading & editing values from tables, as well as reading POST and GET variables and assigning values to database.

  Files folder image Files  
File Role Description
Files folder imagevojjin (2 files, 3 directories)
Accessible without login Plain text file README.md Doc. Readme

  Files folder image Files  /  vojjin  
File Role Description
Files folder imagemysql (2 files)
Files folder imagetables (2 files)
Files folder imageutilities (2 files)
  Accessible without login Plain text file config.php Conf. DB configuration and spl loader
  Accessible without login Plain text file index.php Example index file with samples

  Files folder image Files  /  vojjin  /  mysql  
File Role Description
  Plain text file MySql.php Class MySql manage class
  Plain text file MySqlHelper.php Class helper for MySql

  Files folder image Files  /  vojjin  /  tables  
File Role Description
  Plain text file Article.php Class sample Article table class
  Plain text file Writer.php Class sample table class

  Files folder image Files  /  vojjin  /  utilities  
File Role Description
  Plain text file DateClass.php Class date time manipulation class
  Plain text file Utility.php Class simple utilities class

 Version Control Unique User Downloads Download Rankings  
 0%
Total:114
This week:3
All time:9,571
This week:42Up