Using the tutorial that jonno946 suggested in his first pop at oop thread, I finally learned how to do objects and classes and all that cool stuff!
So I wrote a class to do what I had had a whole page doing. The tutorials section on my site is written in XML, which I then parse with PHP. (It makes it a ton easier to write the individual pages 🙂) Anyway, I had had a page called tutorials.php that would be called on and would parse stuff, but then if something else had to be parsed I needed a new page. So. . . .enter classes. Here's the class I wrote for my parsing thingy (it's not too fancy, and not really very well done, but oh well)
<?php
class Tutorial {
function index() {
?>
<ul>
<li><a href="?id=tutorials&lang=php">PHP (1)</a></li>
</ul>
<?php
}
function load_lang() {
$lang= $_GET['lang'];
echo "<h3 class=\"sub\">{$lang}</h3>";
if($lang == "php") { ?>
<ul>
<li><a href="?id=tutorials&lang=php&t=mail">Mailing with PHP</a></li>
</ul>
<?php
}
}
function view_tutorial() {
$lang = $_GET['lang'];
$t = $_GET['t'];
if(isset($_GET['t'])) {
if(isset($_GET['part'])) {
$file_name = "includes/tutorials/{$lang}/{$t}/".$_GET['part'].".xml";
}
else {
$file_name = "includes/tutorials/{$lang}/{$t}/index.xml";
}
function startElement($parser, $name, $attrs) {
switch($name) {
case 'PAGE';
break;
case 'PART':
echo "<h3 class=\"sub\">";
break;
case 'TITLE':
echo "<span style=\"text-align:left;\">";
break;
case 'AUTHOR':
echo " <i>by ";
break;
case 'HEAD':
echo "<h2>";
break;
case 'PARA':
echo "<p>";
break;
case "PCENT":
echo "<p style=\"text-align:center\">";
case "BR":
echo "<br />";
break;
case "CODE":
echo "<b>";
break;
case "LINK":
echo "<a href=\"";
break;
case "LTXT":
echo '';
break;
}
}
function endElement($parser, $name) {
switch($name) {
case 'PAGE':
break;
case 'PART':
echo "</h3>";
break;
case 'TITLE':
echo "</span>";
break;
case 'AUTHOR':
echo "</i></span>";
break;
case 'HEAD':
echo "</h2>";
break;
case "PARA":
echo "</p>";
break;
case "PCENT":
echo "</p>";
break;
case "CODE":
echo "</b>";
break;
case "LINK":
echo "</a>";
break;
case "LTXT":
echo "\">";
break;
}
}
function characterData($parser, $value) {
$text = trim($value, "\t");
$text = trim($text, "\n");
$text = trim($text, "\r");
(text)
echo $text;
}
$parser = xml_parser_create();
xml_set_element_handler($parser, 'startElement', 'endElement');
xml_set_character_data_handler($parser, 'characterData');
if ($file = fopen($file_name, 'r')) {
while ($data = fgets($file, 4096)) {
if (!xml_parse($parser, $data, feof($file))) {
die('<p><b>XML parsing error.</b></p>');
}
}
fclose($file);
} else {
die('<p>Could not open file <b>'.$file_name.'</b>.</p>');
}
xml_parser_free($parser);
}
}
}
?>
If you want more, here's the navigation class that I wrote to handle navigation through GET variables (and it utilizes the xml parser class:
<?php
class Nav {
var $id,
$lang,
$t,
$part,
$s,
$program;
function navbar() {
$this->id = $_GET['id'];
if($this->id == "about") {
}
if($this->id == "affiliates") {
}
if($this->id == "tutorials") { ?>
<h3>Tutorial Navigation</h3>
<a href="?id=tutorials&lang=php">PHP</a>
<?php
}
if($this->id == "scripts") { ?>
<h3>Script Navigation</h3>
<a href="?id=scripts&lang=php">PHP Scripts</a> <?php
}
if($this->id == "freeware") {
}
if($this->id == "contact") {
}
}
function index() {
?>
<div id="blog">
<h2>» news</h2>
<ul>
<li>» <a href="">12-19-04</a> -- I've gotten the basic code working for the new tutorial displayer. This way I can use XML to write the tutorials (making it a LOT easier!!) and use the SAX PHP functions to display it as XHTML. Just a couple of quirks to work out, but then it will be up!</li>
<li>» <a href="">12-18-04</a> -- I decided to use this design as opposed to the other one because of two reasons. One, because this utilizes divs and css as opposed to tables and css. Because I design using divs and css I'm more familiar with it.
</ul>
<?php
}
function about() {
echo "<div id=\"main\">";
echo "<h2>» about</h2>";
include("includes/about.php");
}
function aff() {
echo "<div id=\"main\">";
echo "<h2>» affiliates</h2>";
include("includes/affiliates.php");
}
function tutorials() {
echo "<div id=\"main\">";
echo "<h2>» tutorials</h2>";
include "includes/tclass.php";
$tut = new Tutorial();
if(!isset($_GET['t'])) {
if(!isset($_GET['lang'])) {
$tut->index();
}
else {
$tut->load_lang();
}
}
else{
$tut->view_tutorial();
}
}
function scripts() {
echo "<div id=\"main\">";
echo "<h2>» scripts</h2>";
function index() {
?>
<ul>
<li><a href="?id=scripts&lang=php">PHP (1)</a></li>
</ul>
<?php
}
function load_lang() {
$lang= $_GET['lang'];
echo "<h3 class=\"sub\">{$lang}</h3>";
if($lang == "php") { ?>
<ul>
<li><a href="?id=scripts&lang=php&script=htmltrans">HTMLTransformer</a></li>
</ul>
<?php
}
}
function view_script() {
$lang = $_GET['lang'];
$script = $_GET['script'];
$dir = "scripts/{$lang}/{$script}";
include("includes/{$dir}/index.php");
}
if(!isset($_GET['script'])) {
if(!isset($_GET['lang'])) {
index();
}
else {
load_lang();
}
}
else{
view_script();
}
}
function freeware() {
echo "<div id=\"main\">";
echo "<h2>» freeware articles</h2>";
echo "<p>These are all my freeware articles that I've written. I write one each week generally, and they review up to five freeware programs of the same type (word processors, ftp programs, e-mail clients, text editors, etc.)</p>";
}
function contact() {
echo "<div id=\"main\">";
echo "<h2>» contact</h2>";
$send = $_GET['mail'];
if($send == "form") { ?>
<form method="post" action="index.php?p=contact&mail=send">
<input type="text" name="name" value="Full Name" /><br />
<input type="text" name="email" value="E-mail Address" /><br />
<input type="text" name="website" value="Website" /><br />
<textarea name="msg">Message</textarea>
<input type="submit" value="Send" />
</form>
<?php
}
$send = $_GET['mail'];
if($send == "send") {
$name = $_GET['name'];
$email = $_GET['email'];
$msg = $_GET['msg'];
$web = $_GET['web'];
$msg = "{$name} has sent you a message!\n\nCONTACT INFO\n-----------------------\nName: {$name}\nE-mail: $email\nWeb: $web\n------------------\n$msg";
$email2 = "aaron@artlangs.com";
$subject = "Form Submission on CodeZilla";
mail($email2, $subject, $msg, "From: $email");
echo("Thank you for contacting CodeZilla. We will get back to you as soon as possible.");
}
}
}
?>