Keyword

Specific Extra Fields in generic template - Thought I had it!

  • Michael Tull
  • Michael Tull's Avatar Topic Author
  • Offline
  • New Member
More
13 years 6 months ago #95157 by Michael Tull
Hi There,

 

I am looking for some thoughts or help with adding specific extra fields to my generic.php

 

I have searched the forum, and it seems like there is one discussion of it, but it looks a little old as it talks about adding extra fields ( and names) in general.  When I looked at the generic template (and activate the option in parameters ) I can get field names and values in my generic.php item list.. all of them, which is great, but my problem is, that I only want two of the fields on the generic list view.  ( Namely a date and a venue name )

 

I have managed to achive this finction and pnly display the fields I want in a specific catagory list view by modifying the category_item.php and specifying for each field (by ID) that I want to display:

 

<span class="catItemExtraFieldsLabel"><?php echo $this->item->extra_fields[0]->name; ?></span>

 

But when I try to replicate this code in my generic.php I get no values at all.. Obviously, I am missing something.. oh.. And I am no php coder!  I can 'generally' follow along but!!  I am hoping someone can look at what I have done and give me some pointers.

 

Here is the orogional code from generic.php :

          <?php if($item->params->get('genericItemExtraFields') && count($item->extra_fields)): ?>          <div class="genericItemExtraFields">            <h4><?php echo JText::_('Additional Info'); ?></h4>              <ul>                <?php foreach ($item->extra_fields as $key=>$extraField): ?>                <li class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">                    <span class="genericItemExtraFieldsLabel"><?php echo $extraField->name; ?></span>                    <span class="genericItemExtraFieldsValue"><?php echo $extraField->value; ?></span>                        </li>                <?php endforeach; ?>                </ul>            <div class="clr"></div>          </div>          <?php endif; ?>

 

Here is my go at it ( to just dispaly fields 0 and 1 ):



       <?php if($item->params->get('genericItemExtraFields') && count($item->extra_fields)): ?>          <div class="genericItemExtraFields">                                      <ul>                <li>                                <span class="catItemExtraFieldsLabel"><?php echo $this->item->extra_fields[0]->name; ?></span>                    <span class="catItemExtraFieldsValue"><?php echo $this->item->extra_fields[0]->value; ?></span>                </li>                                <li>                                <span class="catItemExtraFieldsLabel"><?php echo $this->item->extra_fields[1]->name; ?></span>                    <span class="catItemExtraFieldsValue"><?php echo $this->item->extra_fields[1]->value; ?></span>                </li>                        </ul>            <div class="clr"></div>          </div>        <?php endif; ?>     

 

The formatting I can deal with, but I am just no getting any values through, and when I look at my code for category_item that IS working.( Thanks to searching the forums : )...

 

<?php if($this->item->params->get('catItemExtraFields') && count($this->item->extra_fields)): ?>      <!-- Item extra fields -->      <div class="catItemExtraFields">          <h3>Event Details</h3>          <ul>                    <li class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">                            <span class="catItemExtraFieldsLabel"><?php echo $this->item->extra_fields[0]->name; ?></span>                <span class="catItemExtraFieldsValue"><?php echo $this->item->extra_fields[0]->value; ?></span>            </li>                         <li class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">                            <span class="catItemExtraFieldsLabel"><?php echo $this->item->extra_fields[2]->name; ?></span>                <span class="catItemExtraFieldsValue"><?php echo $this->item->extra_fields[2]->value; ?></span>            </li>

      </ul>

        <div class="clr"></div>      </div>      <?php endif; ?>

 

So my attempt at the generic.php looks pretty close as far as I can see:  I can see that the params are different, but the count is the same...  The code is the classes is not a problems as far as I can tell.. Yes, Ive removed the loop, but I am specifting the extra_fields[id]...

 

As I siad, I am obviously missing something so any throughts would be appreaciated.

 

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

  • Yiota
  • Yiota's Avatar
  • Visitor
13 years 6 months ago #95158 by Yiota
You can control the extra fields by their name. For example, to get only the extra fields with name Date and Venue you can change the output as follows:

 

