Page 1 of 1

Dynamic content

Posted: Wed Sep 14, 2005 9:28 am
by lisamarie_tt
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

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;
fruit.html

Code: Select all

<!-- Some Html Code goes here -->
<{loop_start}>
  <tr>
      <td>{{name}}</td>
      <td>{{qty}}</td>
  </tr>		
<{loop_end}>
</table>
Can you comment on my code's logic and see where i have gone wrong.

Posted: Wed Sep 14, 2005 11:07 am
by feyd
template blocks must be pulled out of the code and saved in their "code" state. You then iterate over the data needed for the block using the saved code state as a starting point for all of them diverting the output elsewhere..

Posted: Fri Sep 16, 2005 8:45 am
by lisamarie_tt
I am unsure as to what you mean.

Could you give a simple example.

Posted: Wed Sep 21, 2005 8:01 am
by lisamarie_tt
I've tried all sorts of code changes to get this to work and still my output prints only the last record in my record set...
Does anyone understande what the following means -
" template blocks must be pulled out of the code and saved in their "code" state. You then iterate over the data needed for the block using the saved code state as a starting point for all of them diverting the output elsewhere..".

I have not the slighest idea how to make such a modifcation to my code. :(

Posted: Wed Sep 21, 2005 8:09 am
by feyd
basic idea:
  • use a regex to pull the loop section into a single variable.
  • use this variable as a base for each iteration of the loop, storing the results in a different variable always.
  • after looping, store the full results into the output or something similar

Posted: Wed Sep 21, 2005 8:10 am
by lisamarie_tt
Figured it out...

All elements within the loop block on the template (HTML file) must follow the following synatax:
<{element_name}>.
Elements that are not in the loop block can be referenced or accessed using single braces {element_name} or double braces {{element_name}}.

Note: The element name must match the php variable name that represents the data to be displayed within the template element e.g. $name and <{name}>