PHP Classes
elePHPant
Icontem

File: sample.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Han Jun Kwang  >  MySqueaks  >  sample.php  >  Download  
File: sample.php
Role: Example script
Content type: text/plain
Description: Sample code
Class: MySqueaks
Manipulate MySQL database tables
Author: By
Last change:
Date: 8 years ago
Size: 4,522 bytes
 

Contents

Class file image Download
<?php
   
/*
        MySqueaks sample code
        As this code will write to your database,
        please make sure that the database and table name are valid.
        you may also *not* want to publish this online,
        although database operations are only limited to truncating
        and inserting 12 rows every time.
    */
   
if (!isset($_GET["rss"])) :
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <style type="text/css">
        #results table {
            border: solid 2px #000000;
            border-collapse: collpase;
            background-color: #fafafa;
        }
        #results td {
            border: solid 1px #999999;
        }
    </style>
    <title>MySqueaks sample code</title>
</head>
<body>
<pre>
<?php
   
else :
   
header('Content-Type: application/xml; charset=iso-8859-1');
    echo
'<?xml version="1.0" encoding="iso-8859-1"?>';
?>
<rss version="2.0">
    <channel>
        <title>MySqueaks sample code</title>
        <link>http://mysqueaks.ikueb.com</link>
        <language>en-us</language>
        <pubDate><?php echo date("r"); ?></pubDate>
        <lastBuildDate><?php echo date("r"); ?></lastBuildDate>
        <generator>MySqueaks</generator>
        <description>
<?php
   
endif;

include(
"class_sql.php");
// initialisation
$book = new MySqueaks(array("localhost", "root", ""), "sqx_sample", "book");
if (
$book->Status === null) {
   
// new table
   
$book->PKI("book_id");
   
$book->TypeText("book_title");
   
$book->TypeText("book_publisher");
   
$book->Submit();
    echo
"Book table created.\n";
} else {
   
$book->Truncate();
    echo
"Book table truncated.\n";
}
$user = new MySqueaks(array("localhost", "root", ""), "sqx_sample", "user");
if (
$user->Status === null) {
   
$user->PKI("user_id");
   
$user->TypeText("user_name");
   
$user->TypeInteger("user_contact");
   
$user->Submit();
    echo
"User table created.\n";
} else {
   
$user->Truncate();
    echo
"User table truncated.\n";
}
$record = new MySqueaks(array("localhost", "root", ""), "sqx_sample", "rec");
if (
$record->Status === null) {
   
$record->PKI("record_id");
   
$record->TypeChrono("record_date", SQX_DATE);
   
$record->TypePrecision("record_price");
   
$record->TypeInteger("record_user");
   
$record->TypeInteger("record_book");
   
$record->Submit();
    echo
"Record table created.\n";
} else {
   
$record->Truncate();
    echo
"Record table truncated.\n";
}

// inserting sample data
$book->Values();
$book->Set(array(null, "Book 1", "ABC Publisher"));
$book->Set(array(null, "Book 2", "HJK Publisher"));
$book->Set(array(null, "Book 3", "XYZ Publisher"));
$book->Set(array(null, "Book 4", "SQX Publisher"));
$book->Submit();
echo
"Inserting data for Book table done.\n";
$user->Values();
$user->Set(array(null, "Aaron", "12345"));
$user->Set(array(null, "Betty", "67890"));
$user->Set(array(null, "Charlie", "13579"));
$user->Set(array(null, "Diana", "24680"));
$user->Submit();
echo
"Inserting data for User table done.\n";
$record->Values();
$record->Set(array(null, "2007-01-01", 34.20, 1, 1));
$record->Set(array(null, "2007-01-01", 24.50, 2, 2));
$record->Set(array(null, "2007-02-01", 40.10, 3, 3));
$record->Set(array(null, "2007-03-02", 14.90, 4, 4));
$record->Submit();
echo
"Inserting data for Record table done.\n";

// retrieve records
$book->Join("book_id");
$book->Join($record->JoinTo("record_book", "record_user"));
$book->Join($user->JoinTo("user_id"));
$book->GreaterEquals("and", "record_price", 25);
$book->Like("or", "user_contact", "68", SQX_SKIP, SQX_SKIP, SQX_WILD_BOTH);
if (!isset(
$_GET["rss"])) {
    echo
$book->toString(true)."</pre>\n";
    echo
$book->Tabulate("results");
    echo
"</body>\n</html>\n";
} else {
   
// giving aliases to column name to reflect the RSS tag structure
   
$book->Alias("book_title", "title");
   
$book->Alias("book_id", "guid");
   
$book->Alias("DATE_FORMAT(`record_date`, '%a, %d %M %Y %r')", "pubDate");
   
$book->Alias("`book_publisher`", "description");
   
$book->Alias("CONCAT(`user_name`, '@', `user_contact`, '.com')", "author");
   
$book->Alias("'http://mysqueaks.ikueb.com'", "link");
   
$book->Select(array("title", "guid", "pubDate", "description", "link",
                       
"author"));
    echo
$book->toString()."\n\t\t</description>\n";
   
// note that $sematic = true, so we have:
    // <title>Book 1</title>
    // instead of:
    // <field name="title">Book 1</field>
    // we've also changed the $wrapper default "result" to "item" for RSS usage
   
echo $book->XML(1, true, "item");
    echo
"\t</channel>\n</rss>\n";
}
?>