PDA

View Full Version : implementing php function and an array into a template


egysc
04-06-2012, 11:02 PM
how can I implement a php function and an array into a template some thing similar to :<?php
function draw_rand_array($array,$draws)
{
$lastIndex = count($array) - 1;
$returnArr = array();
while($draws > 1)
{
$rndIndex = rand(0,$lastIndex);
array_push($returnArr,array_splice($array,$rndInde x,1));
$draws--;
$lastIndex--;
}

return $returnArr;
}
?>

kh99
04-06-2012, 11:40 PM
You can't put php in a template, it has to go in a plugin. You can then register a variable (in your plugin code) to a template to display it. In the case of an array, you can then use <vb:each> tags in the template like:

<vb:each from="myarray" value="item">
//Code to display an item
{vb:raw item}
</vb:each>



For instructions on registering variables to a template, you can look at this article: https://vborg.vbsupport.ru/showthread.php?t=228078 . If you are trying to use your array in an existing template, you will want to use the vB_Template::preRegister() function.