[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

If field is empty then do this

Posted on 2011-04-22
16
Medium Priority
?
249 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:Eternal_Student
  • 8
  • 6
  • 2
16 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35447818
You can use the empty() function.
http://php.net/manual/en/function.empty.php
0
 
LVL 19

Expert Comment

by:Greg Alexander
ID: 35447865
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
 
LVL 18

Author Comment

by:Eternal_Student
ID: 35447921
@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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35448350
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
 
LVL 18

Author Comment

by:Eternal_Student
ID: 35449615
I set the counter to zero like so:

<?php $counter=0;?>

I thought you would of gathered that, sorry.
0
 
LVL 19

Expert Comment

by:Greg Alexander
ID: 35449637
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
 
LVL 18

Author Comment

by:Eternal_Student
ID: 35449655
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35450595
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
 
LVL 18

Author Comment

by:Eternal_Student
ID: 35451033
$this->item->extra_fields can contain anything that is entered in the Joomla! interface.

Not entirely sure how to post the entire class?

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35451042
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
 
LVL 18

Author Comment

by:Eternal_Student
ID: 35451055
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
 
LVL 18

Accepted Solution

by:
Eternal_Student earned 0 total points
ID: 35451075
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35451140
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35451210
Thanks for posting that "solution."  I'll let someone else work with you on your next question.
0
 
LVL 18

Author Comment

by:Eternal_Student
ID: 35451247
No problem :-)
0
 
LVL 18

Author Closing Comment

by:Eternal_Student
ID: 35473607
I got there myself in the end.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses
Course of the Month19 days, 1 hour left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question