<?php if($this->item->params->get('genericItemExtraFields') && count($item->extra_fields)): ?>                            <div class="genericItemExtraFields">                         <?php foreach ($item->extra_fields as $key=>$extraField):?>    

                                                    <?php if ($extraField->name=='Date'): ?>    

                                                               <span class="genericItemExtraFieldsLabel"><?php echo $extraField->name; ?></span>                                     <span class="genericItemExtraFieldsValue"><?php echo $extraField->value; ?></span>

                           <?php elseif ($extraField->name=='Venue'): ?>      

                                                            <span class="genericItemExtraFieldsLabel"><?php echo $extraField->name; ?></span>                                     <span class="genericItemExtraFieldsValue"><?php echo $extraField->value; ?></span>

                            <?php else: echo ''; ?>                             <?php endif; ?>   

                                                 <?php endforeach; ?>                      <div class="clr"></div>                   </div><?php endif; ?>

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

  • Michael Tull
  • Michael Tull's Avatar Topic Author
  • Offline
  • New Member
More
13 years 6 months ago #95159 by Michael Tull
 

Hi Yiota,

 

Thank you so much for your help!  I tried to implement your code as above, changing the field names obviously, but I am guessing there is a syntax error there somewhere as each time the page loaded with this code, it would drop out on the first line.

 

I scoured the code, looked for difference between this and the original code in generic.php, but with my limited knowladge of php (and even how to debug!), I could not find any problems.. but it would not work, so I took the origional code, and most of your idea with the IF and names rather than id's, and I got it to work.

 

My final block of code ended up as:

 

 <!-- Item extra fields   -->

<?php if($item->params->get('genericItemExtraFields') && count($item->extra_fields)): ?>

    <div class="genericItemExtraFields">

<?php foreach ($item->extra_fields as $key=>$extraField): ?>  <?php if ($extraField->name=='Event Date'): ?>

<span class="genericItemExtraFieldsLabel"><?php echo $extraField->name; ?>: </span>

  <span class="genericItemExtraFieldsValue"><?php echo $extraField->value; ?></span>

  <span class="genericItemExtraFieldsSpacer">|</span>  

<?php endif; ?>        

 

<?php if ($extraField->name=='Venue'): ?>

  <span class="genericItemExtraFieldsLabel"><?php echo $extraField->name; ?>: </span>

<span class="genericItemExtraFieldsValue"><?php echo $extraField->value; ?></span>

   <?php endif; ?>   

<?php endforeach; ?>     </div>

 <?php endif; ?>

 

It is not as elegant a logic structure as yours, I googled for some help on php if,then,else logic and just found discussions of people thinking there was a logic bug with php, and talk of orphan elseif/endif statements, and just cause more questions for myself so thought I would leave that for a later learning session and trim it back to basics.

 

Anyway, with your help I was able to make it work, so that you so very much!

 

One questions if I may in an effort to better understand K2 and hopefully ask less questions :) ... Why does referencing the fields directly by their ID ( as in my original post) not work in generic, when it does is the catitem listing?  it looks like the data is being read into the array the same way with the "count($item->extra_fields" ( I assume that is what it is doing?) but then i cant access the data in the same way..  As I said, its working so this is not really important, but I do want to understand K2 better.. oh, and I am going to try something similar with sub templates for K2_content so I  am guessing the more knowledge the better.

 

Either way, one final big thank you.

 

Regards

 

Michael Tull

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

More
13 years 6 months ago #95160 by william white
I havent modified generic.php, but i think its a scope problem. When you are in a file category_item.php that already references extra fields you find no problem as the item is in scope and you can get to the variables. When you are else where you are processing lots of items and not one specific item...

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

  • Michael Tull
  • Michael Tull's Avatar Topic Author
  • Offline
  • New Member
More
13 years 6 months ago #95161 by Michael Tull
Thanks William,

 

I was guessing something like that, but have not tried to trace it 'back up the line' so to speak.  I think I need to learn more php :) .. As I have mentioned earlier, I can generally read the code and follow the logic as I have some experience back as far as Pascal and Fortran and was working in asp for a fair while but its the syntax that gets me.. been spending lots of time with w3schools and php.net tabs open.

 

Thanks again!  K2 is a great tool, but it is the community that really makes it work.

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

  • ahmad balavipour
  • ahmad balavipour's Avatar
  • Offline
  • New Member
  • هاستینگ
More
13 years 3 months ago #95162 by ahmad balavipour
and if you cant find your field with this if($extraField->name=='Venue') like me you can use something like this:

 

if(strpos($extraField->name,"Venue"))

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


Powered by Kunena Forum