Jump to content

Hide field if value is empty/null/equal to something


Ortix

Recommended Posts

Hello everyone! I'm at the brink of going mad right now!  :shrug::facewall:

 

So let me start:

 

I'm making a website with files that have multiple mirrors. Some have 5, some have 3. To make this happen i use joomla and K2 CCK as my tools of trade. I made some extra fields in K2, which are empty download link fields. These fields belong to a group. When i assign the group to a category these fields automatically appear with every single item in that specific category. Now please focus on the coming php and not on the groups and what not. I tried every possible option without success. This is the easiest way around (Which i'm gonna describe now)

 

Basically, I have 8 or so extra fields. They are all links. Some items use 5 of them and some use 3. That leaves me with a lot of empty fields that DO show up in the item. Example

There is one download link that has a value and the other one that does NOT.

 

I want to be able to hide the download links that have no values or equal a value like empty or something. Also, in the admin back-end the "value" i'm talking of consists of 3 parts. The text, the address, and the function (open in new window etc).

 

So how do i hide the field which renders nothing for the value so that i only see fields that have actual links in them.

 

This is the code that renders the fields (original):

 

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

      <!-- Item extra fields --> 

      <div class="itemExtraFields">

          <h3><?php echo JText::_('Download Links'); ?></h3>

          <ul>

            <?php foreach ($this->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="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>

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

                <br class="clr" />           

            </li>

            <?php endforeach; ?>

            </ul>

        <div class="clr"></div>

      </div>

      <?php endif; ?>

 

I tried some mods without success. Either an error comes up, ALL the fields disappear or they all stay.

 

Currently i have something like this:

	  <?php if($this->item->params->get('itemExtraFields') && count($this->item->extra_fields)): ?>
  <!-- Item extra fields  --> 
  <div class="itemExtraFields">
  	<h3><?php echo JText::_('Download Links'); ?></h3>
  	<ul>
        <?php #var_dump($this->item->extra_fields); ?>
		<?php
foreach ($this->item->extra_fields as $key => $extraField)
{
    $value = trim($extraField->value);
    if (is_null($value))
    {
        continue;
    }
   ?>
     <li class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
                
                <span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
                <span class="itemExtraFieldsLabel"><?php echo $extraField->name; ?>:</span>
                <br class="clr" />            
            </li>
<?php
} 
; ?> 

I am a total php noob. I got some help without success.

 

 

 

Link to comment
Share on other sites

I struggle a bit with classes and OOP but if I understand correctly, you should only build the array with the ones that have a value.

 

Otherwise something like this?

 

<?php 
if($extraField->value !=''){
?>
<li class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
               
                <span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
                <span class="itemExtraFieldsLabel"><?php echo $extraField->name; ?>:</span>
                <br class="clr" />           
            </li>
<?php
}
?>

 

Probably wrong but I am still doing old school php

Link to comment
Share on other sites

what do you mean with relevant code? Sorry, i'm very noob at php. I haven't had the time to study it yet. I'm trying to learn by myself.  And what do you mean with queries as well. Please don't hate. If you tell me what to do, i will do it!

 

As for the relevant code, that is A LOT. There are files scattered all over and posting that would be a pain both for you and me. Unless you want me to post the file this code snippet belongs to.

 

As for queries, do you mean var_dump? Sorry for my noobish ness.

Link to comment
Share on other sites

what do you mean with relevant code? Sorry, i'm very noob at php. I haven't had the time to study it yet. I'm trying to learn by myself.  And what do you mean with queries as well. Please don't hate. If you tell me what to do, i will do it!

 

As for the relevant code, that is A LOT. There are files scattered all over and posting that would be a pain both for you and me. Unless you want me to post the file this code snippet belongs to.

 

As for queries, do you mean var_dump? Sorry for my noobish ness.

 

You don't need to apologise if you don't understand some of the terminology. Just ask and we will clarify or if you <3 Google like I do, you can prefix whatever you don't understand with 'define:' like:

 

http://www.google.com/search?q=define%3Aterminology

Link to comment
Share on other sites

these are just errors like unexptected endif or endforeach.

 

What i DID find out however is if i do this (or anything else that states value is empty):

 

<?php foreach ($this->item->extra_fields as $key=>$extraField)
if(empty($extraField->value));
else{
?>
<li class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
                <span class="itemExtraFieldsLabel"><?php echo $extraField->name; ?>:</span>
                <span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
                <br class="clr" />           
            </li>
<?php
}
?>

 

there is an extra field that indeed does NOT show up if it's empty. And that field ONLY has 1 value to enter. It's a simple text box. The extra field i am using (links) has 3 parameters which are: name, link, function (open in new window etc). This code does not affect those!

 

Link to comment
Share on other sites

Sorry for double post but for some reason i can't edit my old post.

 

i also found this:

 

elseif ($extraFieldType=='link'){
				if (substr($values[$i], 0, 7) == 'http://'){$values[$i] = $values[$i];}
				else {$values[$i] = 'http://'.$values[$i];}
				$object->set('value', $values[$i]);
			}

 

This is a system file for k2. The code snippet in the previous posts is not in the same file as this one

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.