PHP Classes

File: example_products.php

Recommend this page to a friend!
  Classes of Thiemo Kreuz   TM::MyCSV   example_products.php   Download  
File: example_products.php
Role: Example script
Content type: text/plain
Description: How to create and sort tables, insert and delete rows
Class: TM::MyCSV
Manage CSV files like database tables
Author: By
Last change: sort() accepts SORT_LOCALE_STRING now.
Date: 14 years ago
Size: 5,838 bytes


Class file image Download

 * This is a complex example script for the TM::MyCSV class. It shows how to
 * create and change a table, how to use insert(), delete() and sort().
 * @author Thiemo Mättig (

// The method sort() depends on your locale settings if you use the sorting type
setlocale(LC_ALL, "de_DE@euro", "de_DE", "deu_deu");

// Include the PHP class first.

// Read the file products.csv if exists. If you run the script for the first
// time, the write() call below will create the file if required.
$products = new MyCSV("products");

if (isset(
$_REQUEST['action']) && $_REQUEST['action'] == "delete")

// Check if something was submitted in the form.
if (!empty($_POST['name']))
// Add a new row to the table. If the file does not exist and the table
    // contains no fields, this call also creates the fields. Not that all
    // TM::MyCSV tables will contain an 'id' column. Every row you add to the
    // table will get a new ID (similar to AUTO_INCREMENT from MySQL).
'name' => stripslashes($_POST['name']),
'price' => stripslashes($_POST['price']),
// Save the whole table to disk.

// Check if the user clicked one of the table headers.
$sort = isset($_REQUEST['sort']) ? $_REQUEST['sort'] : "";
$desc = isset($_REQUEST['desc']) ? " DESC" : "";

// It's a very bad idea to use something that the user entered in your SQL
// statements. This leads to SQL insertions. We do a translation instead. All
// invalid values, hacking attempts and so on will fall back to the default
// behaviour.
switch ($sort)
// Order by name in ascending or descending order. Two products with the
        // same name will be ordered by price.
$products->sort("name SORT_LOCALE_STRING SORT_NULL $desc, price SORT_NUMERIC");
// Order by price using a numeric comparison. SORT_NULL moves products
        // without a price to the bottom.
$products->sort("price SORT_NUMERIC SORT_NULL $desc, name SORT_LOCALE_STRING");
// Ordered by ID ("unordered") by default.
if (!$desc)

// This is the end of the controller component of the script and the start of
// the view component. Check–view–controller
// if you don't know what MVC is. You don't need the frameworks mentioned there.
// The idea of MVC is simple: Prepare everything in the controller part (above
// these lines) and display everything in the view part (below these lines).
// Using code in the view part is not forbidden. However, it's forbidden to
// change data.

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<html lang="de" xml:lang="de" xmlns="">
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<title>TM::MyCSV Example Script</title>
<meta name="robots" content="none" />
<style type="text/css">

/* A little bit of CSS to make the example look nice. */

    color: #333;
    font-family: Verdana, sans-serif;
form div{
    margin: 0.5em 0;
    cursor: pointer;
    float: left;
    padding-right: 0.6em;
    text-align: right;
    width: 10em;
    border-collapse: collapse;
    border-spacing: 0;
    empty-cells: show;
th, td
    border-bottom:1px solid #CCC;
    padding: 0.4em 1em;
    text-align: left;
    vertical-align: top;
th, th a
    background: #666;
    color: #FFF;


<h1>TM::MyCSV Example Script</h1>

<!-- In most cases I use my TM::Apeform class to create such forms. In this
     case, I don't want another dependency in this example script. -->

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
        <legend>Add a new product</legend>
            <label for="name">Product name:</label>
            <input id="name" name="name" type="text" />
            <label for="price">Price:</label>
            <input id="price" name="price" type="text" />
            <input type="submit" value="Add new product" />

            <a href="<?php echo $_SERVER['PHP_SELF']?>?sort=name<?php
if ($sort == "name" && !$desc) echo '&amp;desc'?>">Product name</a><?php
if ($sort == "name") echo $desc ? '&#x25BC;' : '&#x25B2;'?>
            <a href="<?php echo $_SERVER['PHP_SELF']?>?sort=price<?php
if ($sort == "price" && !$desc) echo '&amp;desc'?>">Price</a><?php
if ($sort == "price") echo $desc ? '&#x25BC;' : '&#x25B2;'?>


// Sometimes it is easier to use echo statements instead of the mixed HTML and
// PHP syntax above. I use both depending on the amount of HTML code.

while ($product = $products->each())
'<td>' . htmlspecialchars($product['id']) . '</td>';
'<td>' . htmlspecialchars($product['name']) . '</td>';
    if (empty(
htmlspecialchars($product['price']) . ' &euro;';
'<td><a href="' . $_SERVER['PHP_SELF'] . '?action=delete&amp;id=' .
htmlspecialchars($product['id']) . '">Delete</a></td>';