Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 940119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:54:09+00:00 2026-05-15T21:54:09+00:00

First-timer here, I hope I explain this well enough… PHP/Smarty, I’m working on a

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:54:10+00:00Added an answer on May 15, 2026 at 9:54 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.