|
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 |
artik Smarty Rookie
Joined: 04 May 2019 Posts: 8
|
Posted: Fri Mar 04, 2022 7:46 pm Post subject: Check in a foreach if 2 or more items have the same value |
|
|
HI,
I'm facing a wall when I'm trying to success with the feature I want.
Let's say I have an array:
$products:
Code: | Array (4)
0 => Array
prod_id => "1"
category => "4"
1 => Array (103)
prod_id => "2"
category => "1"
2 => Array (103)
prod_id => "3"
category => "2"
3 => Array (103)
prod_id => "4"
category => "4"
4 => Array (103)
prod_id => "5"
category => "8"
5 => Array (103)
prod_id => "6"
category => "7"
6 => Array (103)
prod_id => "7"
category => "8" |
I'll do my foreach :
Code: | <select>
{foreach $products as $similar}
<option
value="{$similar.prod_id}"
>
</option>
{/foreach}
</select> |
But what I want in this foreach is to count the amount of product with the same category and list them in another select below.
In this case it will create another select with the 2 products that have category 4 and 8.
Any hint on how I can perform this ? |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Mon Mar 07, 2022 5:45 am Post subject: |
|
|
Try this:
Code: | Products:
<select>
{foreach $products as $similar}
<option value="{$similar.prod_id}">Product {$similar.prod_id}</option>
{$category[$similar.category][] = $similar}
{/foreach}
</select>
<br>
{foreach $category as $cat_id => $prod_list}
<br>
Products in Category {$cat_id}:
<select>
-Click to View-
{foreach $prod_list as $prod}<option>Product {$prod.prod_id}</option>{/foreach}
</select>
{/foreach}
<br> |
|
|
Back to top |
|
artik Smarty Rookie
Joined: 04 May 2019 Posts: 8
|
Posted: Mon Mar 07, 2022 11:51 pm Post subject: |
|
|
It works, but is there a way to "merge" the products with the same category in te first select ? |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Tue Mar 08, 2022 2:12 am Post subject: |
|
|
It does.
"Products in Category 4" has a drop-down that has products 1 and 4. "Products in Category 8" has a drop-down that has products 5 and 7.
If this is not what you want, then re-phrase the description of what you do want to see. |
|
Back to top |
|
artik Smarty Rookie
Joined: 04 May 2019 Posts: 8
|
Posted: Tue Mar 08, 2022 2:03 pm Post subject: |
|
|
As you can see, I was able in the third select to sort products that as the same "category_id" from the second one (thanks to your fix)
But in the second select, I'd like to merge these 4 products into 2 (to allow to user to make their choice in the third select).
I tried in the foreach to break of making a count, and display only one product of each (no matter wich one), but I stooped, running out of ideas |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Tue Mar 08, 2022 5:11 pm Post subject: |
|
|
Please use the array given in the OP to describe EXACTLY what you want to see, using the names of the array elements.
(I don't know what your first, second, and third selects are supposed to be with different data items and one screenshot.) |
|
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
|