...

View Full Version : OOP Returning from database



Skippy
11-26-2010, 09:15 PM
Hi,

I'm currently learning OOPHP and I've been searching on google for the best way to return results from a database, I haven't actually been able to find anything that will return results and output it as html without html code being mixed with php such as:


$query = "SELECT u_username, u_firstName, u_lastName FROM userTable";
$users = mysql_query($query);
while($row = mysql_fetch_assoc($users))
{
echo "<div class="user">";
echo "<strong>".$row['u_username']."</strong> ".
"<em>".$row['u_firstName']." ".$row['u_lastName']."</em>";
echo "</div>";
}

Can anyone help me out with seperating PHP code and HTML code whilst returning results from a database?

poyzn
11-26-2010, 09:45 PM
With OOP it will be something like this



<?php
// defining User class
class User {
function __construct($row) {
$this->username = $row['u_username'];
$this->firstname = $row['u_firstName'];
$this->lastname = $row['u_lastName'];
$this->fullname = $row['u_firstName'] . ' ' . $row['u_lastName'];
}
public $username;
public $firstname;
public $lastname;
public $fullname;
}

// mapping user info
$query = "SELECT u_username, u_firstName, u_lastName FROM userTable";
$users = mysql_query($query);
while($row = mysql_fetch_assoc($users)) {
$user->$row['u_username'] = new User($row);
}

// output
foreach($user as $username => $user_info) { ?>
<div class="user">
<strong><?php echo $username ?></strong>
<em><?php echo $user_info->fullname ?></em>
</div>
<?php }

Dormilich
11-26-2010, 11:12 PM
that is still a mixture of PHP and HTML, consider this example


interface ResultReplace
{
public function replace($template);
}

class Base implements ResultReplace
{
protected
$u_username,
$u_firstName,
$u_lastName
;

public function replace($template)
{
$str = $template;
$str = str_replace("_%USER%_", $this->u_username, $str);
$str = str_replace("_%FIRST%_", $this->u_firstName, $str);
$str = str_replace("_%LAST%_", $this->u_lastName, $str);
return $str;
}
}


class User extends Base
{
protected $html = '';

public function __construct($tpl)
{
$this->html = $tpl;
}

public function __toString()
{
return $this->replace($this->html);
}
}

try
{
// set your database credentials here
$pdo = new PDO($dsn, $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$html = file_get_contents(TEMPLATE_DIR . "user.inc")
if (false === $html)
{
throw new Exception("Failed to load template 'user.inc'.");
}
/* user.inc:
<div class="user">
<strong>_%USER%_</strong>
<em>_%FIRST%_ _%LAST%_</em>
</div>
*/

$ps = $pdo->query("SELECT `u_username`, `u_firstName`, `u_lastName` FROM `userTable`");
$ps->setFetchMode(PDO::FETCH_CLASS, "User", array($html));

foreach($ps as $user)
{
echo $user;
}

}
catch (Exception $e)
{
echo "We’re sorry, there seems to be a problem.";
error_log($e->getMessage());
}
if you add autoloading, the real fun starts.

firepages
11-27-2010, 02:32 AM
in other words :) .. look for, or create, a templating system, that way your template class removes the HTML from your code since the HTML is stored in the template, SMARTY (http://www.smarty.net/) is probably the best known PHP template system though there are others.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum