Creating different classes for each dynamicly created menu - php

I am using Zend2 and i am creating menu items dynamicly.
This is the function i am using:
public static function getAdminMenu() {
$config = \App\Application::getInstance()->getConfig();
$menuItems = $config['menu_items'];
$html = '<ul>';
foreach ($menuItems as $section => $menuItem) {
$html .= '<div class="user-menu-section">' . $section . '</div>';
foreach ($menuItem as $subSection => $params) {
$html .= '<li>' . $subSection . '</li>';
}
}
$html .= '</ul>';
return $html;
}
How can i create divs with different class user-menu-section for each menu item. It should be something like 'user-menu-section1', 'user-menu-section2'...
Or maybe better to use something like this:
<div class="' . $section . '">;
but in this case, if $section is a string of two words i would need '-' in between words and both words small caps, if it is possible.

Well, just use your $section and modify this. Using ZF2, you'd use the Filter CamelCaseToDash
$filter = new \Zend\Filter\Word\CamelCaseToDash();
$classFiltered = strtolower($filter->filter($class);)
Now you can use $classFiltered for your CSS-Class assignment.
And since you've mentioned both frameworks in your tags. In case you are using ZF2, that code is horrible :D You should create yourself a ViewHelper that renders the Menu. Evan Coury has written a very easy introduction on how to do that.
Aside from that, you don't need a static call to some Application::getInstance(). If you want to gain access to the config you do this via the ServiceLocator. In a Controller this would look like this:
$config = $this->getServiceLocator()->get('config');
If you need the config in another class outside of the Controller, you create the class from the ServiceLocator and inject the config into this class.

Related

Remove empty categories from magento menu

