<?
/*
* *********************************
* PHP Code Snippet Library V0.1
* *********************************
*
* *********************************
* Author: Stuart Cochrane
* Email: stuartc1@hotmail.com
* Date: 23-07-2002
* Usage: All comments must remain
* License: GNU Genral Public License
* Message:
* This is my first Open Source
* script in PHP - so any comments are
* very welcome :-)
* **********************************
*
* **********************************
* Description:
* A very basic code snippets library
* designed to hold commonly used code.
* A great way to keep track of your
* handy code snippets, functions and
* classes.
* **********************************
*
* **********************************
* Requirements:
* PHP and MySQL
* Tested on Windows NT4 / IIS 4 / PHP 4.1.2
* with standard configuration -
* but should run on all Platforms.
*
* **********************************
* Configuration:
* Change the mysql database connections
* to match yours - see function con()
* Change the mysql database name
* to match yours - see function sel()
*
* *********************************
* Below is the MySQL dump
* needed to run this app
* *********************************
#
# Table structure for table 'snips'
#
CREATE TABLE `snips` (
`ID` bigint(5) unsigned NOT NULL auto_increment,
`Title` varchar(250) default NULL,
`Body` blob,
`Description` blob,
UNIQUE KEY `ID` (`ID`)
) TYPE=MyISAM;
#
# Dumping data for table 'snips'
#
INSERT INTO snips VALUES("12","Test Snippet",
"echo \"This is the test snippet...\";",
"just a test to get started with.");
*/
// database connection
function con() {
return mysql_connect("localhost", "root");
}
// database selection
function sel() {
return mysql_select_db("snippets");
}
// display the code in colour
function dis($body) {
$h_string = "<"."?\n\n".$body."\n\n?".">";
return highlight_string("$h_string");
}
// retrieve the code from the database
function hs($view) {
$db = con();
sel();
$snip_sql = "SELECT Body FROM snips WHERE ID = $view";
$snip_qry = mysql_query($snip_sql, $db);
while ($snip = mysql_fetch_array($snip_qry)) {
dis($snip["Body"]);
}
}
// retrieve the code description from the database
function hsd($view) {
$db = con();
sel();
$snip_sql = "SELECT Description FROM snips WHERE ID = $view";
$snip_qry = mysql_query($snip_sql, $db);
while ($snip = mysql_fetch_array($snip_qry)) {
echo $snip["Description"];
}
}
// retrieve and display the code titles
function title($delete = "no") {
$db = con();
sel();
$snip_sql = "SELECT ID, Title FROM snips ORDER BY Title";
$snip_qry = mysql_query($snip_sql, $db);
while ($snip = mysql_fetch_array($snip_qry)) {
$title = $snip["Title"];
$id = $snip["ID"];
if ($delete == "no") {
echo "<a href=\"snip.php?view=$id\">$title</a><br>";
} else {
echo "<a href=\"snip.php?
opt=del&id=$id\">$title</a><br>";
}
}
}
// add a snippet to the database
function asnip($title,$code,$description = "none") {
$db = con();
sel();
$snip_sql = " INSERT INTO snips (Title, Body, Description)
VALUES
('$title', '$code', '$description')";
if (! mysql_query($snip_sql)){
echo mysql_error();
die;
} else { return true; }
}
// delete snippet from the database
function dsnip($id) {
$db = con();
sel();
$snip_sql = " DELETE FROM snips
WHERE ID = $id";
if (! mysql_query($snip_sql)){
echo mysql_error();
die;
} else { return true; }
}
// build the start of a table
function tstart($class, $width) {
if($class == 1) {
echo '<table class="table1" width="'.$width.'" cellpadding="2">
<tr>
<td>';
} else {
echo '<table class="table2" width="'.$width.'"
cellpadding="2">
<tr>
<td>';
}
}
// build the end of a table
function tend() {
echo '</td>
</tr>
</table>';
}
// set the header vars
$appname= '<b>PHP-CSL</b>';
$appversion = '<i>PHP Code Snippet Library V0.1</i>';
// get the diplay type
if (isset($opt)) {
if ($opt == "add") {
$DoThis = "add";
} elseif ($opt == "del") {
$DoThis = "del";
}
} else {
$DoThis = "snippets";
}
?>