Eternal_Student
asked on
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
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 </a></p></div><div id=\"showMoreLayer\" class=\"mooblock-el mb1_1e\"><div id=\"imageCol\">");
} elseif( $counter == 7 ) {
echo("</div><div id=\"textCol\">");
}
?>
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 </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;
}
ASKER
@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?
@galexander07 - not sure how your code is different from what I am already doing?
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.
ASKER
I set the counter to zero like so:
<?php $counter=0;?>
I thought you would of gathered that, sorry.
<?php $counter=0;?>
I thought you would of gathered that, sorry.
Maybe something like this
foreach ($this->item->extra_fields as $key=>$extraField):
if($counter == 7 ){
if(empty($extraField)){
echo "whatever you want here";
}
}
ASKER
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?
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?
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.
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.
ASKER
$this->item->extra_fields can contain anything that is entered in the Joomla! interface.
Not entirely sure how to post the entire class?
Not entirely sure how to post the entire class?
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
ASKER
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:
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 </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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
Thanks for posting that "solution." I'll let someone else work with you on your next question.
ASKER
No problem :-)
ASKER
I got there myself in the end.
http://php.net/manual/en/function.empty.php