Page 1 of 1

Database Retrival / Processing

Posted: Thu Jun 21, 2007 1:18 am
by ReDucTor
I've built many systems which have used PHP, and have coded systems in many different ways.

I have been debating on the easiest/cleanest way to display records (e.g. forum, news, blog, etc)

My plans are to make a framework which you can use very easily and rely on it to do things like:
  • Striping unsafe characters (e.g. escape db queries, escape html chars)
    Validation (e.g. email, url, integer, floating point)
    Generate output using Ajax + XHTML, XML, plain HTML (old browsers), and WAP/WML
    Database storage, retrival, manipulation
I've came up with many ideas, and am still trying to think of more.

My Plan is to design this using a top-down approach. (Design the implementation, then design the backend)

So what I'm asking everyone here, who has ideas for the good designs for doing this.

I'll post what I coded last night when I get home from work in about an hour.

Posted: Thu Jun 21, 2007 1:51 am
by ReDucTor
I know this is very basic..but just an example of one of the many ideas i have thought up

Code: Select all

<?php


function __autoload($classname) { require_once('includes/'.$classname.'.class.php'); }

class news {
	function __toString() {
		return '<div id="news_'.$this->id.'">'.$this->show().'</div>';
	}
	function show() {
		$str = '<h1>'.$this->title.'</h1> by <i>'.$this->author.'</i>';
		$str .= '[<a href"#" onclick="updateSection(\'news_id\',\'news.php?news_id='.$this->id.'&edit=1\');">Edit</a>]';
		$str .= '<br />';
		$str .= '<pre>'.$this->body.'</pre>';
		return $str;
	}

	function edit() {
		$str ='<form method="#" onsubmit="postPage(this,\'news.test.php\'); updateSection(\'news_id\',\'news.php?news_id='.$this->id.'&show=1\'); return false">';
		$str .= '<input type="hidden" name="id" value="'.$this->id.'">';
		$str .= '<input type="text" name="title" value="'.$this->title.'" size="60"> by <i>'.$this->author.'</i>';
		$str .= '<br />';
		$str .= '<textarea  rows="12" cols="60" name="body">'.$this->body.'</textarea><br />';
		$str .= '<input type="submit" name="action" value="Edit news">';
		$str .= '</form>';
	}
}

$db = new DB();
if($_POST['submit'] == 'Edit news') {
	$db->execute('UPDATE news SET
		title=\''.filter_input(INPUT_POST,'title',FILTER_SANITIZE_MAGIC_QUOTES).'\',
		body=\''.filter_input(INPUT_POST,'body',FILTER_SANITIZE_MAGIC_QUOTES).'\'
		WHERR `id`='.intval($_POST['news_id']));
		exit;
}
if($_GET['news_id'])
	$news = $db->execute('SELECT * FROM news WHERE news_id='.intval($_POST['news_id']));
else
	$news = $db->execute('SELECT * FROM news');
$news->handler('news');
foreach($news as $post) {
	if($_GET['edit'])
		print $post->edit();
	else if($_GET['show'])
		print $post->show();
	else
		print $post;
}

?>

Posted: Thu Jun 21, 2007 2:19 am
by staar2
nice but i would use templates, it is like more cleaner code. :roll:

Posted: Thu Jun 21, 2007 2:39 am
by ReDucTor
My goal is to eliminate or minimise the need for the code to contain too much html, but to let that be generated by the backend/framework.

So the code its self excluding the framework, contains no html, but can be told to output things (note my example above doesnt fit into this)

The reason for this is you can draw your pages up using the framework, and the frame work will then output it in the specified method. e.g. old HTML, XHTML + Ajax, WML/WAP, possibly even CLI possibilites.

Because I am getting into websites which are used by my different agents, e.g. Palms (cards and decks), Normal web browsers (Firefix, IE, Safari, etc). And not needing to worry about all that compatabilty on each page would be great, but instead leave that for the framework to deal with.


btw. Who has seen the Yahoo beta mail yet, thats one thing I hope to give the abiltity to do, design interfaces like that using a frame work.

Posted: Thu Jun 21, 2007 5:50 am
by Styx
Well without templates you minimize the ability to easily edit what HTML you do have.