How can i shorten this PHP code? (if - elseif) - php

I have this simple if else function and i wonder how i could shorten this?
<?php
if( $count_bananas == '1' )
{ echo '<div class="stage-columns-1">'; }
elseif ($count_bananas == '2')
{ echo '<div class="stage-columns-2">'; }
elseif ($count_bananas == '3')
{ echo '<div class="stage-columns-3">'; }
elseif ($count_bananas == '4')
{ echo '<div class="stage-columns-4">'; }
?>
The code works but i wonder if there is a way to code this shorter and more elegant?

You really didn't see the pattern?
echo '<div class="stage-columns-' . $count_bananas . '">';

It can be done without condition checks. For the code snippet you are posted, if the values are same then -
echo '<div class="stage-columns-' . $count_bananas . '">';

Related

How to extract heading tags in php

in wordpress when I use <?php the_title( '<h3>', '</h3>' ); ?> it works
but if I have a different php output like this one <?php echo $variable['custom_title_option']; ?> how can I do the same as on the_title
also if I use a function like the below example:
function change_hading_titles() {
global $variable;
$heading_tag = $variable['custom_title']; //option name
if ($heading_tag == "h1") {
print '<h1>', '</h1>';
} elseif ($heading_tag == "h2") {
print '<h2>', '</h2>';
} elseif ($heading_tag == "h3") {
print '<h3>', '</h3>';
} elseif ($heading_tag == "h4") {
print '<h4>', '</h4>';
} elseif ($heading_tag == "h5") {
print '<h5>', '</h5>';
} elseif ($heading_tag == "h6") {
print '<h6>', '</h6>';
}
}
add_action('change_hading_titles', 'change_hading_titles');
is it possible to use do_action( 'change_hading_titles' ); to change all my custom titles?
So, I mean to retrieve the function for closing <?php echo $variable['custom_title_option']; ?> with heading tags
Do you mean?
function getTag($tagName, $titleValue){
return "<".$tagName.">".$titleValue."</".$tagName.">";
}
$tag = getTag("h1", "hello");
// $tag = <h1>hello</h1>
..If that is not what your after, please explain a little more clearly, am happy to help further. Either way, this would be a significantly more efficient way of doing that function.

Show '0 comments' text on posts.php page using Anchor CMS

I'm using this PHP to show the comments on my homepage:
Functions.php
function tootsweet_article_total_comments() {
return Comment::where('post', '=', article_id())
->where('status', '=', 'approved')
->count();
}
Posts.php
<?php if (tootsweet_article_total_comments() > 0)
{
echo '<a href="'.article_url().'#comments">';
if (tootsweet_article_total_comments() == 1)
echo ' comment';
else
echo tootsweet_article_total_comments().' comments';
echo '</a>';
}
?>
All is working perfectly, but when a post has 0 comments, no text is shown at all whereas I want it to say '0 comments'. I'm a bit of an amateur with PHP, so is there something I need to amend here?
You start by checking if the number of comments is greater then 0, but you don't do anything if the number of comments is equal to 0.
You'd need an else to your first check for comments, i.e.
<?php
if (tootsweet_article_total_comments() > 0) {
echo '<a href="'.article_url().'#comments">';
if (tootsweet_article_total_comments() == 1) {
echo ' comment</a>';
} else {
echo tootsweet_article_total_comments().' comments';
echo '</a>';
}
} else {
echo "<a href='" . article_url() . "'>0 comments</a>";
}
?>
n.b added brackets for readability and to make the logic clearer, feel free to remove if you prefer.

How do I code an IF statment within a string?

I've tried every combination I could think of, but nothing is working. How do I integrate an IF statement within a string?
This is the line I need integrated;
if ($row['SL'] == 'Yes') {echo '<li class=\"sl\">Short Sale</li>' ;}
This is what I tried;
elseif ($row['X'] == 'sold') { echo "
<li id=\"price\"> $ {$row['SP$']}</li>
" .
{ if ($row['SL'] == 'Yes') {echo '<li class=\"sl\">Short Sale</li>' ;} }
. "
<li>Days on Market: {$row['DOM']}</li>;
}
Use a ternary operator:
elseif ($row['X'] == 'sold') { echo "
<li id=\"price\"> $ {$row['SP$']}</li>
" . ($row['SL'] == 'Yes') ? '<li class="sl">Short Sale</li>' : ''
. "
<li>Days on Market: {$row['DOM']}</li>";
}
You just need to break it up:
elseif ($row['X'] == 'sold') {
echo "<li id=\"price\"> $ {$row['SP$']}</li>";
if ($row['SL'] == 'Yes') {
echo '<li class=\"sl\">Short Sale</li>';
}
echo "<li>Days on Market: {$row['DOM']}</li>;
}
You don't. You output the first half the string, then fully stop that statement, then evaluate your if, then output the rest of the string.
...
elseif ($row['X'] == 'sold') {
echo "<li id=\"price\"> $ {$row['SP$']}</li>";
if ($row['SL'] == 'Yes') {
echo '<li class=\"sl\">Short Sale</li>'
}
echo "<li>Days on Market: {$row['DOM']}</li>";
}
You can't "embed" an if statement in a string, firstly because that's not syntactically valid, but even if it was, if statements don't resolve to a value in PHP.
What you are looking for is called Ternary Operator or Shorthand If/Else:
( condition ? true result : false result )
Example:
echo "<li id=\"price\">{$row['SP']}</li>" . ( $row['SL'] == 'Yes' ? '<li class="sl">Short Sale</li>' : '' ) . "<li>Days on Market: {$row['DOM']}</li>";
The above example is the same as:
echo "<li id=\"price\">{$row['SP']}</li>";
if ( $row['SL'] == 'Yes' ) echo '<li class="sl">Short Sale</li>';
else echo '';
echo "<li>Days on Market: {$row['DOM']}</li>";
You can "stack" ternary expressions so the complete example will look like:
echo
$row['X'] == 'sold'
? "<li id=\"price\">{$row['SP']}</li>" .
( $row['SL'] == 'Yes' ? '<li class="sl">Short Sale</li>' : '' ) .
"<li>Days on Market: {$row['DOM']}</li>"
: '';
important: you cannot build a ternary operator without else statement.

If else won't accept class

Im trying to make my language switcher change font weight depending on the language url but i cannot get it to accept the class
if ($_REQUEST["lang"] == "en")
{
echo '<div class="langlight">Svenska</div>';
}
else
{
echo '<div class="langbold">Svenska</div>';
};
if ($_REQUEST["lang"] == "en")
{
echo '<div class="langbold"><a href="http://xyz.com/">English</div>';
}
else
{
echo '<div class="langlight"><a href="xyz.com/">English</div>';
};
Remove semicolons, missing closing tags:
if ($_REQUEST["lang"] == "en")
{
echo '<div class="langlight">Svenska</div>';
}
else
{
echo '<div class="langbold">Svenska</div>';
}
if ($_REQUEST["lang"] == "en")
{
echo '<div class="langbold">English</div>';
}
else
{
echo '<div class="langlight">English</div>';
}

PHP conditional loop help

Hi there in my database I have 3 columns, is_contract, is_permenant and is_temporary. Within these columns there is either a Y or N value.
I am using these columns to echo onto the page what kind of work someone is looking for, my problem is that the user can be looking for more than one type of work, I am currently running 3 if statements to determine what to echo to the page, however I am struggling to add a comma if more than one of the statemnts returns as true, below is my code so far,
<?php
if($rslt['is_contract'] == 'Y') {
echo "Contract ";
}
if($rslt['is_permanent'] == 'Y') {
echo "Permanent ";
}
if($rslt['is_temporary'] == 'Y') {
echo "Temporary";
}
?>
<?php
$out=array();
if($rslt['is_contract'] == 'Y') $out[]="Contract";
if($rslt['is_permanent'] == 'Y') $out[]="Permanent";
if($rslt['is_temporary'] == 'Y') $out[]="Temporary";
echo implode(", ",$out);
?>
Either you can use like this in simple way
if($rslt['is_contract'] == 'Y') {
echo "Contract ";
}
if($rslt['is_permanent'] == 'Y') {
if($rslt['is_contract'] == 'Y') {
echo ", ";
}
echo "Permanent ";
if($rslt['is_temporary'] == 'Y') {
echo ", ";
}
}
if($rslt['is_temporary'] == 'Y') {
if($rslt['is_contract'] == 'Y' && $rslt['is_permanent'] != 'Y') {
echo ", ";
}
echo "Temporary";
}

Categories