i am working on creating php templates for an application, but i am not understanding how to cover while loop in templates, my code is as follow, that is not right and of course not working at all, it is being posted just for example. Please any body give me an idea that how it can be implemented,

<?php
require_once("temp-test.php");
mysql_connect("localhost", "root","")or die("cannot connect"); 
mysql_select_db("test") or die("cannot select DB");

$sql="SELECT rid,tid,name FROM reply";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$rid=$row[rid];
$tid=$row[tid];
$name=$row[name];
}
?>

the temp-test.php file as follow

<html>
<head>
<title>template</title>
</head>
<body>

<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="360" height="100">
 <tr><td width="358" height="100">
<p style="margin-left: 10; margin-top: 0; margin-bottom: 5"><font face="Segoe UI">Reply ID:<font color="#DA251D">
<?php echo "$rid";?></font></font></p>

<p style="margin-left: 10; margin-top: 0; margin-bottom: 5"><font face="Segoe UI">Topic ID:<font color="#DA251D"><?php echo "$tid";?></font></font></p>

<p style="margin-left: 10; margin-top: 0; margin-bottom: 5"><font face="Segoe UI">Name: <font color="#DA251D"><?php echo "$name";?>
</font></font></p></td></tr></table><br>
</body>
</html>

any suggestion please, thanks in advance....

Dani AI

Generated

: the issue is not the loop itself but where and how the template is used. Your template is being loaded once (and with require_once it will only ever be loaded once), then the loop fills variables afterward — so nothing is rendered per row. Two practical, maintainable approaches are common: include a small "row" template inside the loop, or use a token-based template and replace tokens for each row.

A simple, modern pattern (use a partial file that contains only the markup for one reply) looks like this:

$pdo = new PDO($dsn, $user, $pass);
$stmt = $pdo->query('SELECT rid, tid, name FROM reply');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $rid = $row['rid'];
    $tid = $row['tid'];
    $name = $row['name'];
    include 'reply-partial.php';
}

Notes: (1) reply-partial.php should not contain full <html>/<body> wrappers — just the block repeated per row. (2) include (not require_once) will execute the file each iteration. (3) Prefer PDO or mysqli — the old mysql_* extension was removed in newer PHP; see the PDO manual.

If a non-PHP template is preferred, load the fragment once and replace tokens (safer for designers, easier to cache):

$tmpl = file_get_contents('row.tmpl.html'); // contains {{rid}}, {{name}} tokens
$output = '';
foreach ($rows as $r) {
    $output .= strtr($tmpl, [
        '{{rid}}'  => htmlspecialchars($r['rid'], ENT_QUOTES, 'UTF-8'),
        '{{tid}}'  => htmlspecialchars($r['tid'], ENT_QUOTES, 'UTF-8'),
        '{{name}}' => htmlspecialchars($r['name'], ENT_QUOTES, 'UTF-8'),
    ]);
}
echo $output;

Security and tooling: always escape output for HTML, avoid extract() unless confident about variable scope, and use buffering or caching for large result sets. As noted, existing engines like Smarty or XTemplate handle loops and partials if rolling your own becomes too costly. For reference on how PHP includes work, see the include documentation.

Recommended Answers

All 2 Replies

Do you have to design your own templating system ? Otherwise, have a look at Smarty. It will cover all your templating needs.

If you need to create your own, then you would have to define a begin and end marker in your template, and program the parser to copy everything in between and replace your row values. (like XTemplate handles it)

yes i want to create my own template but i don't know how it can be done, please if you can quote me an example...however thank you very much for idea....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.