class design - improving a view_html class [closed]
Want to im开发者_Go百科prove this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this questionThis class provides user created content to the page.
The call is made from html like this:
<?php new route('tweets'); ?>
or
<?php new route('bookmarks'); ?>
Route instantiates view_html and passes the paramter along.
What improvments that can be made?
/*view_html*/
class view_html extends database
{
function __construct($type)
{
parent::__construct();
switch ($type)
{
case "bookmraks":
$this->bookmarks();
break;
case "tweets":
$this->tweets();
break;
default:
echo "Invalid View Type";
break;
}
}
private function bookmarks()
{
$email = $_SESSION['email'];
$query_return = database::query("SELECT * FROM bo WHERE email='$email' ORDER BY name ASC");
while ($ass_array = mysqli_fetch_assoc($query_return))
{
$fav=$this->fav($ass_array['url']);
echo "<img name=\"bo_im\" class=\"c\" src=\"$fav\"/ onerror=\"i_bm_err(this)\"><a target=\"_blank\" name = \"a1\" class = \"b\" href = \"$ass_array[url]\">$ass_array[name]</a>";
}
}
private function tweets()
{
$query_return = database::query("SELECT * FROM tw ORDER BY time DESC LIMIT 7");
$time = time();
while ($a = mysqli_fetch_assoc($query_return))
{
echo "<div class=\"Bb2b\"><img class=\"a\" src=\"p/$a[email].jpg\" alt=\"\"/><a class=\"a\" href=\"javascript:void(0)\">$a[fname] posted <script type=\"text/javascript\">document.write(v0($a[time],$time))</script></a><br/><p class=\"c\">$a[message]</p></div>";
}
}
private function fav($url)
{
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if(preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs))
{
return $pieces['scheme'] . '://www.' . $regs['domain'] . '/favicon.ico';
}
return false;
}
}
This isn't a code review site, you'd be better off taking this question here https://codereview.stackexchange.com/
However, you may want to consider using a switch block instead of if statements in case you want to expand the number of possible types later. http://php.net/manual/en/control-structures.switch.php
For the type, you could use a switch case construct in your constructor. The default case could be throwing an InvalidArgumentException.
精彩评论