PHP Classes

File: positions.example.php

Recommend this page to a friend!
  Classes of Vladimir S. Bredihin   Position's Records   positions.example.php   Download  
File: positions.example.php
Role: Example script
Content type: text/plain
Description: positions example
Class: Position's Records
Manage the positions of MySQL table record tree
Author: By
Last change: delete row
print $table;
Date: 17 years ago
Size: 3,022 bytes
 

Contents

Class file image Download
<?php
require_once('positions.class.php');

global
$_POST; //For order :)

//Config DB connect;
$host = 'localhost';
$login = 'banners';
$password = '';
$db = 'banners';
//Connect
if (!mysql_connect($host, $login, $password))
    die (
'Not connect db: '.mysql_error());
elseif (!
$this->link = mysql_select_db($db))
    die (
'Not selected db: '.mysql_error ());

   
/*****************CREATE EXAMPLE DATA****************************/
//Create and fill test table "t1"
@mysql_query ("CREATE TABLE t1 (
                                id int NOT NULL auto_increment,
                                parent int default '0',
                                value varchar (255),
                                position int default '1',
                                primary key (id)
                                )"
);
//if empty table then fill
$result = mysql_query('SELECT id FROM t1 LIMIT 0,1');
if (
mysql_num_rows($result) == 0)
{
   
mysql_query ("INSERT t1 SET value='Pic3'");
   
mysql_query ("INSERT t1 SET value='Pic1'");
   
$insert_id = mysql_insert_id();
   
mysql_query ("INSERT t1 SET value='Pic2', parent='{$insert_id}'");
   
   
mysql_query ("INSERT t1 SET value='Article1'");
   
mysql_query ("INSERT t1 SET value='Article3', parent='{$insert_id}'");
   
mysql_query ("INSERT t1 SET value='Article2'");
}
/*****************END CREATE EXAMPLE DATA************************/
/*********************EXAMLE USE CLASS***************************/
$table = 't1'; //this parameter is table name
$parent = '0'; //this parameter is id parent record (default null)
$id = '1'; //this parameter is id current record (default null) (In example id of record 'Pic3');
$fieldName = 'value'; //this parametr is field name in table labels' records (default 'name')
$fieldParent = 'parent'; //this parametr is is field name in table parents' records (default 'parent')

$positions = &new Positions($table, $parent, $id, $fieldName, $fieldParent);

if (!empty (
$_POST))
{
   
$positions->modify($_POST['position']);
}

$positionsList = $positions->lst (); //this function have 2 parameters - $table and $parent;
                                    //if with calling function parameters not set then function use parameters $table and $parent seted on constroctor

//create select positions
print "<form action='{$_SERVER['REQUEST_URI']}' method='post'>".sampleSelect ('position', $positionsList, $id)
        .
"<input type='submit' /></form>";
   
   
$out = null;
$result = mysql_query ('SELECT * FROM t1 ORDER BY parent, position');
while (
$row = mysql_fetch_assoc($result))
{
    if (empty(
$out))
    {
       
$keys = array_keys($row);
       
$out .= '<tr><td><b>'.implode ('</b></td><td><b>', $keys).'</b></td></tr>';
    }
   
$out .= '<tr><td>'.implode ('</td><td>', $row).'</td></tr>';
}
if (!empty(
$out)) print '<table border=\'1\'>'.$out.'</table>';



function
sampleSelect($name, $data, $active)
{
   
$select = null;
    foreach (
$data as $id=>$value)
    {
        if (
$id == $active) $selected = ' selected'; else $selected = null;
       
$select .= "<option value='{$id}'{$selected}>{$value}</option>";
    }
    return
"<select name='{$name}'>{$select}</select>";
}
?>