« Previous entry | Next entry » Browse > Snippets
Skip to comments (5)
Content Class
Posted by cpress on Dec 24 2005 @ 07:14 :: 2650 unique visits
I had gotten bored last week and put together a simple content class. I hope you can figure out everything else.CODE: PHP
<?php
class Content
{
function Title()
{
$sql = "SELECT title FROM config WHERE id='1'";
$query = mysql_query($sql);
$array = mysql_fetch_array($query);
return $array[0];
}
function CSS()
{
$sql = "SELECT css FROM config WHERE id='1'";
$query = mysql_query($sql);
$array = mysql_fetch_array($query);
return $array[0];
}
function Banner()
{
$sql = "SELECT banner FROM config WHERE id='1'";
$query = mysql_query($sql);
$array = mysql_fetch_array($query);
return $array[0];
}
function Navigation()
{
$sql = "SELECT src, name FROM navigation ORDER BY name DESC";
$query = mysql_query($sql);
echo "<a href='index.php'>Home</a><br>\n";
while($links = mysql_fetch_array($query))
{
echo "<a href='". /*$_SERVER['HTTP_HOST'] ."/".*/ $links[0] ."'>". $links[1] ."</a><br>\n";
}
}
function Articles()
{
$sql = "SELECT title, body, date, time, username FROM articles ORDER BY date DESC LIMIT 3";
$query = mysql_query($sql);
if(mysql_num_rows($query) != 0)
{
echo "<br>\n";
while($artical = mysql_fetch_array($query))
{
echo "<table align='center' border='1' cellpadding='0' cellspacing='0' width='75%'>\n";
echo " <tr>\n";
echo " <td align='center' valign='top'><b>". $artical[0] ." - ". $artical[4] ."</b></td>\n";
echo " </tr>\n";
echo " <tr>\n";
echo " <td valign='top'><blockquote>". $artical[1] ."</blockquote></td>\n";
echo " </tr>\n";
echo " <tr>\n";
echo " <td align='right' valign='top'>". $artical[2] ." ". $artical[3] ."</td>\n";
echo " </tr>\n";
echo "</table>\n<br>\n";
}
}else{
echo "<div align=center><b>There are no articals at this time.</b></div>";
}
}
function Footer()
{
$sql = "SELECT footer FROM config WHERE id='1'";
$query = mysql_query($sql);
$array = mysql_fetch_array($query);
return $array[0];
}
}
?>
class Content
{
function Title()
{
$sql = "SELECT title FROM config WHERE id='1'";
$query = mysql_query($sql);
$array = mysql_fetch_array($query);
return $array[0];
}
function CSS()
{
$sql = "SELECT css FROM config WHERE id='1'";
$query = mysql_query($sql);
$array = mysql_fetch_array($query);
return $array[0];
}
function Banner()
{
$sql = "SELECT banner FROM config WHERE id='1'";
$query = mysql_query($sql);
$array = mysql_fetch_array($query);
return $array[0];
}
function Navigation()
{
$sql = "SELECT src, name FROM navigation ORDER BY name DESC";
$query = mysql_query($sql);
echo "<a href='index.php'>Home</a><br>\n";
while($links = mysql_fetch_array($query))
{
echo "<a href='". /*$_SERVER['HTTP_HOST'] ."/".*/ $links[0] ."'>". $links[1] ."</a><br>\n";
}
}
function Articles()
{
$sql = "SELECT title, body, date, time, username FROM articles ORDER BY date DESC LIMIT 3";
$query = mysql_query($sql);
if(mysql_num_rows($query) != 0)
{
echo "<br>\n";
while($artical = mysql_fetch_array($query))
{
echo "<table align='center' border='1' cellpadding='0' cellspacing='0' width='75%'>\n";
echo " <tr>\n";
echo " <td align='center' valign='top'><b>". $artical[0] ." - ". $artical[4] ."</b></td>\n";
echo " </tr>\n";
echo " <tr>\n";
echo " <td valign='top'><blockquote>". $artical[1] ."</blockquote></td>\n";
echo " </tr>\n";
echo " <tr>\n";
echo " <td align='right' valign='top'>". $artical[2] ." ". $artical[3] ."</td>\n";
echo " </tr>\n";
echo "</table>\n<br>\n";
}
}else{
echo "<div align=center><b>There are no articals at this time.</b></div>";
}
}
function Footer()
{
$sql = "SELECT footer FROM config WHERE id='1'";
$query = mysql_query($sql);
$array = mysql_fetch_array($query);
return $array[0];
}
}
?>
Then declare your class:
CODE: PHP
which will display the title that is stored in the database.
Simple as that.
5 comments posted so far
Add your own »
2. On Dec 24 2005 @ 21:12 cpress wrote:
All the database contains is text/urls/ then i format everyting in the index.php file.3. On Dec 27 2005 @ 10:12 Buzzard wrote:
First putting the header/footer/css/banner into a db is a bit silly IMO, that thats one of the things PHP does really well (embeding code into html).I forget what it's called, but I prefer to pass around objects, as oposed to mixing SQL + presentation.
eg:
CODE: PHP
<?php
class Article
{
function Article($title, $body, $date, $time, $username)
{
# A quick one liner
list($this->title, $this->body, $this->date, $this->time, $this->username) = array($title, $body, $date, $time, $username);
}
}
class Content
{
/*static*/ function getArticles($limit = 10, $offset = 0)
{
# note, I can't actually remember the offset command, so no idea if it actually works
$sql = "SELECT title, body, date, time, username FROM articles ORDER BY date DESC LIMIT $limit OFFSET $offset";
$query = mysql_query($sql);
if( mysql_num_rows($query) == 0)
return false;
$articles = array();
while($row = mysql_fetch_array($query))
$articles[] = new Article($row[0],$row[1],$row[2],$row[3],$row[4]);
return $articles;
}
}
#Then its as simple as in your presentation code:
$articles = Content::getArticles();
if( $articles === false)
{
echo "No articles at this time\n";
}
else
{
foreach($articles as $article)
{
echo "title {$article->title}<br/>\n";
echo "body {$article->body}<br/>\n";
echo "date {$article->date}<br/>\n";
echo "time {$article->time}<br/>\n";
echo "username {$article->username}<br/>\n";
}
}
?>
class Article
{
function Article($title, $body, $date, $time, $username)
{
# A quick one liner
list($this->title, $this->body, $this->date, $this->time, $this->username) = array($title, $body, $date, $time, $username);
}
}
class Content
{
/*static*/ function getArticles($limit = 10, $offset = 0)
{
# note, I can't actually remember the offset command, so no idea if it actually works
$sql = "SELECT title, body, date, time, username FROM articles ORDER BY date DESC LIMIT $limit OFFSET $offset";
$query = mysql_query($sql);
if( mysql_num_rows($query) == 0)
return false;
$articles = array();
while($row = mysql_fetch_array($query))
$articles[] = new Article($row[0],$row[1],$row[2],$row[3],$row[4]);
return $articles;
}
}
#Then its as simple as in your presentation code:
$articles = Content::getArticles();
if( $articles === false)
{
echo "No articles at this time\n";
}
else
{
foreach($articles as $article)
{
echo "title {$article->title}<br/>\n";
echo "body {$article->body}<br/>\n";
echo "date {$article->date}<br/>\n";
echo "time {$article->time}<br/>\n";
echo "username {$article->username}<br/>\n";
}
}
?>
While its a bit longer, it makes changing the presentation really simple and is not as error prone as passing around arrays. You can also change the backend without having to edit anything else.
My websites objects actually contain a function for the template class so it knows what variables to search and replace (it's a real hack, but it makes it really simple to display objects given a template)
4. On Mar 02 2009 @ 07:47 guest wrote:
When the wolf wow gold finally found the hole Buy Wow Gold in the chimney he Cheap WoW Gold crawled down and KERSPLASH right into that kettle of water cheapest wow gold and that was the end of his troubles with the big bad wolf.The next day the wow gold cheap little pig invited his mother over . She said "You see it is just as I told you. The way to get along in the world is to do things as well as you can." Fortunately for that world of warcraft gold little pig, he learned that lesson. And he just lived happily ever after! buy wow gold .
1. On Dec 24 2005 @ 18:48 Erik wrote:
Are you storing html in your database?Wouldn't it be better to do stuff like this:
include './footer.tpl';
}
where footer.tpl contains the html for the footer (it could even include php code).
This would be much much much faster and easyer to edit.