PHP Classes

File: 01_mysql_listing_sample.php

Recommend this page to a friend!
  Classes of J.B.   Simple Object   01_mysql_listing_sample.php   Download  
File: 01_mysql_listing_sample.php
Role: Example script
Content type: text/plain
Description: SimpleObject in use: list MySQL table records in three lines of code
Class: Simple Object
Base class with common variable access functions
Author: By
Last change: Changed require_once('mysqlresultiterator.php'); to require_once('simplemysqlresultiterator.php');
Date: 20 years ago
Size: 1,621 bytes
 

Contents

Class file image Download
<?php
   
# This script shows how use of SimpleObject and its child classes
    # may ease your programming
   
    # TASK 1 - List all mysql table records
   
    # include all required classes
   
require_once('simpleobject.php');
    require_once(
'simpleiterator.php');
    require_once(
'simplemysqlresultiterator.php');
    require_once(
'simpletemplate.php');
    require_once(
'listtemplate.php');
   
   
# Connect to mysql
   
mysql_connect('localhost','root','');
   
mysql_select_db('dbname');
   
   
# Write down your SQL query:
   
$query = "SELECT * FROM my_table";
   
   
# Create an instance of list template:
    # Send an instance of MysqlResultIterator as argument
   
$list_tpl =& new ListTemplate(new SimpleMysqlResultIterator($query));
   
   
# Set title (variable in template) to list
   
$list_tpl->set('list_title','What a Simple List!');
   
   
# Display the list of items:
   
$list_tpl->display('list_template.html','item_template.html');
   
   
# That's it!
   
    # Don't forget to make list_template.html and item_template.html
    # files such that they have markers like <!--$field_name--> in
    # item_template.html and <!--$list--> and <!--$list_title-->
    # in list_template.html. <!--$list--> will be replaced by output
    # of item listing:
   
    /*
        list_template.html:
       
        <h1><!--$list_title--></h1>
        <p>Here are the records from table:</p>
        <table>
        <tr>
            <th>record id</th>
            <th>record title</th>
        </tr>
        <!--$list-->
        </table>
       
       
        item_template.html:
       
        <tr>
            <td><!--$id--></td>
            <td><!--$title--></td>
        </tr>
       
       
        !! modify id and title to names of fields in your mysql table
    */
?>