I want my main menu to not include any categories that are empty. I've done this for the layered navigation very easily in the relevant phtml file by using
$_category->getProductCount()
However, for the navigation menu, I'm finding it impossible to do this as easily (I have seen the Prattski example but it does seem rather OTT).
The main menu seems to be built in Mage_Page_Block_Html_Topmenu.php, specifically in the function _getHtml. This gets all the children in the menu and if I try something like $child->getId(), I get something like "category-node-36".
It doesn't seem like I'm too far from being able to use getProductCount() and so test if it's more than zero.
Is it possible to do this? Can somebody point me to how?
If I can, I'll extend the class with my version.
To do this, go to:
app/code/core/Mage/Catalog/Block Folder and copy Navigation.php and override it in your local package.
Open Navigation.php of your package and paste below code in this file:
if ($category->getIsActive()) {
$cat = Mage::getModel('catalog/category')->load($category->getId());
$products = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($cat);
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($products);
if(count($products)==0)
return;
}
I hope my code will help you.
I finally cracked it although I'm far from convinced it's an optimum solution. Anyway, I'll described what I did here and hopefully somebody can make it more efficient. I'll give a blow-by-blow description as I was ufamiliar with quite a few areas. So apologies for the length.
As I said, in my case at least, the main menu is built via Topmenu.php in app/code/core/Mage/Page/Block/Html, specifically the method _getHtml. I very definitely don't want to modify a core file so I found out how to extend this method via a new module. (You can skip this bit if you're familiar with creating new modules.)
Configuring a new module
I needed to create a new module (I'll call it MYMOD below). As I'm overwriting the core magento page block, I had to create new folders: app/code/local/MYMOD/Page and in there two sub-folders, Block and etc (I believe they are case sensitive). And within Block another subfolder Html. You can see this is exactly mirroring the folder structure from app/code/core/Mage.
The etc folder holds the specification for the new module in a config.xml file. This is what mine looks like:
<?xml version="1.0" encoding="UTF-8"?>
<!-- The root node for Magento module configuration -->
<config>
<!--
The module's node contains basic
information about each Magento module
-->
<modules>
<!--
This must exactly match the namespace and module's folder
names, with directory separators replaced by underscores
-->
<MYMOD_Page>
<!-- The version of our module, starting at 0.0.1 -->
<version>0.0.1</version>
</MYMOD_Page>
</modules>
<global>
<blocks>
<page>
<rewrite>
<html_topmenu>MYMOD_Page_Block_Html_Topmenu</html_topmenu>
</rewrite>
</page>
</blocks>
</global>
</config>
You can find out about the whys and wherefors of this elsewhere.
Unfortunately (in my opinion!), that's not all you have to do to specify a new module in Magento. You also have to create a file called "MYMOD_Page.xml" in app/etc/modules. This is just telling Magento about your module and where to look for it. Mine looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<MYMOD_Page>
<!-- Whether our module is active: true or false -->
<active>true</active>
<!-- Which code pool to use: core, community or local -->
<codePool>local</codePool>
</MYMOD_Page>
</modules>
</config>
OK, sorry for the irrelevant instructions on modules but I do like self-contained blow-by-blow explanations.
Overwriting the method
I can now create a new file in my module with a subclass in which I can have methods (functions) that will be used instead of the core Magento ones. The file name has to be the same as the original file, Topmenu.php and it goes in app/code/local/MYMOD/Page/Block/Html.
Remember, the object oriented structure means that all of the functions in the original core version of Topmenu.php are available to me, I don't have to copy them in my new version (great for maintainability). My version of Topmenu.php only has to contain any new functions I might want (in this case I didn't need any) and redeclare any functions I want to overwite with my own version. In my case, I needed to modify two functions: _getHtml and _getMenuItemClasses.
_getHtml needs additional checks so any empty categories aren't included.
_getMenuItemClasses needs additional checks so that the class "parent" isn't added to categories whose children are empty.
Here's how I did it (with comments). I'm sure there are better ways but I'm still fairly new to Magento.
class MYMOD_Page_Block_Html_Topmenu extends Mage_Page_Block_Html_Topmenu
// Create my subclass, in accordance with how I've defined the new module
{
/**
* Recursively generates top menu html from data that is specified in $menuTree
*
* #param Varien_Data_Tree_Node $menuTree
* #param string $childrenWrapClass
* #return string
*/
protected function _getHtml(Varien_Data_Tree_Node $menuTree, $childrenWrapClass)
{
$html = '';
$children = $menuTree->getChildren();
$parentLevel = $menuTree->getLevel();
$childLevel = is_null($parentLevel) ? 0 : $parentLevel + 1;
$counter = 1;
$childrenCount = $children->count();
$parentPositionClass = $menuTree->getPositionClass();
$itemPositionClassPrefix = $parentPositionClass ? $parentPositionClass . '-' : 'nav-';
foreach ($children as $child) {
$child->setLevel($childLevel);
$child->setIsFirst($counter == 1);
$child->setIsLast($counter == $childrenCount);
$child->setPositionClass($itemPositionClassPrefix . $counter);
$outermostClassCode = '';
$outermostClass = $menuTree->getOutermostClass();
if ($childLevel == 0 && $outermostClass) {
$outermostClassCode = ' class="' . $outermostClass . '" ';
$child->setClass($outermostClass);
}
/*
* Find out if this category has any products. I don't know an easier way.
* The id of every child returned by getID is of the form "category-node-nnn"
* where nnn is the id that can be used to select the category details.
* substr strips everything leaving just the nnn.
* Then use getModel-> getCollection with a filter on the id. Although this looks
* like it will return many, obviously category ids are unique so in fact it only
* returns the category we're currently looking at.
*/
$_gcategoryId = substr($child->getId(), 14, 6);
$_gcategories = Mage::getModel('catalog/category')->getCollection()->addFieldToFilter('entity_id', array('eq', $_gcategoryId));
foreach ($_gcategories as $_gcategory) {
$_gcategoryCount = $_gcategory->getProductCount();
}
/*
* Now only include those categories that have products.
* In my case I also wanted to include the top level categories come what may.
*/
if (($childLevel == 0) || ($_gcategoryCount > 0)) {
$html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
$html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>'
. $this->escapeHtml($child->getName()) . '</span></a>';
if ($child->hasChildren()) {
if (!empty($childrenWrapClass)) {
$html .= '<div class="' . $childrenWrapClass . '">';
}
$html .= '<ul class="level' . $childLevel . '">';
$html .= $this->_getHtml($child, $childrenWrapClass);
$html .= '</ul>';
if (!empty($childrenWrapClass)) {
$html .= '</div>';
}
}
$html .= '</li>';
}
$counter++;
}
return $html;
}
/**
* Returns array of menu item's classes
*
* #param Varien_Data_Tree_Node $item
* #return array
*/
protected function _getMenuItemClasses(Varien_Data_Tree_Node $item)
{
$classes = array();
$classes[] = 'level' . $item->getLevel();
$classes[] = $item->getPositionClass();
if ($item->getIsFirst()) {
$classes[] = 'first';
}
if ($item->getIsActive()) {
$classes[] = 'active';
}
if ($item->getIsLast()) {
$classes[] = 'last';
}
if ($item->getClass()) {
$classes[] = $item->getClass();
}
if ($item->hasChildren()) {
/*
* Don't just check if there are children but, if there are, are they all empty?
* If so, then the changes in _getHtml will mean none of them will be included
* and so this one has no children displayed and so the "parent" class is not appropriate.
*/
$children = $item->getChildren(); // Get all the children from this menu category
foreach ($children as $child) { // Loop over each child and find out how many products (see _getHtml)
$_gcategoryId = substr($child->getId(), 14, 6);
$_gcategories = Mage::getModel('catalog/category')->getCollection()->addFieldToFilter('entity_id', array('eq', $_gcategoryId));
foreach ($_gcategories as $_gcategory) { // Remember, there's actually only one category that will match the child's id
$_gcategoryCount = $_gcategory->getProductCount();
}
if ($_gcategoryCount > 0) { // As soon as one child has products, then we have a parent and can stop looking
$classes[] = 'parent';
break;
}
}
}
return $classes;
}
}
I hope this is clear. It does what I want (my store is small) but any suggestions for improvement welcome.
path: app/design/frontend/rwd/default/template/page/html/topmenu/renderer.phtml
Make this query (it is 0.0004 sec), under the foreach ($children as $child) {
$mageconnection = Mage::getSingleton("core/resource")->getConnection("core_read");
$query="select count(cataloginventory_stock_item.is_in_stock) as subcount,catalog_category_flat_store_1.`name` from catalog_category_flat_store_1 INNER JOIN
catalog_category_product_index on catalog_category_product_index.category_id=catalog_category_flat_store_1.entity_id INNER JOIN
cataloginventory_stock_item on cataloginventory_stock_item.product_id=catalog_category_product_index.product_id
where cataloginventory_stock_item.is_in_stock=1 and catalog_category_product_index.category_id=";
$subCatqueryId = str_replace('category-node-', '', $child->getId());
$prodCollection = $mageconnection->fetchAll("$query'{$subCatqueryId}'");
if($prodCollection[0]["subcount"] > 0) {
$child->setLevel($childLevel);
$child->setIsFirst($counter == 1);
// these are the existing code ..
...
...
$counter++;
}
}
It is very fast and secure way to control product count.

