Dynamic content

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lisamarie_tt
Forum Commoner
Posts: 32
Joined: Fri Jul 15, 2005 8:20 am

Dynamic content

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
lisamarie_tt
Forum Commoner
Posts: 32
Joined: Fri Jul 15, 2005 8:20 am

Post by lisamarie_tt »

I am unsure as to what you mean.

Could you give a simple example.
lisamarie_tt
Forum Commoner
Posts: 32
Joined: Fri Jul 15, 2005 8:20 am

Post 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. :(
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
lisamarie_tt
Forum Commoner
Posts: 32
Joined: Fri Jul 15, 2005 8:20 am

Post 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}>
Post Reply