views:

46

answers:

1

First-timer here, I hope I explain this well enough...

PHP/Smarty, I'm working on a section of a page that displays bullet lists of notes associated with either the general page, or individual places on that page. Some places don't have notes. Something like:

General

  • note 1

New York

  • note 2
  • note 3

Boston

  • note 4

I have two arrays assigned to the .tpl I'm working with and populated by the UI class: $places and $notes

$places contains place objects, each unique by place_id

$notes contains uniquely identified note objects, each of which may or may not have a place_id as one of the attributes

here's what i'm thinking:

{if (**ANY NOTES EXIST IN $notes WITH NO place_id**)}
    <ul id="list-general">
        <h4>General</h4>
        {foreach from=**[NOTES WITH NO place_id]** item=note}
            <li id="note-{$note->get_id()}">$note->get_text()</li> 
    {/foreach}
    </ul>
{else}
    <ul id="list-general" class="hide">
        <h4>General</h4>
    </ul>
{/if}

{foreach from=$places item=place}
     {assign var=curr_place_id value=$place->get_id()}
     {if (**ANY NOTES EXIST IN $notes WHERE place_id == $curr_place_id**)}
     <ul id="list-{$curr_place_id}">
             <h4>{$place->get_name()}</h4>
             {foreach from=**[NOTES WHERE place_id == $curr_place_id]** item=note}
                 <li id="note-{$note->get_id()}">$note->get_text()</li> 
         {/foreach}
         </ul>
     {else}
        <ul id="list-{$curr_place_id}" class="hide">
            <h4>{$place->get_name()}</h4> 
        </ul>
     {/if}
{/foreach}

I'm pretty new to all of this, and I'm having a hard time with the items in CAPS...so, my question is: what's the best way to evaluate the attributes of the objects in the two different arrays, and possibly build temp arrays that contain only the notes that i need.

(In case you're wondering, I'm creating the hidden nodes so I can access them via javascript and don't have to worry about order)

+2  A: 

Hi Byron,

While I'm guessing it is possible to do this in Smarty, it does seem that you're mixing your business logic and presentation logic here. Personally, I'd consider splicing your array in in two parts (general and places) in the PHP calling this template.

$placesNotes = array();
$generalNotes = array();

array_filter($notes, function($note) {
    global $placesNotes, $generalNotes;
    if($note->get_place_id() == null) {
        $generalNotes[] = $note;
    } else {
        $placesNotes[$note->get_place_id()][] = $note;
    }
});

(note: this uses an anonymous function, so >5.3 only - if you want this to run on older versions, move the function to it's own named function and use the Callback type)

You then end up with two arrays you easily loop over, using an isset($notes_places.$curr_place_id) within the Places-loop to find out if the place has any notes.

Also, you forgot the {}'s around the get_text() calls.

I've whipped up a quick sample, to be found at: http://gist.github.com/479392 - hope that helps?

BTW: Your question was very clear, and explained what you want to do, even detailing the decision behind what you were doing when it wasn't immediately obvious - so for a first-time: excellent question!

kander
thanks kander, makes sense. i'll give it a shot this morning.
byron
works like a charm. FYI, i used a foreach in the PHP instead of arrayfilter..just couldn't get arrayfilter working. thanks a lot for your help.
byron
You're welcome :)
kander