Dynamic content
Posted: Wed Sep 14, 2005 9:28 am
Description of Task:
I am creating a template application using php and html.
Description of Problem:
The php file which generates the template has to loop through a record set. The information being displayed is only the last row of the record set for the number of rows in the record set.
Example: say the data to be displayed is Apple 20, Pear 44, Mango 12 the dispaly shows.
| Fruit |Quantity|
----------------------
Mango | 12
Mango | 12
Mango | 12
Sample Code:
fruit.php
fruit.html
Can you comment on my code's logic and see where i have gone wrong.
I am creating a template application using php and html.
Description of Problem:
The php file which generates the template has to loop through a record set. The information being displayed is only the last row of the record set for the number of rows in the record set.
Example: say the data to be displayed is Apple 20, Pear 44, Mango 12 the dispaly shows.
| Fruit |Quantity|
----------------------
Mango | 12
Mango | 12
Mango | 12
Sample Code:
fruit.php
Code: Select all
$file=file("fruit.html");
$filecontent=join("",$file);
// database connetion and query
preg_match("/<{loop_start}>(.*?)<{loop_end}>/s",$filecontent,$out);
$selected_content=$out[0];
$str="";
for($index=0;$index<count($fruits);$index++)
{
$name = $vehicles[$index]['desc'];
$qty = $vehicles[$index]['qty'];
$str.=preg_replace("/<{(.*?)}>/e","$$1",$selected_content);
}
$filecontent=preg_replace("/<{loop_start}>(.*?)<{loop_end}>/s",$str,$filecontent);
$filecontent=preg_replace("/{{(.*?)}}/e","$$1",$filecontent);
echo $filecontent;Code: Select all
<!-- Some Html Code goes here -->
<{loop_start}>
<tr>
<td>{{name}}</td>
<td>{{qty}}</td>
</tr>
<{loop_end}>
</table>