Zend form element use viewHelper as decorator

What I'm trying to achieve is using viewHelper to customize my element. I know that it's mostly done by decorators, but I would like to know is it possible to do it with viewHelper only?
Where I want to use viewHelper:
$defaults = $_GET;
$defaults['randomText'] = 'Something';
$defaults['something'] = 'placeholder';
$form = new Extension_Form();
$element = new Extension_Form_Element_Xhtml('randomText', $this->view->SpanAdder($defaults['randomText'])); // I'm creating Xhtml element, where I'm replacing content with viewHelper return. Want to get rid of $this->view->SpanAdder part :)
$element->setLabel('Label');
$form->addElement($element);
unset($defaults['randomText']); // I want to get rid of this line, but unfortunately I have to have it, otherwise SpanAdder result will be overwritten.
$form->setDefaults($defaults);
And I have viewHelper, which gives span around my value.
class View_Helper_SpanAdder extends Zend_View_Helper_Abstract {
public function SpanAdder($value) {
return '<span name="' . $value . '">' . $value . '</span>';
}
}

Writing 'one off' javascript in codeigniter

I'm in the process of working on my first code igniter project. I have some javascript that I need to write to initiate an instance of something on document ready. It's a one off, will never be called again. I need to include it in the head, but there may be a case where I need to do this at the end of the body. So, for example, lets say I have this:
<script>
alert('this is a one off alert');
</script>
What is the best practice in doing this stuff? Is it acceptable practice to put this in the controller? Does it need a model writing for it? Or do I need to create individual views for each script to be in MVC?
Thanks.
JS is part of HTML so it should not be in controller or in model, so it should be in view, as you are using framework then you should keep it in a separate file, you can have separate file for each method in your controller or one single JS file each controller.
also you can make a base_controller in your core folder and extend all your controller with it, there you can set all the default JS and CSS. so if you call an index method for a controller it will load with default JS and CSS and if need to add any new just pass it from the controller as John B said
You can have this function in your helper file
/**
* getting js files from public folder
* #param array $js
* #return string
*/
function js_tag($js) {
$out = "";
if (!empty($js)) {
$js = array_reverse($js);
foreach ($js as $j) {
if (strstr($j, "http:") == "" && strstr($j, "https:") == "") {
if ($j == 'tinymac') {
$out.='<script type="text/javascript" src="' . base_url() . 'jscripts/tiny_mce/tiny_mce.js"></script>' . "\n";
} else {
$out.='<script type="text/javascript" src="' . base_url() . 'public/js/' . $j . '"></script>' . "\n";
}
} else {
$out.='<script type="text/javascript" src="' . $j . '"></script>' . "\n";
}
}
}
$out .= '<script type="text/javascript"> var baseurl = "' . base_url() . '"</script>' . "\n";
return $out;
}
/**
* getting css files from public folder
* #author Amir M
* #param array $css
* #return string
*/
function css_tag($css) {
$out = "";
if (!empty($css)) {
$css = array_reverse($css);
foreach ($css as $c) {
if (strstr($c, "http:") == "" && strstr($c, "https:") == "") {
$out.= link_tag(base_url() . "public/css/" . $c) . "\n";
} else {
$out.= link_tag($c) . "\n";
}
}
}
return $out;
}
and you can call it from controller
public function index(){
$data['js'] = js_tag('one.js', 'two.js' , 'jquery.js');
$data['css'] = css_tag('one.css', 'two.css' , 'jquery-ui.css');
$thuis->load->view('index' , $data);
}
Note: the above method will reverse the array so the last value in the array will come first on the page so keep the jQuery last in the array
in the header which is common on all website
<?php echo $js.$css?>
generally the practice I use is this:
All view files load a common header. And the view files are loaded at the end of the controller.
You can either hardcode or pass the links for scripts you want to initiate from the controller. Hardcoding the script tags is self explanatory. If you want to do it a bit more dynamically, you could set up an array of script sources and just loop through them in the header.
so in the controller
$data['scripts'] = array();
$data['scripts'][] = 'http://yoursourcehere';
or if self hosting:
$data['scripts'][] = site_url('assets/js/yourscript.js');
then the view
if(isset($scripts))
{
foreach($scripts as $script)
{
echo '<script type="text/javascript" src="'.$script.'"></script>';
}
}
so basically you can just put all your custom script in a separate file an load it this way. Its practical because it can load all your scripts at once in a common way.

How to populate a custom zend_form_element with $form->populate() and $form->isValid()

I've created a custom Zend_form_element following a tutorial to have some custom input data. Everything is more or less okay, it displays correctly and so on. What i need to do is to populate it when i display the form for updating or when displaying the form when it does not pass validation. Here is the code for my custom element:
class ZC_Form_Element_TabellaRendite
extends Zend_Form_Element_Xhtml
{
public $helper = "tabellaRenditeElement";
private $_data;
// the second paramater was added by me, i'll explain why below
function __construct($spec, $data = null){
$this->_data = $data;
parent::__construct($spec);
}
public function setValue() {
}
public function getValue() {
return $this->_data;
}
}
And here is the helper function
class ZC_View_Helper_TabellaRenditeElement
extends Zend_View_Helper_FormElement
{
protected $html = '';
public function tabellaRenditeElement ($name, $value=null, $attribs = null){
//Here the $attribs are correctly the $specs i passed, the $value only has some value because of the workaround i explain below
$helper = new Zend_View_Helper_FormText();
$helper->setView($this->view);
fb($value, 'value in ');
fb($name, 'name');
$options = array('class'=> 'somma','size'=> 4);
$optionsReadonly = array('readonly' => 1, 'class'=> 'totale', 'size'=> 4);
if (!$attribs['modificabile']){
$options['readonly'] = 1;
}
$this->html .= "
<table class='display datatablesRendite' id='tableRendite' style='border:1px solid;'>
<thead>
<tr bgcolor='#B8D3E8'>
<th>RENDITA da LOCAZIONI (canone di locazione - manutenzione)</th>
<th>Importo</th>
</tr>
</thead>
<tbody>";
$this->html .= '<tr>';
$this->html .= '<td>LOCALI COMMERCIALI - IMPIANTI SPORTIVI</td>';
$this->html .= '<td>';
$this->html .= $helper->formText("renditaImpianti",$value['renditaImpianti'], $options);
$this->html .= '</td>';
$this->html .= '</tr>';
$this->html .= '<tr>';
$this->html .= '<td>LOCALI COMMERCIALI - AGGIUNTI (servizio di ristorazione)</td>';
$this->html .= '<td>';
$this->html .= $helper->formText("renditaAggiunte", $value['renditaAggiunte'], $options);
$this->html .= '</td>';
$this->html .= '</tr>';
$this->html .= '</tbody></table>';
return $this->html;
}
}
I'm totally new to the zend_framework and this is obviously wron, as you se i added a second parameter called data to the __construct of the element: i've done this because when i create my form and pass the data to populate it i don't know how to have it passed to the helper. So i made the workaround of passing the data directly to the custom zend_form_element in the constructor and (i don't know why) it works.
This means that if i do
$form = new My_Form();
$form->populate($data);
or
$form = new My_Form();
$form->isValid($_POST);
The $value in the helper is empty.
So in the init() function of the form i pass the $data to the custom element like this:
$myCustomElement = new My_custom_element($specs, $data);
and i pass the data to the form on creation
$form = new My_Form($data);//this way i pass the data to populate custom elements
$form->populate($data);//this way i populate all the standard elements
Same for isValid()
$form = new My_Form($_POST);//this way i pass the data to populate custom elements
$form->isValid($_POST);//this way i populate all the standard elements
This way everythings work ok, but i'm sure this is pretty wrong: my boss finally gave me half a day to refactor the code and so i want to poulate bothe the custom and standard fields with $form->populate() and $form->isValid().
P.S. maybe i got everything wrong and this is not the correct way to do what i wanted to do: feel free to point out the correct way, i'm new to the framework and i i hadn't the time to understand it fully.
I think that, as far as populate is concerned, what should be enough is to have ZC_Form_Element_TabellaRendite as follows:
class ZC_Form_Element_TabellaRendite extends Zend_Form_Element_Xhtml {
public $helper = "tabellaRenditeElement";
/**
* Is the value provided valid?
*
*
*#param string $value
*#param mixed $context
*#return bool
*/
public function isValid($value, $context = null) {
// you need to specify what it means that your element is valid or not.
}
}
You don't need to create any variables for your data. Methods from Zend_Form_Element will take care of this. With this you can set the element value as, e.g.:
$t = new ZC_Form_Element_TabellaRendite('somename', array('modificabile' =>'1'));
$t->setValue($data);
Also you should be able to populate the form with this element as, e.g.:
$data2Populate = array(
'somename' => array(
'renditaImpianti' => 112,
'renditaAggiunte' => 132
)
);
$myForm = new My_Form();
$myForm->populate($data2Populate);
Hope this helps.

