Hide HTML with PHP conditional - Not working - php

I am very new to PHP and I am trying to get a piece of PHP code to run inside of my HTML file.
I have a drop down menu. If users select one item, it should display additional fields. So, I want them to only display if they select that item from the drop down menu. I am trying to select it based on the value for that drop down item. I have not declared any PHP values in a PHP script. This is all in HTML.
I know that with jquery you have to pull in the jquery library before running the script. Do I need to do this with PHP also?
Here is the code that I am trying to run:
Thank you in advance!
<?php
if ($dropmenuValue == "specificParameter") {
?>
<div>
-conditional content-
</div>
<?php
}
?>

<?php
if ($value ) {
?>
<div>
-conditional content-
</div>
<?php
}
?>

If you are setting the $values variable like this
$value = true
Then you can do
<? if($value){ ?>
<div>Content Here</div>
<? } ?>
Is the page a .php page?

A multi conditional statement is done in a different manner. The manner being:
<?php if($val): ?>
<div>
-conditional content-
</div>
<?php endif; ?>

the problem is that your comparing a boolean to a String.That's why your code won't work I would suggest something like If($value){ or if($value==true){.

you want to do if($value == true). The quotes you have around true does not make it a boolean variable.
Everything else in your code looks find.
tldr: correct: if($value == true) ----------
incorrect: if($value == "true")

<?php if ($value ):?>
<div>
-conditional content-
</div>
<?php endif;?>
Here are a few pointers.
$value is a boolean value. It is best to evaluate using if($value)
The PHP if syntax used above is a much cleaner way.
You will be able to see where your if statement begins and ends.
Enjoy!

Related

display none if php function is empty

I have a wordpress function that displays adverts every so often. When are not shown essentially I would prefer to the div to display:none;
I can not seem to figure out the correct PHP function in order for the div not to display when a advert is uploaded.
<div class="advert" <?php if(!empty($_GET['details'])) {echo "style='display: none'";} ?>></div>
Why not completely not echo "advert" element?
if ( !empty($_GET['details']) ){
echo '<div class="advert">add text</div>';
}
if you really want to just hide, you can assign hide class
<div class="advert <?php echo ( empty($_GET['details'])? 'hide' : '' );">add text</div>
then you would need to add "hide" class with display:none in your style.css
Above is shorthand/ternary if/else statement used, its great if you need output some string.
And, please don't output/trust any user input i.e. $_GET['details'] 'as is' anywhere without escaping it, for security reasons.
Wordpress have plenty easy-to-use escape functions, like esc_attr() and esc_html().
This should do it for you
<?php
$advert_display_string = "";
if (!isset($_GET['details'])) {
$advert_display_string = "style='display: none'";
}
?>
<div class="advert" <?php echo $advert_display_string ; ?> ></div>`
but having said that, instead of displaying it and then hiding it with css, you could just choose only to display it if you need it there, like below
<?php
if (isset($_GET['details'])) {
?>
<div class="advert"></div>
<?
}
?>

PHP - Do this, and if the next part meets this criteria also do this

I'm quite new to PHP, but have no idea how to word this question.
I want to return a coloured circle if the colour has been selected elsewhere.
So if silver has been ticked, show a silver circle.
If silver and gold have been ticked, show both.
There must be an easier way to code the below, instead of having to create lots of seperate if statements and call a variable each time?
elseif's wouldn't work here from my understanding.
Forgive me for my naivety!
The code I've written below:
<?php
// for iphone
$colours_available = get_field('iphone_colours_available');
// check
if( $colours_available && in_array('silver', $colours_available) ): ?>
<div class="colour-circle-title"><p>Silver</p></div>
<?php endif; ?>
<?php
// vars
$colours_available = get_field('iphone_colours_available');
// check
if( $colours_available && in_array('gold', $colours_available) ): ?>
<div class="colour-circle-title"><p>Gold</p></div>
<?php endif; ?>
Thanks so much for any help!
As mentioned in comments, you can iterate through your $colours_available array, and then use ucwords() to camel-case your p-tag's text:
<?php $colours_available = get_field('iphone_colours_available'); ?>
<?php foreach($colours_available as $color) :; ?>
<div class="colour-circle-title"><p><?php echo ucwords($color); ?></p></div>
<?php endforeach; ?>
Note: If you don't need to use the $colours_availble array elsewhere, you could eliminate that line and replace it wtih get_field('iphone_colours_available') in the loop. Also, this solution does not address sorting, which your previous solution does. For that, I would recommend looking at PHP's sorting functions, specifically uasort, uksort, and usort

open&close php tags on each line, within arrays, functions

I collaborate with a web-programmer on a php project based in kirby cms, and he wants to open and close every line as such:
<main>
<?php /*php code here/* ?>
<?php /*more php here*/ ?>
...
Trying to follow this style, I found some errors in my code. The first is that it seems I canNOT do this in the middle of an array as such:
BAD CODE
<?php $oo = array( ?>
<?php 'h' => 100, ?>
<?php 'v' => 100, ?>
<?php ); ?>
but I can do it in the middle of a foreach loop as such:
<?php foreach ($p as $subp): ?>
<div id='<?= $subp->title() ?>'>
<?php endforeach; ?>
Are there any other cases such as array in which I canNOT do this?
/edit
According to the answer, there can only be tag-breaks within 'foreach', 'while', or 'if' blocks.
How about a 'foreach', 'while' or 'if' within a function? is that 'legal'?:
<?php
function myFunction($arg){
if($arg === 'this'): ?>
<?= '<p>yep</p>' ?>
<?php else: ?>
<?= '<p>nop</p>' ?>
<?php endif;
};
?>
And how about nesting if within foreach within function?:
<?php
function myFunction($arr){
foreach($arr as $val): ?>
<p><?= $val ?> <p>
<?php if($val === 'this'): ?>
<?= '<p>yep</p>' ?>
<?php else: ?>
<?= '<p>nop</p>' ?>
<?php endif;
endforeach;
};
?>
edit/
Thank you
you cannot "break out of php" mid statement. wline defining an array for example you cannot close the php tags. The only time when you can "break out" of php is between opening and closing a loop or an if/else statement. This actualy does not break the statement as <?php foreach: ?> is a complete statement whereas <?php foreach{ ?> is not. Here some examples of what you can do:
<?php if($this!=$that): ?>
{something}
<?php endif ?>
<?php foreach($things as $thing): ?>
{something}
<?php endforeach ?>
<?php $while($this): ?>
{something}
<?php endwhile ?>
I think you get the message. you must have complete statements within php tags, without interruptions.
P.S. Also avoid using the shorthand <? instead of <?php at all cost, moving your project to a different hosting or an upgrade of your hosting might break your project as per default short tags are not activated. <?= ?> shorthand is safe as this is unaffected by the setting for newer php versions.
P.P.S Do not listen to the guy who wants php in one line, this will make your code hard to read and maintain. Stand strong and write beautiful code :)
UPDATE: (after the update on the question from #Jaume Mal)
I did not mean the examples in my answer as exclusive but as examples of statements that are complete vs statements that are incomplete. (I also forgot to mention closing php tags mid fuction, wich also work but I despise and woudl strongly advise against.) So for example <?php function foo(){ is a complete statement of starting a function but (as the other cases with loops etc..) it needs a closing statement, in this case }. This is true for if / else or foreach and so on:
<?php if($this){ ?>
some code
<?php } ?>
is a valid code, as the code pieces within the php tags are complete statements.

PHP while loop not ++'ing variable within if statements

statmentI am currently working on a project. I am having issues with making a variable ++ in the loop while nested in if statements. I really do not understand why it's doing what it's doing. Here is my code.
<?php $i=0;
while ($loop->have_posts() ) : $loop->the_post(); ?>
<?php if(get_field('featured_game') && valid_release(get_field('release_date'))):?>
<div class="item <?php if($i == 0){echo 'active';}?> row carousel-<?=$i;?>">
// do generated html
<style>
.carousel-<?=$i;?>{
// do generated css
}
</style>
</div>
<?php $i++;?>
<?php endif?>
<?php endwhile;?>
</div>
My results have
carousel-
these spots all having returned 0 despite the loop running. meaning that incrment is not happening.
if I remove the if statement the loop works, as expected. The if statement are just to verify criteria to be true. I do can place the ++ outside of the if statement I get the same results.
Could some please explain what I am missing here? This seems like such a basic problem. I really do not understand why $i will not increment when does meet the criteria and posts the correct info. Thanks in advance to anyone who takes the effort.
I ran this code:
<?php $i = 0;
while ($i < 10) : ?>
<?php if (true): ?>
<?php if ($i == 0) {
echo 'active';
} ?>
<?= $i; ?>
<?php $i++; ?>
<?php endif ?>
<?php endwhile; ?>
and the result was:
active 0 1 2 3 4 5 6 7 8 9
so either
get_field('featured_game')
or
valid_release(get_field('release_date'))
must be false.
Try
var_dump(get_field('featured_game'));
var_dump(valid_release(get_field('release_date')));
inside the loop.
Turns out, it was increment properly all along.
My issue turns out to be that bootstraps carousel cycles the the content within the the parent div not the actual div it seems. If I disable it, or look directly at the source, everything is what it is supposed to be.

Stacking PHP code?

I've done a project that now needs to be changed in order to display one div if a variable is in an array and a different div if it isn't in the array.
Normally I'd just do
<?php $quartermonths = array("February","May","August","November");
if (in_array($month,$quartermonths))
{echo "quarter code in here";}
else
{echo "nonquarter code in here";}
?>
and be on my merry way, however the code I've got already contains a load of html and php code already, which doesn't like encapsulated within another PHP block (as far as I'm aware?)
e.g.
<?php $quartermonths = array("February","May","August","November");
if (in_array($month,$quartermonths))
{echo "Quarter HTML CODE
<?php quarter phpcode ?>";}
else
{echo "Non-Quarter HTML CODE
<?php non-quarter phpcode ?>";}
?>
So my question is, what is the best way to tackle this? Is it simply to do a javascript hide div A when the variable is met and hide divB when the variable isn't met, or is there a better solution?
Thanks
<?php
if (in_array($month,$quartermonths))
{ ?>
Quarter HTML CODE
<?php quarter phpcode ?>
<?php } ?>
split your html code from php code like this.
It sounds like you just want to concatenate the value of quarter phpcode. Let's say it's a single function, quarter_phpcode(). You can just do this:
{ echo "Quarter HTML CODE" . quarter_phpcode(); }

Categories