php...@jakeman.plus.com wrote:
Hi,

These two files will output the number of records (2) from a MySQL database but not the record data. Can anyone suggest what I'm doing wrong?

Thanks, Chris

<?php
$dbc = mysqli_connect("localhost", "root", "password", "cjfiles");
$results = mysqli_query($dbc, "SELECT * FROM article");
$records = mysqli_num_rows($results);

require_once 'PHPTAL.php';

$template = new PHPTAL('show_articles.html');
$template->results = $results;
$template->records = $records;

try {
   echo $template->execute();
}
catch (Exception $e){
   echo $e;
}
?>

<html>
<head>
</head>
<body>
<h1>Articles</h1>
<h1 tal:content="records">Records</h1>
<table border="1" cellpadding="2" cellspacing="2">
  <tr>
    <th>id</th>
    <th>entitled</th>
    <th>located_at</th>
    <th>written_by</th>
    <th>created_on</th>
    <th>updated_on</th>
    <th>words_long</th>
  </tr>
  <tr tal:repeat="result results">
    <td tal:content="result/id">text replaced by result</td>
    <td tal:content="result/entitled">text replaced by result</td>
    <td tal:content="result/located_at">text replaced by result</td>
    <td tal:content="result/written_by">text replaced by result</td>
    <td tal:content="result/created_on">text replaced by result</td>
    <td tal:content="result/updated_on">text replaced by result</td>
    <td tal:content="result/words_long">text replaced by result</td>
  </tr>
</table>
</body>
</html>

first of all, the value returned by "mysqli_query" is a resource, not a resultset. You have to use one of "fetch" functions to get a resultset. fetch function gives you one row at a time, so you could create an array of these rows and in phptal template use "repeat:" functionality like this:
<table>
 <tr tal:repeat="row result">
   <td tal:content="row/id">text replaced by result</td>
   <td tal:content="row/entitled">text replaced by result</td>
   <td tal:content="row/located_at">text replaced by result</td>
   <td tal:content="row/written_by">text replaced by result</td>
   <td tal:content="row/created_on">text replaced by result</td>
   <td tal:content="row/updated_on">text replaced by result</td>
   <td tal:content="row/words_long">text replaced by result</td>
 </tr>
</table>
(given "result" is an array of fetched rows)

hth,
kaaposc

_______________________________________________
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal

Reply via email to