...

View Full Version : Question: How do I display a list of an array in a template?



Attilitus
01-05-2007, 03:36 PM
I am sure it is something silly, either that or it can't be done at all.

This is what I mean:

I have a script that allows me to:

In php I can get the right value by putting:

$myarray[$increment]->fieldtodisplay

Which will pull all fields from the result list that I queried in the previous function.

Now whenever I try to add the above variable into a templating system it will only output the first value. The query itself is working fine, it is just the display end of it. For example if I go to the "next page" it will display the correct FIRST ENTRY of that page.

Entry into template is: {$myarray[$increment]->fieldtodisplay}

Is there anyway to get templates to correctly display ALL values of custom arrays?

Here are some examples of what I want to do:



<?php

// define array
$artists = array('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses');
// loop over it and print array elements
for ($x = 0; $x < sizeof($artists); $x++) {
echo '<li>'.$artists[$x];
}

?>

</ul>
</body>
</html>

When you run this script, here's what you'll see:

My favourite bands are:


* Metallica
* Evanescence
* Linkin Park
* Guns n Roses

Taken right off of zend.com

echo functions will display the correct value when displayed within the for() loop, but not when outside.

When defining a variable within the for() loop a value is assigned correctly and displayed in the template, but only the last one. (not the list).

I need to have a variable pulled by a template display the entire list. Could anyone point me in the right direction?

I really appreciate any help you guys can give me.

wordnerd
01-05-2007, 05:53 PM
Well, I can't really figure out what you're trying to do, but the 'only the last one' is displaying problem sounds like you're dumping values from an array into a variable with a simple declaration rather than a continuing one.

ex.,


$var = "apples, ";
$var = "pears";
echo $var;
//outputs 'pears'


as opposed to


$var = "apples, ";
$var .= "pears";
echo $var;
//ouputs 'apples, pears'


That might help you out.

Fou-Lu
01-06-2007, 05:42 AM
I'm not sure I quite get you either on this one...
Generally when I work with template based systems, I make use of an eval() or custom function which calls the eval(). Now, eval is a sweet function, since you can store the value of an output set into the variable if specified:


// Note, stolen directly from php.ca site:
<?php
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
?>

Now, lets assume that the non evaluated $str is an html template instead. Still works fine and dandy, although you will need to play with the escaping on the eval since the $str if fetched from a db for instance will be a little different. Anyway, to parse multiple values into the same variable, you simply append the value of the output eval:


for (;;)
{
if ($i >= count($arrayVal))
{
break;
}

eval("\$str .= \"$arrayVal[$i]\";");
$i++;
}

Mmkay. So, first, never use your code like this. I don't really recommend breaking from a loop if you can avoid it (although not wrong), but breaking from an infinite is risky. I really should change it, but meh, I'm lazy. Note the one major difference in the eval call: .. There is a period before the assignment operator - this will store each value of the array into the $str. Definitly helpful with template systems.
Does this help you at all?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum