|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
muralik Smarty Rookie
Joined: 08 Mar 2004 Posts: 25
|
Posted: Thu Nov 04, 2004 8:49 am Post subject: array looping with single element |
|
|
i have a problem looping with array when there is single element
[php:1:d1ed56f972]
[CustomerReviews] => Array
(
[AverageRating] => 5
[TotalReviews] => 1
[Review] => Array
(
[ASIN] => B00026FXPO
[Rating] => 0
[HelpfulVotes] => 0
[TotalVotes] => 0
[Date] => 2004-07-11
[Summary] => Great 1st helmet
[Content] => We just bought our 2 year old daughter her first tricycle,
)
[/php:1:d1ed56f972]
i am using smarty code like this
Code: |
{assign var="reviews" value=$item.CustomerReviews.Review}
{foreach from=$reviews item=r}
<strong>Summary: </strong>{$r.Summary} <br>
Date: {$r.Date} - Rating: {$r.Rating}<br><br>
<strong>Comment: </strong>{$r.Content}
<hr size=0 color='gray'>
{/foreach}
|
here $item is my main array, this is working when i have multiple reviews,
when it is single element, it is displaying first letter of each element.
what do i have to do when i have only element.
i also tried section, same problem
i have to use foreach there because sometimes it contains more than one element in array.
it is working with following array
Code: |
[CustomerReviews] => Array
(
[AverageRating] => 4.36
[TotalReviews] => 11
[Review] => Array
(
[0] => Array
(
[ASIN] => B00005A1LK
[Rating] => 5
[HelpfulVotes] => 0
[TotalVotes] => 0
[Date] => 2001-11-29
[Summary] => This bike is BEAUTIFUL!
[Content] => If you know from experience with the tricycle that the first bike is constantly
)
[1] => Array
(
[ASIN] => B00005A1LK
[Rating] => 3
[HelpfulVotes] => 0
[TotalVotes] => 0
[Date] => 2004-07-24
[Summary] => Advertisement misinformation
[Content] => I would think that the information of a child's bike that did not
)
[2] => Array
(
[ASIN] => B00005A1LK
[Rating] => 3
[HelpfulVotes] => 26
[TotalVotes] => 27
[Date] => 2004-01-13
[Summary] => NO BRAKES?!?!?!?!?!?
[Content] => Okay... I have to admit that this is an adorable little bike.
)
|
|
|
Back to top |
|
jaey Smarty Regular
Joined: 25 May 2004 Posts: 36 Location: Louisville, Kentucky, USA
|
Posted: Fri Nov 05, 2004 2:40 am Post subject: |
|
|
The problem is you're using two different array structures. For example, when there is a single review, the "Content" field is referenced as:
Code: | $item['CustomerReviews']['Review']['Content'] |
When there are multiple reviews, the same field is referenced as:
Code: | $item['CustomerReviews']['Review'][0]['Content'] |
|
|
Back to top |
|
muralik Smarty Rookie
Joined: 08 Mar 2004 Posts: 25
|
Posted: Fri Nov 05, 2004 4:34 am Post subject: still issues |
|
|
for that reason, i actually did something,
I am counting the array first, and then trying to do it accordingly.
here is my code but still not working
Code: |
{assign var="similar" value=$item.SimilarProducts.SimilarProduct}
{if $similar|@count > 1}
{foreach} blah blah {/foreach}
{else}
$item.SimilarProducts.SimilarProduct.Title
{/if}
|
$similar|@count is generating correctly if there multiple arrays, for eg. if it contains 4 arrays, it is displaying 4, if there is only one array, count is displaying number of elements instead of displaying 1.
what can i do here |
|
Back to top |
|
muralik Smarty Rookie
Joined: 08 Mar 2004 Posts: 25
|
Posted: Fri Nov 05, 2004 6:38 am Post subject: solution |
|
|
dont know if this is proper solution, but fixed like this
wrote a plugin
Code: |
function smarty_function_is_assoc_array($params, &$portal)
{
$result = $params['assign'];
$var = $params['assoc_array'];
if ( (!is_array($var)) || (!count($var)) ) return false;
foreach($var as $k=>$v)
{
if (is_int($k))
{
$portal->assign($result, true);
return false;
}
}
$portal->assign($result, false);
return false;
}
|
then i use like this in my template
Code: |
{assign var="similar" value=$item.SimilarProducts.SimilarProduct}
{is_assoc_array assoc_array=$item.SimilarProducts.SimilarProduct assign=multiple}
{if $multiple}
{foreach from=$similar item=s}
blah blah
{/foreach}
{else}
blah blah
{/if}
|
|
|
Back to top |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Fri Nov 05, 2004 7:12 am Post subject: |
|
|
what about foreachelse tags?
Code: |
{foreach}
blah blah
{foreachelse}
$item.SimilarProducts.SimilarProduct.Title
{/foreach}
|
|
|
Back to top |
|
muralik Smarty Rookie
Joined: 08 Mar 2004 Posts: 25
|
Posted: Fri Nov 05, 2004 7:59 am Post subject: foreach for arrays |
|
|
my foreach is like this
Code: |
{if $item.SimilarProducts.SimilarProduct}
<tr><td colspan="2">
<h3>Similar - People who bought this also bought below</h3>
{assign var="similar" value=$item.SimilarProducts.SimilarProduct}
{is_assoc_array assoc_array=$item.SimilarProducts.SimilarProduct assign=multiple}
{if $multiple}
{foreach from=$similar item=s}
{if $s.Title}
<a href="{$baseURL}product-detail/{$currentid}/{$item.ItemAttributes.ProductGroup|sanitize_title}/{$s.ASIN}">{$s.Title}</a><br>
{/if}
{/foreach}
{else}
{if $item.SimilarProducts.SimilarProduct.Title}
<a href="{$baseURL}product-detail/{$currentid}/{$item.ItemAttributes.ProductGroup|sanitize_title}/{$item.SimilarProducts.SimilarProduct.ASIN}">{$item.SimilarProducts.SimilarProduct.Title}</a><br>
{/if}
{/if}
</td></tr>
<tr><td colspan=2><hr size=0></td></tr>
{/if}
|
|
|
Back to top |
|
giank Smarty n00b
Joined: 20 Sep 2006 Posts: 3
|
Posted: Wed Sep 20, 2006 3:29 am Post subject: I do not understand the Smarty looping behaviour |
|
|
Hello to all.
This is my first post on this forum. It's years I'm using Smarty, before it I had my own templating engine, really nothing compared to it.
I'm pretty sorry for reviving this two years old thread, because I've stumbled upon the very same problem here.
My problem is: if I try to loop an array with a single element, where this element is an array itself, the section control structure would have to simply execute the code once, because there is no need to repeat it. Instead, the control structure processes the array values of the sub elements. I do not understand if this is by design, I hope this is a bug.
Let me explain better.
If I have this:
$people = array (0 => array (id => "1", name => "Patrick", surname => "Stewart"));
doing this
{foreach item=person from=people}
ID: {$person.id}<br />
NAME: {$person.name}<br />
SURNAME: {$person.surname}<br />
<hr />
{/foreach}
This would have to output
ID:1<br />
NAME:Patrick<br />
SURNAME:Stewart<br />
<hr />
right?
Instead the output is like this:
ID:<br />
NAME:<br />
SURNAME:<br />
<hr />
ID:<br />
NAME:<br />
SURNAME:<br />
<hr />
ID:<br />
NAME:<br />
SURNAME:<br />
<hr />
why?
because the data has been processed by the loop structure ( {section} does the same) as if it was
$people = array (id => "1", name => "Patrick", surname => "Stewart");
So loops three times and it can't access the data.
Passing a data structure as follows
$people = array (0 => array (id => "1", name => "Patrick", surname => "Stewart"), 1 => array (id => "2", name => "Jean Luc", surname => "Picard"));
makes the loop work with the expected behaviour, so it loops
ID:1<br />
NAME:Patrick<br />
SURNAME:Stewart<br />
<hr />
ID:2<br />
NAME:Jean Luc<br />
SURNAME:Picard<br />
<hr />
Personally? I think this is a bug. But I would like to have more opinions before filing it into the bugs forum. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|