Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-26-2010, 08:15 PM   PM User | #1
Skippy
New Coder

 
Join Date: Jul 2009
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Skippy is an unknown quantity at this point
OOP Returning from database

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:

Code:
$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?
Skippy is offline   Reply With Quote
Old 11-26-2010, 08:45 PM   PM User | #2
poyzn
Regular Coder

 
poyzn's Avatar
 
Join Date: Nov 2010
Posts: 265
Thanks: 2
Thanked 61 Times in 61 Posts
poyzn is on a distinguished road
With OOP it will be something like this

PHP Code:
<?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 }
__________________
Ushousebuilders.com

Last edited by poyzn; 11-26-2010 at 08:52 PM..
poyzn is offline   Reply With Quote
Old 11-26-2010, 10:12 PM   PM User | #3
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
that is still a mixture of PHP and HTML, consider this example

PHP Code:
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_ERRMODEPDO::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.
__________________
please post your code wrapped in [CODE] [/CODE] tags

Last edited by Dormilich; 11-26-2010 at 10:16 PM..
Dormilich is offline   Reply With Quote
Old 11-27-2010, 01:32 AM   PM User | #4
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,904
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
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 is probably the best known PHP template system though there are others.
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:32 PM.


Advertisement
Log in to turn off these ads.