If field is empty then do this

Hi,

I have a series of fields in Joomla that are outputted and the layout is created around them, please see php below:

The 'show more' link that is outputted should ONLY show if one of the fields is NOT empty. At the minute it shows regardless. How would I build in an IF statement to this PHP to check whether the field number [7] is empty or not and show that link depending on the outcome?

Many thanks
<?php 				
				foreach ($this->item->extra_fields as $key=>$extraField):
						if( $counter == 0 ) { 
								echo("<div id=\"largeImageWrap\">");
						} elseif( $counter == 1 ) {
								echo("</div><div id=\"sidePanelWrap\">");
						} elseif( $counter == 3 ) {
								echo("<p><a class=\"mooblock-title mb1_1t\" href=\"#\">Show more&nbsp;&nbsp;&nbsp;</a></p></div><div id=\"showMoreLayer\" class=\"mooblock-el mb1_1e\"><div id=\"imageCol\">");	
						} elseif( $counter == 7 ) {
								echo("</div><div id=\"textCol\">");										
						}											
				?>

Open in new window

LVL 18
Eternal_StudentAsked:
Who is Participating?
 
Eternal_StudentAuthor Commented:
OK, ive ended up doing it like this, and it works!

So I am using 2 foreachloops, the 1st one just goes through and sets the variable:


<?php $counter=0; foreach ($this->item->extra_fields as $key=>$extraField): if($counter == 7 && $extraField->value ==""){$hideMe = 'hideMe';}$counter++; endforeach;?>
		
				<div class="itemExtraFields">
				
				<?php 				
				$counter=0;
				foreach ($this->item->extra_fields as $key=>$extraField):
				
						if( $counter == 0 ) { 
								echo("<div id=\"largeImageWrap\">");
						} elseif( $counter == 1 ) {
								echo("</div><div id=\"sidePanelWrap\">");
						} elseif( $counter == 3 ) {
								echo("<p><a class=\"mooblock-title mb1_1t $hideMe\" href=\"#\">Show more&nbsp;&nbsp;&nbsp;</a></p></div><div id=\"showMoreLayer\" class=\"mooblock-el mb1_1e\"><div id=\"imageCol\">");	
						} elseif( $counter == 7 ) {
								echo("</div><div id=\"textCol\">");										
						}											
				?> 		
						<div 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>
								<div class="clear"></div>
						 </div>
				 
						 
					<?php $counter++; endforeach; ?>

Open in new window

0
 
Ray PaseurCommented:
You can use the empty() function.
http://php.net/manual/en/function.empty.php
0
 
Greg AlexanderLead DeveloperCommented:
Switch Maybe:
switch($counter){
	case 0:
		 echo("<div id=\"largeImageWrap\">");	
	break;
	
	case 1:
		echo("</div><div id=\"sidePanelWrap\">");
	break;
	
	case 3:
		 echo("<p><a class=\"mooblock-title mb1_1t\" href=\"#\">Show more&nbsp;&nbsp;&nbsp;</a></p></div><div id=\"showMoreLayer\" class=\"mooblock-el mb1_1e\"><div id=\"imageCol\">");
	break;
	
	case 7:
		echo("</div><div id=\"textCol\">");
	break;
	
	default:
		//this will run if no condition met
	break;
}

Open in new window

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Eternal_StudentAuthor Commented:
@Ray_Paseur - Could you show me a solution modifying my code above please.

@galexander07 - not sure how your code is different from what I am already doing?
0
 
Ray PaseurCommented:
I do not know how to help you.  It appears that $counter is undefined in the code above, so the script can never work correctly if it depends on a value in $counter.
0
 
Eternal_StudentAuthor Commented:
I set the counter to zero like so:

<?php $counter=0;?>

I thought you would of gathered that, sorry.
0
 
Greg AlexanderLead DeveloperCommented:
Maybe something like this


foreach ($this->item->extra_fields as $key=>$extraField):
		
		if($counter == 7 ){
			if(empty($extraField)){
				echo "whatever you want here";	
			}
		}

Open in new window

0
 
Eternal_StudentAuthor Commented:
The trouble is, the show more link is outputted before the counter gets to the field that could be empty.

The link gets outputted when the $counter == 3, the empty field could be when the $counter == 7.

So it needs to check if the field is empty before going through that loop, I guess?
0
 
Ray PaseurCommented:
I thought you would of gathered that, sorry.

No, programming is a very literal process.  Computers do not assume, and even though the PHP language allows for a lot of assumptions the best programmers do not assume, either.

Now that we know what $counter contains, please tell us what $this->item->extra_fields contains.  Or better yet, please post the entire class definition and the test data you are using.  We can show you how to do data visualization, etc.
0
 
Eternal_StudentAuthor Commented:
$this->item->extra_fields can contain anything that is entered in the Joomla! interface.

Not entirely sure how to post the entire class?

0
 
Ray PaseurCommented:
Maybe you could extract a smaller test case and post that.  I think we can illustrate some of the important principles if we have some code and data to work with
0
 
Eternal_StudentAuthor Commented:
Can you see what I am struggling with though?

I have the foreach loop which cycles through the fields and outputs them. What I need to do is test the very last field 1st and if it is empty apply a CSS class (.hideMe) to a link that appears if counter== 3.

Here is the foreach loop in its' entirety:


<?php $counter=0;?>
				<div class="itemExtraFields">
				
				<?php 				
				foreach ($this->item->extra_fields as $key=>$extraField):
					
					if($counter == 7 && $extraField->value ==""){
							$hideMe = 'hideMe';
					}							
				
						if( $counter == 0 ) { 
								echo("<div id=\"largeImageWrap\">");
						} elseif( $counter == 1 ) {
								echo("</div><div id=\"sidePanelWrap\">");
						} elseif( $counter == 3 ) {
								echo("<p><a class=\"mooblock-title mb1_1t $hideMe\" href=\"#\">Show more&nbsp;&nbsp;&nbsp;</a></p></div><div id=\"showMoreLayer\" class=\"mooblock-el mb1_1e\"><div id=\"imageCol\">");	
						} elseif( $counter == 7 ) {
								echo("</div><div id=\"textCol\">");										
						}											
				?> 		
						<div 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>
								<div class="clear"></div>
						 </div>
				 
						 
					<?php $counter++; endforeach; ?>
				</div>

Open in new window

0
 
Ray PaseurCommented:
Have you considered taking a class in PHP programming?  I am asking because the principles of separation between the code and presentation layers might make your code easier to read and understand.  For example, setting up entire strings instead of using a combination of HTML and an echoed fraction of a string - those kinds of things make it easier to keep the logic working correctly.  Technically it is possible to intermix PHP code and HTML strings but as a practical matter it makes for incomprehensible software that is difficult to debug and even harder to modify.  The HEREDOC notation might be helpful here.  

But I am not sure I can envision a design pattern that would test the last field and change a separate field if the counter was 3.  It's just not something that you see very often.

If $this->item->extra_fields is an array, you can get the end of the array with end().  Then perhaps you could set up a shadow array with the CSS classes and use the appropriate class with each HTML statement.
0
 
Ray PaseurCommented:
Thanks for posting that "solution."  I'll let someone else work with you on your next question.
0
 
Eternal_StudentAuthor Commented:
No problem :-)
0
 
Eternal_StudentAuthor Commented:
I got there myself in the end.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.