File: vojjin/index.php

Recommend this page to a friend!
  Classes of Vojin Petrovic  >  PHP Database Tables Class  >  vojjin/index.php  >  Download  
File: vojjin/index.php
Role: Example script
Content type: text/plain
Description: index file with samples
Class: PHP Database Tables Class
Store and retrieve class objects in database table
Author: By
Last change:
Date: 13 days ago
Size: 2,210 bytes
 

 

Contents

Class file image Download
<?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"]);
        }
    }
}



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