How To OOP-ify
Posted: Thu Nov 03, 2005 7:49 pm
I am struggling to figure out how to convert my current code
(mainly functions) into an OOP approach.
The problem(and frustration) comes from the fact
that I got like 10 functions that almost all do the same thing
minus different ways of doing it.
One function displays a random news article
Another displays last 10 news article
Another display only one based on what was selected.
All three show common data:
Number of comments
Comment link
Make Comment Link
Tell Friend Link
News Article Link
Then they show different data based on which one is showing:
One random Article:
Next random Link
Only one article based on what was selected displays:
Prev/Next Links
article title/author in browser title bar
and etc.....
Now it is starting to get difficult having to seperatly code each function by
duplicating most code and having different code for each based on what it does.
I need an example of how to redo this based on OOP if
anyone is kind enough to show me?
The three functions listed above are as follows:
As you can see... this badly needs converted to OOP
p.s.
I know the arrays are horrible... another thing I am slowly trying to correct
(mainly functions) into an OOP approach.
The problem(and frustration) comes from the fact
that I got like 10 functions that almost all do the same thing
minus different ways of doing it.
One function displays a random news article
Another displays last 10 news article
Another display only one based on what was selected.
All three show common data:
Number of comments
Comment link
Make Comment Link
Tell Friend Link
News Article Link
Then they show different data based on which one is showing:
One random Article:
Next random Link
Only one article based on what was selected displays:
Prev/Next Links
article title/author in browser title bar
and etc.....
Now it is starting to get difficult having to seperatly code each function by
duplicating most code and having different code for each based on what it does.
I need an example of how to redo this based on OOP if
anyone is kind enough to show me?
The three functions listed above are as follows:
Code: Select all
//This is not a function but is used in the random function to
//return the Title/Author to the Browser Title Bar
$getTitle = mysql_query("SELECT * FROM table ORDER BY RAND() LIMIT 1");
while($displayTitle = mysql_fetch_array($getTitle))
{ $sitename = $displayTitle['title']; }Code: Select all
//Shows One Random News Entry
function one_random_news_entry($sitename) {
$result = mysql_query("SELECT * FROM table ORDER BY RAND() LIMIT 1");
$title_extra = '';
while ($row = mysql_fetch_assoc($result))
{
echo"
<a href='./random.php'>Keep The Bashes Coming!</a> | <a href='./submitbash.php'> Post Your Own Bash!</a> |<br />
";
$title_extra .= $row['row'] .' | Author: '. $row['name'] .' ';
$bash = $row['id'];
/* get number of comments */
$comment_query = "SELECT count(*) FROM comments WHERE newsid='$bash'";
$comment_result = mysql_query ($comment_query);
$comment_row = mysql_fetch_row($comment_result);
echo "<br /><a href=\"$bash\">$comment_row[0] Comments</a> <a href=/submitcomment.php?bash=$bash>Make A Comment</a> <a href=\"/tell/bash/$bash\">Tell-A-Friend</a><br />";
$newposts = array ( "Bash Title" => $row['title'], "Name" => $row['name'], "Bash" => $row['bash'] );
foreach ( $newposts as $postfield => $postinput )
{
$postinput = submitTags($postinput, 1, 1);
echo "<b>{$postfield}:</b> {$postinput}<br />";
}
echo "</div></div></div>";
}
echo"<title>$sitename | $title_extra</title>";
}Code: Select all
//Shows Last 10 Entries
function show_last_five_entries($url) {
$bash = 1;
$max_results = 10;
$from = (($bash * $max_results) - $max_results);
$sql = mysql_query("SELECT * FROM table WHERE published = '1' ORDER BY date DESC LIMIT $from, $max_results");
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM table"),0);
$total_bashs = ceil($total_results / $max_results);
while ($row = mysql_fetch_assoc($sql))
{
$bash = $row['id'];
/* get number of comments */
$comment_query = "SELECT count(*) FROM table WHERE newsid='$bash'";
$comment_result = mysql_query ($comment_query);
$comment_row = mysql_fetch_row($comment_result);
$newposts = array ( "<a href=$url/display/bash/$bash>" => $row['bash'], "</a>\n</div>\n<b>Submitted by</b>" => $row['name'], "\n<br />\n<a href=$url/comment/bash/$bash>$comment_row[0] Comments</a> \n<a href=http://bashmyex.com/submitcomment.php?bash=$bash>Make A Comment</a> \n<a href=$url/tell/bash/$bash>Tell-A-Friend</a>\n<br /><br />" => $row['news'] );
foreach ( $newposts as $postfield => $postinput )
{
$postinput = submitTags($postinput, 1, 1);
echo "{$postfield} {$postinput} ";
}
}
}Code: Select all
// Returns News Page Links
function NewsPage($sitename) {
$bash = $row['id'];
$bash = $_GET['bash'];
$max_results = 1;
if(!isset($_GET['bash'])){
$bash = 1;
} else {
$bash = $_GET['bash'];
}
$from = (($bash * $max_results) - $max_results);
$sql = mysql_query("SELECT * FROM table LIMIT $from, $max_results");
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM table"),0);
$total_bashs = ceil($total_results / $max_results);
/* get number of comments */
$comment_query = "SELECT count(*) FROM table WHERE newsid='$bash'";
$comment_result = mysql_query ($comment_query);
$comment_row = mysql_fetch_row($comment_result);
if($bash > 1){
$prev = ($bash - 1);
echo "<center><a href=\"/display/bash/$prev\"><<-Previous</a> ";
}
if($bash < $total_bashs){
$next = ($bash + 1);
echo " <a href=\"/display/bash/$next\">Next->></a><br />";
}
if($bash > 1){
$send = ($bash - 0);
$com = ($bash - 0);
echo "</center><br /><br />";
}
$title_extra = '';
while ($row = mysql_fetch_assoc($sql))
{
$title_extra .= $row['title'] .' | Bashed by: '. $row['name'] .' ';
$newposts = array ( "<a href=$url/comment/bash/$bash>" => $row['title'], "</a>Submitted By" => $row['name'], "<a href=$url/comment/bash/$bash>$comment_row[0] Comments</a> <a href=http://bashmyex.com/submitcomment.php?bash=$bash>Make A Comment</a> <a href=$url/tell/bash/$bash>Tell-A-Friend</a><br /><br />" => $row['news'] );
foreach ( $newposts as $postfield => $postinput )
{
$postinput = submitTags($postinput, 1, 1);
echo "<b>{$postfield}</b> {$postinput}<br />";
}
}
echo"<title>$sitename | $title_extra</title>";
}As you can see... this badly needs converted to OOP
p.s.
I know the arrays are horrible... another thing I am slowly trying to correct