Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post

    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?

  • #2
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    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 }
    Last edited by poyzn; 11-26-2010 at 08:52 PM.

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,236
    Thanks
    12
    Thanked 340 Times in 336 Posts
    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.
    Last edited by Dormilich; 11-26-2010 at 10:16 PM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    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)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •