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
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Templating with results from mysql database.

    Okay, so I want to template information from my database. How can I do that?
    For instance
    PHP Code:
    // connect
    // query 
    while($row mysql_fetch_assoc($result)){

    echo 
    "<div id='main'>".$row['id']."</div>";
    }
    // etc 
    How can I template that? I would love to learn how. Thanks alot.
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #2
    New Coder
    Join Date
    Feb 2004
    Location
    California
    Posts
    96
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I do something similar:
    PHP Code:
    <table>
    <?php
    while($row mysql_fetch_assoc($query)){
    ?>
    <tr>
       <td><?php echo $row['result1']; ?></td>
    </tr>
    <?php
    }
    ?>
    I hate doing this whole echo "<table>" but its your choice

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    simplistic example of template replacement with loop & main template
    this assumes that id , name,surname,comments are all database fields , not tested but should be close to working , you get the idea anyway ?

    loop.tpl;
    Code:
    <div id='main'>{id} {name} {surname}</div>
    <div id='main'>{comments}</div>
    main.tpl
    Code:
    <h3>Installing linux on a dead badger</h3>
    <b>Contributors</b>
    {loop}
    <b>etc</b>
    PHP Code:
    <?
    // connect
    // query

    /*or use file_get_contents if available*/
    $tpl implode'' file'./templates/loop.tpl' ) ) ;
    $main_tpl =  implode'' file'./templates/main.tpl' ) ) ;

    $all '' ;
    while(
    $row mysql_fetch_assoc($result)){
        
    $tmp $tpl;
       foreach( 
    $row as $k=>$v ){
         
    $tmpstr_replace'{'.$k.'}' $v $tmp) ;
       }
      
    $all .= $tmp;
    }

    $main_tpl str_replace'{loop}' $all $main_tpl ) ;
    ?>
    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)

  • #4
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks.
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!


  •  

    Posting Permissions

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