PHP Classes

File: test.php

Recommend this page to a friend!
  Classes of Gonzalo Chumillas   DbTable   test.php   Download  
File: test.php
Role: Example script
Content type: text/plain
Description: Example script
Class: DbTable
Update table records in multiple related tables
Author: By
Last change:
Date: 9 years ago
Size: 1,666 bytes
 

Contents

Class file image Download
<?php
header
("Content-type: text/plain; charset=UTF-8");
require_once
"classes/autoload.php";
use
com\soloproyectos\common\db\DbConnector;
use
com\soloproyectos\common\db\DbDataSource;
use
com\soloproyectos\common\db\DbTable;

$db = new DbConnector("database", "username", "password");

// updates the record "content.id = 101"
$t = new DbTable($db, "content", 101);
$t->set("description", "Record description...");
$t->set("image.path", "/path/to/image.jpg");
$t->set("video.path", "/path/to/video.mp4");
$t->set("file.path", "/path/to/file.pdf");
$t->update();

// inserts a new record
$t = new DbTable($db, "content");
$t->set("description", "Record description...");
$t->set("image.path", "/path/to/image.jpg");
$t->set("video.path", "/path/to/video.mp4");
$t->set("file.path", "/path/to/file.pdf");
$t->insert();

// deletes the record "content.id = 101"
$t = new DbTable($db, "content", 101);
$t->delete();

// prints info from the record "content.id = 101"
$t = new DbTable($db, "content", 101);
$t->get("description");
$t->get("image.path");
$t->get("video.path");
$t->get("file.path");

// loops through the records of the table "content"
$t = new DbTable($db, "content");
$t->setOrder("id desc");
$t->setFilter("section_id = 'blah'");
foreach (
$t as $row) {
   
// updates the record
   
$row->set("description", "New description");
   
$row->set("image.path", "/path/to/new/image.jpg");
   
$row->update();
   
   
// prints some info
   
echo "Description: " . $row->get("description") . "\n";
    echo
"Image: " . $row->get("image.path") . "\n";
    echo
"Video: " . $row->get("video.path") . "\n";
    echo
"File: " . $row->get("file.path") . "\n";
}