foreach help to understanding

gaga

Member
Hello,
I want a little help explain to me what went wrong.
I want to load data from external json file.

My template is

Code:
<xen:foreach loop="$news" value="$new">
               <li>
               <span class="title">{$new.title}</span>
               <span class="subtitle">{$new.stitle}</span>
               </li>
   </xen:foreach>

And receive this error>
  • Illegal string offset 'title' in C:\wamp\www\Xenforo5\library\XenForo\Template\Abstract.php(265) : eval()'d code, line 48:
    47: <li>
    48: <span class="title">' . htmlspecialchars($new['title'], ENT_QUOTES, 'UTF-8') . '</span>
    49:

The controller is:

PHP:
$Helper =   new Myapp_Helper_News();
$News                   =
                array(
                $title   = $Helper->getTitle(),
                $stitle   = $Helper>getSTitle(),                              
                );
             
                $params     = $template->getParams();
                $hookParams = array(
                    'news'       => $News

And the Helper_News class for this is:

PHP:
 public function getTitle()
    {      
         for ($i = 0; $i < 5; $i++) {
       
        $text = $this->news[$i]->title;
    }
    return $text;
    }

I want to load last 5 News from json file.

Api structure is
results->channel->item->news[0]

Thanks for helping undestand this problem.

Regards.
 
You might try to add this template tag within the foreach to see what is being returned.

{xen:helper dump, $new}
 
PHP:
$News                   =
                array(
                $title   = $Helper->getTitle(),
                $stitle   = $Helper>getSTitle(),                             
                );

Should be:
PHP:
$News                   =
                array(
                'title'   => $Helper->getTitle(),
                'stitle'   => $Helper>getSTitle()                              
                );
 
  • Like
Reactions: LPH
PHP:
$News                   =
                array(
                $title   = $Helper->getTitle(),
                $stitle   = $Helper>getSTitle(),                            
                );

Should be:
PHP:
$News                   =
                array(
                'title'   => $Helper->getTitle(),
                'stitle'   => $Helper>getSTitle()                             
                );
Nothing happens

On {xen:helper dump, $new}
give:
array(4) {
["title"] => string(25) "Test3Test5Test5Test5Test0"
["stitle"] => string(90) "testtestetstests"

}
 
Nothing happens

On {xen:helper dump, $new}
give:

Is the title key containing an array of items? If so your template code needs to reflect that (assuming stitle is an array too with the same indexes as title):

Code:
<xen:foreach loop="$news.title" value="$title" key="$key">
               <li>
               <span class="title">{$title.{$key}}</span>
               <span class="subtitle">{$news.stitle.{$key}}</span>
               </li>
   </xen:foreach>

After looking a little further, your helper is just returning a string of text that gets over-written with each $i++. Is $text suppose to be returning an array, or a string of concatenated text?

PHP:
 public function getTitle()
{
       // if an array 
        $text = array();
         for ($i = 0; $i < 5; $i++) {
     
        $text[$i] = $this->news[$i]->title;
        }
    return $text;
}

public function getTitle()
{
        // if a string of text
         $text = '';
         for ($i = 0; $i < 5; $i++) {
     
        $text.= $this->news[$i]->title;
        }
    return $text;
 }
 
Last edited:
Tanks.
Now gives just one error:
  1. Invalid argument supplied for foreach() in C:\wamp\www\Xenforo5\library\XenForo\Template\Abstract.php(265) : eval()'d code, line 44:
    43: ';
    44: foreach ($news['title'] AS $key => $title)
    45: {
 
Consider that every news has its own title and sub title.

From api it looks like this ...


Code:
results->channel->item->news[0]->title
results->channel->item->news[0]->stitle

results->channel->item->news[1]->title
results->channel->item->news[1]->stitle

results->channel->item->news[2]->title
results->channel->item->news[2]->stitle

results->channel->item->news[3]->title
results->channel->item->news[3]->stitle

results->channel->item->news[4]->title
results->channel->item->news[4]->stitle

results->channel->item->news[5]->title
results->channel->item->news[5]->stitle
 
Consider that every news has its own title and sub title.

From api it looks like this ...


Code:
results->channel->item->news[0]->title
results->channel->item->news[0]->stitle

results->channel->item->news[1]->title
results->channel->item->news[1]->stitle

results->channel->item->news[2]->title
results->channel->item->news[2]->stitle

results->channel->item->news[3]->title
results->channel->item->news[3]->stitle

results->channel->item->news[4]->title
results->channel->item->news[4]->stitle

results->channel->item->news[5]->title
results->channel->item->news[5]->stitle

I'm not sure what your variable actually is. Is it $results['channel']['item']?
If it is then this should work:
HTML:
<xen:foreach loop="$results.channel.item" value="$new">
               <li>
               <span class="title">{$new.title}</span>
               <span class="subtitle">{$new.stitle}</span>
               </li>
   </xen:foreach>
 
Back
Top Bottom