/****************************************************************
VIEW MODULE
view, view_database, view_message
view_arche_1 (external), view_arche_2 (external) - these are
primarily html files
****************************************************************/
/*view*/
class view extends database
{
}
/*view_db*/
class view_db extends view
{
function __construct($type)
{
parent::__construct();
$this->invoke($type);
}
private function invoke($type)
{
switch ($type)
{
case "bookmarks":
$this->html_bookmarks();
break;
case "tweets":
$this->html_tweets();
break;
default:
throw new Exception('Invalid View Type');
break;
}
}
private function html_bookmarks()
{
$email = $_SESSION['email'];
$query_return = database::query("SELECT * FROM bo WHERE email='$email' ORDER BY name ASC");
$html_string='';
while ($ass_array = mysqli_fetch_assoc($query_return))
{
$fav=$this->favicon($ass_array['url']);
$html_string = $html_string . "<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>";
}
echo $html_string;
}
private function html_tweets()
{
$query_return = database::query("SELECT * FROM tw ORDER BY time DESC LIMIT 7");
$time = time();
$html_string='';
while ($a = mysqli_fetch_assoc($query_return))
{
$html_string = $html_string . "<div class='Bb2b'><img class='a' src='pictures/$a[email].jpg' alt=''/><a class='a' href='javascript:void(0)'>$a[fname] posted <script type='text/javascript'>document.write(v0($time,$a[time]))</script></a><br/><p class='c'>$a[message]</p></div>";
}
echo $html_string;
}
private function favicon($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;
}
}
class view_message extends view
{
function __construct($type)
{
$this->invoke($type);
}
private function invoke($type)
{
$this->message($type);
}
private function message($type)
{
$type_message = array(
'empty' => '<si_f>Please complete all fields.',
'name'=> '<su_f>Only letters or dashes for the name field.',
'email' => '<si_f>Please enter a valid email.',
'taken' => '<si_f>Sorry that email is taken.',
'pass' => '<si_f>Please enter a valid password, 6-40 characters.',
'validate' => '<si_f>Please contact <a class="d" href="mailto:[email protected]">support</a> to reset your password.');
echo $type_message[$type];
}
}
\$\begingroup\$
\$\endgroup\$
Add a comment
|
1 Answer
\$\begingroup\$
\$\endgroup\$
3
Some random observations:
- What does your base class
database
do? I hope not the stuff it sounds like. Your aim is to separate storage and view logic, not add view logic to your database. - Having both static and non-static functions in a class should make you suspicious (as this is an indicator this class has one then more objective or is inconsistently written).
- Some functions do
echo
, somereturn
and some call other functions. Decide for one way ;). invoke
for parameter to function resolving? Have a look at the much cooler magic function__call
.
Some suggestions:
- It is probably a good idea to move the single view operations into it's own classes. This becomes more important when you add even more operations.
- Unify the way your class is accessed and the return values.
- Views don't extend a database.
Easy to implement way:
- Let every view operation be it's own class.
- Define a common interface for all view operations (e.g.
ViewOperation
with one required functionrun()
). - Create one main class
View
that is responsible to resolve view operations to subclasses.
Example implementation:
interface ViewOperation
{
public function run();
}
public class ControllAdd implements ViewOperation
{
private $_types = array('pass' => '<xx_p>', 'fail' => '<xx_f>');
public function run()
{
$params = func_get_arg(1);
if(isset($params) && !empty($params) && array_key_exists($this->_types, $params))
{
return $this->_types[$params];
}
}
}
public class View
{
public function __call($name, $arguments)
{
// More security required here of course
require_once './ViewOperartopns/' . $name . '.php';
$instance = new $name;
return $instance->run($arguments);
}
}
-
\$\begingroup\$ Thanks...I recently re-structured my code to fit the MVC pattern using this guidline here: php-html.net/tutorials/model-view-controller-in-php : I'll add in your suggestions as well : Excellent information. Thank you. \$\endgroup\$– user7459Commented Nov 1, 2011 at 3:21
-
\$\begingroup\$ Why can't I extend a view from my database...if not how than? Here is just my view module..It needs access to the datbase. Theoretcially this should come through the controller...but how do I do this? \$\endgroup\$– user7459Commented Nov 1, 2011 at 3:28
-
\$\begingroup\$ You sure can, but that'd mix two differenct responsibilites in one class (storage & view). It's the controllers job to pull the data from the database and put it into the view. \$\endgroup\$– FgeCommented Nov 1, 2011 at 14:48