Simple template var replacement, but with a twist

So I'm setting up a system that has a lot of emails, and variable replacement within it, so I'm writing a class to manage some variable replacement for templates stored in the database.
Here's a brief example:
// template is stored in db, so that's how this would get loaded in
$template = "Hello, %customer_name%, thank you for contacting %website_name%";
// The array of replacements is built manually and passed to the class
// with actual values being called from db
$replacements = array('%customer_name%'=>'Bob', '%website_name%'=>'Acme');
$rendered = str_replace(array_keys($replacements), $replacements, $template);
Now, that works well and good for single var replacements, basic stuff. However, there are some places where there should be a for loop, and I'm lost how to implement it.
The idea is there'd be a template like this:
"hello, %customer_name%, thank you for
requesting information on {products}"
Where, {products} would be an array passed to the template, which the is looped over for products requested, with a format like:
Our product %product_name% has a cost
of %product_price%. Learn more at
%product_url%.
So an example rendered version of this would be:
"hello, bob, thank you for requesting
information on:
Our product WidgetA has a cost of $1.
Learn more at example/A
Our product WidgetB has a cost of $2.
Learn more at example/B
Our product WidgetC has a cost of $3.
Learn more at example/C.
What's the best way to accomplish this?
Well, I really dont see the point in a template engine that uses repalcements/regex
PHP Is already a template engine, when you write <?php echo $var?> its just like doing <{$var}> or {$var}
Think of it this way, PHP Already translates <?php echo '<b>hello</b>'?> into <b>hello</b> by its engine, so why make it do everything 2 times over.
The way i would implement a template engine is like so
Firstly create a template class
class Template
{
var $vars = array();
function __set($key,$val)
{
$this->vars[$key] = $val;
}
function __get($key)
{
return isset($this->vars[$key]) ? $this->vars[$key] : false;
}
function output($tpl = false)
{
if($tpl === false)
{
die('No template file selected in Template::output(...)');
}
if(!file_exists(($dir = 'templates/' . $tpl . '.php')))
{
die(sprintf('Tpl file does not exists (%s)',$dir));
}
new TemplateLoader($dir,$this->vars);
return true;
}
}
This is what you use in your login such as index.php, you will set data just like an stdClass just google it if your unsure. and when you run the output command it sends the data and tpl to the next class below.
And then create a standalone class to compile the tpl file within.
class TemplateLoader
{
private $vars = array();
private $_vars = array(); //hold vars set within the tpl file
function __construct($file,$variables)
{
$this->vars = $variables;
//Start the capture;
ob_start();
include $file;
$contents = ob_get_contents();
ob_end_clean(); //Clean it
//Return here if you wish
echo $contents;
}
function __get($key)
{
return isset($this->vars[$key]) ? $this->vars[$key] : (isset($this->_vars[$key]) ? $this->_vars[$key] : false) : false;
}
function __set($key,$val)
{
$this->_vars[$key] = $val;
return true;
}
function bold($key)
{
return '<strong>' . $this->$key . '</string>';
}
}
The reason we keep this seperate is so it has its own space to run in, you just load your tpl file as an include in your constructor so it only can be loaded once, then when the file is included it has access to all the data and methods within TemplateLoader.
Index.php
<?php
require_once 'includes/Template.php';
require_once 'includes/TemplateLoader.php';
$Template = new Template();
$Template->foo = 'somestring';
$Template->bar = array('some' => 'array');
$Template->zed = new stdClass(); // Showing Objects
$Template->output('index'); // loads templates/index.php
?>
Now here we dont really want to mix html with this page because by seperating the php and the view / templates you making sure all your php has completed because when you send html or use html it stops certain aspects of your script from running.
templates/index.php
header
<h1><?php $this->foo;?></h1>
<ul>
<?php foreach($this->bar as $this->_foo):?>
<li><?php echo $this->_foo; ?></li>
<?php endforeach; ?>
</ul>
<p>Testing Objects</p>
<?php $this->sidebar = $this->foo->show_sidebar ? $this->foo->show_sidebar : false;?>
<?php if($this->sidebar):?>
Showing my sidebar.
<?php endif;?>
footer
Now here we can see that were mixing html with php but this is ok because in ehre you should only use basic stuff such as Foreach,For etc. and Variables.
NOTE: IN the TemplateLoader Class you can add a function like..
function bold($key)
{
return '<strong>' . $this->$key . '</string>';
}
This will allow you to increase your actions in your templates so bold,italic,atuoloop,css_secure,stripslashs..
You still have all the normal tools such as stripslashes/htmlentites etc.
Heres a small example of the bold.
$this->bold('foo'); //Returns <strong>somestring</string>
You can add lots of tools into the TempalteLoader class such as inc() to load other tpl files, you can develop a helper system so you can go $this->helpers->jquery->googleSource
If you have any more questions feel free to ask me.
----------
An example of storing in your database.
<?php
if(false != ($data = mysql_query('SELECT * FROM tpl_catch where item_name = \'index\' AND item_save_time > '.time() - 3600 .' LIMIT 1 ORDER BY item_save_time DESC')))
{
if(myslq_num_rows($data) > 0)
{
$row = mysql_fetch_assc($data);
die($row[0]['item_content']);
}else
{
//Compile it with the sample code in first section (index.php)
//Followed by inserting it into the database
then print out the content.
}
}
?>
If you wish to store your tpl files including PHP then that's not a problem, within Template where you passing in the tpl file name just search db instead of the filesystem
$products = array('...');
function parse_products($matches)
{
global $products;
$str = '';
foreach($products as $product) {
$str .= str_replace('%product_name%', $product, $matches[1]); // $matches[1] is whatever is between {products} and {/products}
}
return $str;
}
$str = preg_replace_callback('#\{products}(.*)\{/products}#s', 'parse_products', $str);
The idea is to find string between {products} and {products}, pass it to some function, do whatever you need to do with it, iterating over $products array.
Whatever the function returns replaces whole "{products}[anything here]{/products}".
The input string would look like that:
Requested products: {products}%product_name%{/products}

Categories