[solved] help with "for each" statement

More
13 years 3 months ago - 13 years 3 months ago #18159 by bobthebob01
Hi,
I'm not a php guru, but i know enough to modify things around to suit my need. But this time, i'm hitting a wall can not figure out how to achieve my result.

Here is the situation:
I working in a category view.
I have 2 custom fields: an image (called artist-image in my template) and a text (called artist-subtitle in my template) fields.

I'm adapting a script that displays a the title and a subtitle of the item when hovering the image.

My position are set-up fine and works when i use a basic template.

I manage to make everything working fine, but i have a problem with my subtitle field.
here is the code that works without the subtitle output:
Code:
<?php /** * @version 1.5 stable $Id: category_items.php 481 2011-03-01 07:14:56Z emmanuel.danan@gmail.com $ * @package Joomla * @subpackage FLEXIcontent * @copyright (C) 2009 Emmanuel Danan - www.vistamedia.fr * @license GNU/GPL v2 * * FLEXIcontent is a derivative work of the excellent QuickFAQ component * @copyright (C) 2008 Christoph Lukes * see www.schlu.net for more information * * FLEXIcontent is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ defined( '_JEXEC' ) or die( 'Restricted access' ); // first define the template name $tmpl = $this->tmpl; ?> <?php $items = $this->items; $count = count($items); if ($count) : ?> <?php $leadnum = $this->params->get('lead_num', 100); $leadnum = ($leadnum >= $count) ? $count : $leadnum; if ($this->limitstart == 0) : ?> <div id="catview-artist-list"> <?php for ($i=0; $i<$leadnum; $i++) : ?> <?php if (isset($items[$i]->positions['artist-image'])) : ?> <?php foreach ($items[$i]->positions['artist-image'] as $field) : ?> <div class="catview-artist-image rounded7px"> <div class="mosaic-block fade"> <a href="<?php echo JRoute::_(FlexicontentHelperRoute::getItemRoute($items[$i]->slug, $items[$i]->categoryslug)); ?>" class="mosaic-overlay"> <div class="details"> <h4><?php echo $this->escape($items[$i]->title); ?></h4> <p>subtitle field should go here</p> </div> <div class="mosaic-backdrop"><?php echo $field->display; ?></div> </div> </div> <?php endforeach; ?> <?php endif; ?> <?php endfor; ?> </div> <?php endif; ?> <?php endif; ?>

But i need to have the subtitle to be an output from a field entry of the item "subtitle field should go here"

But if i try to place my artist-subtitle position in between the artist-image position, i get an error and it breaks everything or it does not display properly such as this try:
Code:
<?php /** * @version 1.5 stable $Id: category_items.php 481 2011-03-01 07:14:56Z emmanuel.danan@gmail.com $ * @package Joomla * @subpackage FLEXIcontent * @copyright (C) 2009 Emmanuel Danan - www.vistamedia.fr * @license GNU/GPL v2 * * FLEXIcontent is a derivative work of the excellent QuickFAQ component * @copyright (C) 2008 Christoph Lukes * see www.schlu.net for more information * * FLEXIcontent is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ defined( '_JEXEC' ) or die( 'Restricted access' ); // first define the template name $tmpl = $this->tmpl; ?> <?php $items = $this->items; $count = count($items); if ($count) : ?> <?php $leadnum = $this->params->get('lead_num', 100); $leadnum = ($leadnum >= $count) ? $count : $leadnum; if ($this->limitstart == 0) : ?> <div id="catview-artist-list"> <?php for ($i=0; $i<$leadnum; $i++) : ?> <?php if (isset($items[$i]->positions['artist-image'])) : ?> <?php foreach ($items[$i]->positions['artist-image'] as $field) : ?> <div class="catview-artist-image rounded7px"> <div class="mosaic-block fade"> <a href="<?php echo JRoute::_(FlexicontentHelperRoute::getItemRoute($items[$i]->slug, $items[$i]->categoryslug)); ?>" class="mosaic-overlay"> <div class="details"> <h4><?php echo $this->escape($items[$i]->title); ?></h4> <?php if (isset($items[$i]->positions['artist-subtitle'])) : ?> <?php foreach ($items[$i]->positions['artist-subtitle'] as $field) : ?> <p><?php echo $field->display; ?></p> <?php endforeach; ?> <?php endif; ?> </div> <div class="mosaic-backdrop"><?php echo $field->display; ?></div> </div> </div> <?php endforeach; ?> <?php endif; ?> <?php endfor; ?> </div> <?php endif; ?> <?php endif; ?>

I have a feeling it's something stupid, but i can figure it out.

I guess i need a first "for each item" statement and then use the "for each position" statement.

Any help would be greatly appreciated.
Last edit: 13 years 3 months ago by bobthebob01.

Please Log in or Create an account to join the conversation.

More
13 years 3 months ago #18161 by ggppdk
You have been detailed in your POST, so i was able to easily spot one problem.

I see that besides naming your fields: artist-image AND artist-subtitle. You have also have field positions with these names. But this is not a problem

Reason first code work but second code does not is that you have 2 foreach LOOPs, one inside the other that use same variable: $field
Rename variable for second loop:
Code:
<?php foreach ($items[$i]->positions['artist-subtitle'] as $field) : ?> <p><?php echo $field->display; ?></p> <?php endforeach; ?>
like this:
Code:
<?php foreach ($items[$i]->positions['artist-subtitle'] as $fieldST) : ?> <p><?php echo $fieldST->display; ?></p> <?php endforeach; ?>

This should work out this problem

Regards


-- Flexicontent is Free but involves a big effort on our part.
Like the our support? (for a bug-free FC, despite having a long list of functions) Like the features? Like the ongoing development and future commitment to FLEXIcontent?
-- Add your voice to the FLEXIcontent JED listing with a 5-star...

Please Log in or Create an account to join the conversation.

More
13 years 3 months ago #18162 by bobthebob01
Once again: nice one ggppdk!

you do know your code. it works perfectly.

So i can learn for future project.
What did i do wrong exactly beside not naming as you suggest.

is it because the position and the field name are the same?

Is there a way i could have achieve this with the way it is done in the default template:
Code:
<?php foreach ($items[$i]->positions['artist-subtitle'] as $field) : ?> <p><?php echo $field->display; ?></p> <?php endforeach; ?>

Please Log in or Create an account to join the conversation.

More
13 years 3 months ago #18163 by ggppdk
I guess you are using some javascript , so you needed to do what you did.

I simply pointed out that you had:
Code:
<?php foreach ($items[$i]->positions['artist-image'] as $field) : ?> ... <?php foreach ($items[$i]->positions['artist-subtitle'] as $field) : ?> <p><?php echo $field->display; ?></p> <?php endforeach; ?> ... <p><?php echo $field->display; ?></p> ... <?php endforeach; ?>
Inner foreach change the variable of the outer foreach
so the second:
<?php echo $field->display; ?>
will not print your image but the subtitle.

Regards


-- Flexicontent is Free but involves a big effort on our part.
Like the our support? (for a bug-free FC, despite having a long list of functions) Like the features? Like the ongoing development and future commitment to FLEXIcontent?
-- Add your voice to the FLEXIcontent JED listing with a 5-star...

Please Log in or Create an account to join the conversation.

More
13 years 3 months ago #18178 by bobthebob01
got it.
And yes i am using some javascript to achieve the effect.

Correct me if i'm wrong but i just could have define it by anything else basically. Just needed to be different so the flexicontent does not get confused on what to "echo", right?
it could have been "... as $fieldABC" and or any word for that matter and it would have worked?

thanks for taking your time to explain.

Please Log in or Create an account to join the conversation.

More
13 years 3 months ago #18181 by ggppdk
Yes, the variable will have the last value you assign to it. You could name it anything else.

Regards


-- Flexicontent is Free but involves a big effort on our part.
Like the our support? (for a bug-free FC, despite having a long list of functions) Like the features? Like the ongoing development and future commitment to FLEXIcontent?
-- Add your voice to the FLEXIcontent JED listing with a 5-star...

Please Log in or Create an account to join the conversation.

Moderators: vistamediajoomlacornerggppdk
Time to create page: 0.581 seconds
Save
Cookies user preferences
We use cookies to ensure you to get the best experience on our website. If you decline the use of cookies, this website may not function as expected.
Accept all
Decline all
Essential
These cookies are needed to make the website work correctly. You can not disable them.
Display
Accept
Analytics
Tools used to analyze the data to measure the effectiveness of a website and to understand how it works.
Google Analytics
Accept
Decline