Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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 Code:
    <?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.

  • #2
    New Coder
    Join Date
    Dec 2006
    Location
    Denver, Colorado
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.,
    PHP Code:
    $var "apples, ";
    $var "pears";
    echo 
    $var;
    //outputs 'pears' 
    as opposed to
    PHP Code:
    $var "apples, ";
    $var .= "pears";
    echo 
    $var;
    //ouputs 'apples, pears' 
    That might help you out.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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:
    PHP Code:
    // 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:
    PHP Code:
    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?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •