1) databse code (sql)
the database code in SQL.
Code:
1 CREATE TABLE `snippet` (
2 `id` INT( 12 ) NOT NULL AUTO_INCREMENT ,
3 `snipname` TEXT NOT NULL ,
4 `snipdesc` TEXT NOT NULL ,
5 `snippet` TEXT NOT NULL ,
6 `date` TEXT NOT NULL ,
7 `author` TEXT NOT NULL ,
8 PRIMARY KEY ( `id` )
9 ) TYPE = MYISAM ;
10
the database code in PHP.2 `id` INT( 12 ) NOT NULL AUTO_INCREMENT ,
3 `snipname` TEXT NOT NULL ,
4 `snipdesc` TEXT NOT NULL ,
5 `snippet` TEXT NOT NULL ,
6 `date` TEXT NOT NULL ,
7 `author` TEXT NOT NULL ,
8 PRIMARY KEY ( `id` )
9 ) TYPE = MYISAM ;
10
Code:
1 $sql = 'CREATE TABLE `snippet` ('
2 ********. ' `id` INT(12) NOT NULL AUTO_INCREMENT, '
3 ********. ' `snipname` TEXT NOT NULL, '
4 ********. ' `snipdesc` TEXT NOT NULL, '
5 ********. ' `snippet` TEXT NOT NULL, '
6 ********. ' `date` TEXT NOT NULL, '
7 ********. ' `author` TEXT NOT NULL,'
8 ********. ' PRIMARY KEY (`id`)'
9 ********. ' )'
10 ********. ' TYPE = myisam';
11
this will be a bare bones snippet library (like a shell) with search functions to replace the lack of categories. i do not like to use categories because you have to constantly switch pages, blah, blah. so i usually integrate search functions into database script because its much easier in a user oriented website to LET the user decided what they want 2 ********. ' `id` INT(12) NOT NULL AUTO_INCREMENT, '
3 ********. ' `snipname` TEXT NOT NULL, '
4 ********. ' `snipdesc` TEXT NOT NULL, '
5 ********. ' `snippet` TEXT NOT NULL, '
6 ********. ' `date` TEXT NOT NULL, '
7 ********. ' `author` TEXT NOT NULL,'
8 ********. ' PRIMARY KEY (`id`)'
9 ********. ' )'
10 ********. ' TYPE = myisam';
11

this will include a backend WITH NO AUTHENTICATI ON, to submit new snippets. I WILL NOT PROVIDE STRIPPING OF CHARATCERS suchas removing whitespaces and special characters since i am writing this as a reference and to produce tutorials and code that cant be found in many places. so before we begin i would like to say that this code is free since one day you could code something like this on your own

2) submitting data
before we can do anything we need to build a backend to submit the snippets. so this will be a basic 2 script process. we will use a regular HTML page and a php page to process the information into the database. for this you will need to know basic HTML form tags, and how to process the name and values. of course im going to generate a basic one for this tutorial, and here it is. (copy&paste)
Code:
1 [ submit.html ]
2 <form method=post action=snipprocess.php>
3 <input type=text name=snipname> Snippet Name<bR>
4 <input type=text name=snipdesc>Snippet Description<br>
5 snippet<br>
6 <textarea name=snippet></textarea><br>
7 <input type=text name=date>Date<bR>
8 <input type=text name=author>Author<bR>
9 <input type=submit value=submit> <input type=reset value=reset>
10 </form>
11 [ /submit.html ]
12
its important to always know what fields you have entered into the table so you can insert them properly. when we insert the information we are going to pass the field values to the php script in plain text. submitting plain text to the database is very unstable and you should always follow proper sanitation before processing anything to your database.2 <form method=post action=snipprocess.php>
3 <input type=text name=snipname> Snippet Name<bR>
4 <input type=text name=snipdesc>Snippet Description<br>
5 snippet<br>
6 <textarea name=snippet></textarea><br>
7 <input type=text name=date>Date<bR>
8 <input type=text name=author>Author<bR>
9 <input type=submit value=submit> <input type=reset value=reset>
10 </form>
11 [ /submit.html ]
12
now we are going to construct the php page and i will break it down first, then provide the full code. Im going to first use the POST values to retrieve the form fields. so...
Code:
1 $snipname = $_POST['snipname'];
2 // $snipname is the variable that will hold our form field value
3 // for the snippet name. since we named the field 'snipname'
4 // we specify the value to grab in the $_POST['FIELD_NAME_HERE];
5
which we will do for evey value we specified except the id field which was set to 'auto_increme nt'. if the field is set to 'auto_increme nt' it will add 1 to the previous id number so it can keep track of how many dbase entries you have made in that table. i ALWAYS USE THE ID FIELD WITH AUTO_INCREME NT SO I NEVER LOSE ANYTHING, AND KNOW WHAT ORDER I ENTERED IT. i can't stress it enough to add this since it will come in handy, it will eliminate many other code you might have to throw in there to know when or where you entered something. so, now that the ID rant is over; let's continue.2 // $snipname is the variable that will hold our form field value
3 // for the snippet name. since we named the field 'snipname'
4 // we specify the value to grab in the $_POST['FIELD_NAME_HERE];
5
the next thing we are going to do is use the mysql functions in php to enter the data. this can be tricky for some people since it takes a tedious eye to make sure you dont get sytax errors (which suck balls). we are going to use the mysql_query function....
Code:
1 mysql_query("insert into master (id,snipname,snipdesc,snippet,date,author) values('','$snipname','$snipdesc','$snippet','$date','$author')");
2
first we send where to insert it, then we define what to insert. you can see why sytax errors may be a problem. its always a biotch. so, so far we have a backend page (submit.html), and a process page. at the end of processing it, the page will just sit there so instead of a redirect, i always just put some link back to the main page, or submit page. in this case i used the submit page, so here is the code i used...2
echo "back to <a href=submit.php>snippet library</a>";
one thing i have not covered was connecting to the database. you should already know how to do this and if you dont, look it up. lol im to lazy to go back and add it

Code:
1 [ snipprocess.php ]
2 <?php
3 $snipname = $_POST['snipname'];
4 $snipdesc = $_POST['snipdesc'];
5 $snippet = $_POST['snippet'];
6 $date = $_POST['date'];
7 $author = $_POST['author'];
8
9 mysql_query("insert into snippet (id,snipname,snipdesc,snippet,date,author) values('','$snipname','$snipdesc','$snippet','$date','$author')") or die(mysql_error());
10 echo "back to <a href=submit.html>snippet library</a>";
11 ?>
12 [/ snipprocess.php ]
13
3) displaying the data.2 <?php
3 $snipname = $_POST['snipname'];
4 $snipdesc = $_POST['snipdesc'];
5 $snippet = $_POST['snippet'];
6 $date = $_POST['date'];
7 $author = $_POST['author'];
8
9 mysql_query("insert into snippet (id,snipname,snipdesc,snippet,date,author) values('','$snipname','$snipdesc','$snippet','$date','$author')") or die(mysql_error());
10 echo "back to <a href=submit.html>snippet library</a>";
11 ?>
12 [/ snipprocess.php ]
13
displaying the data requires a while loop (or so i used) to pull the values from the table. first we need to setup the 'mysql_query' function in the php script to specify how to pull the data from the database. we are going to make the dataabase call a variable to handle it easier.
Code:
1 $result = mysql_query("SELECT * FROM snippets");
2
that will pull ALL the entries, hence the asterick meaning "all". but i want to change the order so that it displays by the id, which will display the last one entered first. you can specify alphabetical ly orother ways, but i want the FIFO to apply (first in first out) so im gonna make the sql query look like....2
$result = mysql_query("SELECT * FROM snippets ORDERBY id");
now that that is out of the way we can work on the while loop to pull them from the database. while loops aren't that hard to understand since its a 'set' function. but you do need to know that when specifying rows to pull from you will need to use the $row[''] function. you'll see what i mean. i always use $row, just to remind me what im pulling, its just easier.
Code:
1 while($row = mysql_fetch_array($result)) {
2 echo "name: $row['snipname']";
3 }
4
this will return all the snippent names that are in the database because we told it to "select all from the table snippets and order them by id". to display them individully you would use the same $row function but substitute the row name for whatever row you want. i also used a mysql_fetch_ array to get them all to my page, which is just about what you'll need everytime you pull them, well, when they are in an array like that.2 echo "name: $row['snipname']";
3 }
4
you will also see that i added tables to make the output more pleasurable on the eyes.
Code:
1 [ display.php ]
2 <?php
3 $result = mysql_query("SELECT * FROM snippet order by id desc");
4
5 echo "<table><tr><tD><b>name</b></tD><tD><b>description</b></td><tD><b>date</b></td><td><b>author</b></td></tR>";
6 while($row = @mysql_fetch_array($result)) {
7 echo "<tr><td>$row[snipname]</tD><td>$row[snipdesc]</tD><Td>$row[date]</tD><td>$row[author]</tD></tr>";
8 }
9 echo "</table>";
10 ?>
11 [/ display.php ]
12
2 <?php
3 $result = mysql_query("SELECT * FROM snippet order by id desc");
4
5 echo "<table><tr><tD><b>name</b></tD><tD><b>description</b></td><tD><b>date</b></td><td><b>author</b></td></tR>";
6 while($row = @mysql_fetch_array($result)) {
7 echo "<tr><td>$row[snipname]</tD><td>$row[snipdesc]</tD><Td>$row[date]</tD><td>$row[author]</tD></tr>";
8 }
9 echo "</table>";
10 ?>
11 [/ display.php ]
12
that should be it. if something is missing, or if you need help, dont hesitate to ask.
-- hostile graphics // sunjester
discuss this topic to forum
