...

View Full Version : Templating with results from mysql database.



SDP2006
04-18-2004, 01:21 AM
Okay, so I want to template information from my database. How can I do that?
For instance

// 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.

Steveo31
04-18-2004, 07:04 AM
I do something similar:


<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 :)

firepages
04-18-2004, 02:17 PM
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;


<div id='main'>{id} {name} {surname}</div>
<div id='main'>{comments}</div>


main.tpl


<h3>Installing linux on a dead badger</h3>
<b>Contributors</b>
{loop}
<b>etc</b>




<?
// 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 ){
$tmp= str_replace( '{'.$k.'}' , $v , $tmp) ;
}
$all .= $tmp;
}

$main_tpl = str_replace( '{loop}' , $all , $main_tpl ) ;
?>

SDP2006
04-18-2004, 06:21 PM
Thanks. :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum