how to write conditional statements inside div html? - php

I have a requirement of showing 2 div's based on a condition like if value is '1' show one div else if value is '0' display another div. Below is my code:
<div class="csv" <?php if($campaign[0]->method != 'CSV'){ echo "style='display:none'"; } ?> >
-----------
------------
</div>
<div class="api" <?php if($campaign[0]->method != 'API'){ echo "style='display:none'"; } ?> >
----------
----------
</div>
Code is looking fine but not working properly. I am trying to achieve this in Laravel 5.4 framework. Any help would be appreciated. Thanks

you can do inline if statements like this:
<div class="csv" <?php echo ($campaign[0]->method != 'CSV' ? 'style="display: none;" : ''); ?>></div>
() - starts the if statement and closes
? - is the if condition is met
: - is the else

It is easy to drop into and out of the PHP processor which will allow you to put the logic into the html file. The following also uses PHP Alternative syntax for control structures
I removed the style="display: none"; bits as I assume that was included to hide the alternate <div>.
<?php //drop into PHP processor mode
if($campaign[0]->method != 'CSV'): //now, back to html processing mode ?>
<div class="csv">
<!-- html markup-->
</div>
<?php elseif($campaign[0]->method != 'API'): ?>
<div class="api">
<!-- html markup-->
</div>
<?php endif; ?>

This is not an exact response to your answer, but you need to have in mind that doing display: none you can always show it playing with the css a bit. In order to prevent that, you should use conditional in php to only render what you desire to show, to prevent the code to be visible. I'd do this way:
<?php if($campaign[0] == 'CSV'):?>
<div class="csv">
-----------
------------
</div>
<?php elseif($campaign[0] == 'API'):?>
<div class="api">
----------
----------
</div>
<?endif;
Or using a switch if there are more options than CSV and API

Use short form of if statement, and write it inside of curly braces as laravel blades' echo.
<div class="api" {{ $campaign[0]->method != 'API' ? "style='display:none'" : "" }}>
It may help you

Related

Detect Category ID=4 in Joomla and show div

I would like to detect a certain category with ID=4 and then apply if/else to add div to display contents.
<?php
if (JRequest::getVar('view')=='categories' && JRequest::getVar('id')==4) { ?>
<?php } ?>
How do I achieve this?
I want to show this if the article belongs to category 4 else show another div.
Imagining this two be the different div
<div class="category4">text here</div>
<div class="categoryxxx">text here</div>
Do note that <?php echo $this->item->catid;?> shows the correct Category ID. I would like to apply if and else statement using catid==9 or something. Just not good at PHP.
You can also put directly the html code, avoiding echo. It may useful especially when html code is sizeable.
<?php if ($this->item->catid == 4): ?>
<div class="category4">text here</div>
<!--- any other html code --->
<?php else: ?>
<div class="categoryxxx">text here</div>
<!--- any other html code --->
<?php endif; ?>
Hey I got it working :)
<?php
if ($this->item->catid == 9) {
echo "yes, it exists";
}
else {
echo "no, it don't exist";
}
?>

How to add css properties inside the PHP tag

I need one help.i need to restrict some css display property with PHP condition.I am explaining my code below.
<div style="width:24%; float:left; padding:10px;display:none;" id="compid">Select Company :
</div>
Here initially my div's css property display:none is there.But i need to block this using some php condition like below.
<?php if($getcustomerobj->companypro == 1 and $_REQUEST['edit']!=""){display:block}else{display:none} ?>
I need to put the above condition inside the style properties.Please help me how to do it proper way.
you can instanciate your display value before outputing :
<?php
$display = ($getcustomerobj->companypro == 1 && $_REQUEST['edit']!="") ? 'block' : 'none';
?>
<div style="width:24%; float:left; padding:10px;display:<?= $display ?>;" id="compid">Select Company :
</div>
If you want to mix php and html, you can use php tags inside html :
<p><?php echo $something ?></p>
or short tags if enabled on your server
<p><?= $something ?></p>
Do something like
<div style="width:24%; float:left; padding:10px;<?php if($getcustomerobj->companypro == 1 && $_REQUEST['edit']!=""){echo 'display:block;';}else{echo 'display:none;';}?>" id="compid">Select Company :</div>
add your php and echo in the style attribute
Hope it helps !
You just need to set a variable to the string you want to use as the CSS property.
Just for brevity, I tend to use ternaries for this sort of thing:
<?php $display = ($getcustomerobj->companypro == 1 and $_REQUEST['edit']!="") ? 'display:block;' : 'display:none;'; ?>
<div style="width:24%; float:left; padding:10px;<?php echo $display;?>" id="compid">Select Company :
</div>
Its not the tidiest solution, you would be better of using HTML classes and separating your CSS, but this should get the job done.

Can I put two divs in if statement and close it with one div?

I have following code.
<?php if(ot_get_option('wide') == 'on') : ?>
<div class="nothing">
<?php else : ?>
<div class="container">
<?php endif; ?>
Some other codes
</div>
Can I put two divs in if statement and close it with one div? Or what is the best practice?
Yes you can in this case, as there will always be one div tag and one closing div tag.
The only issue you might encounter is if you're using an IDE, it might validate your HTML and give you an error due to that, as it'll see two divs and only one closing tag.
If you want a more "proper" way to do it or you want to avoid what I've mentioned, you can do this:
<?php $myClass = null; ?>
<?php if(ot_get_option('wide') != 'on') $myClass = 'container'; ?>
<div class="<?php echo $myClass; ?>">
</div>

How to get a div class to be displayed on certain pages using a template and php

I have a template for all my web pages but i want a div class not to be seen on certain pages. I use the URL to get the content out of a database with php so an example of my URL
= index.php?categorie=navigatie_bar&onderwerp=startpagina
I need a code to say get catagory from the url and if catagory is not navigation_bar
echo '<div class"fb-comments"></div>';
can someone help ?
<div id="container">
<div id="pagina_text">
{{ CONTENT }}
</br>
<div class="rw-ui-container"></div>
</br></br>
<?php
if(strcmp($_GET['categorie'], "navigatie_bar") != 0)
{
echo '<div class="fb-comments" data-href="http://alledaagsetips.nl" data-numposts="10" data-colorscheme="light"></div>';
}
?>
</div> <!-- end pagina_text -->
</div><!-- end container -->
You can access the URL variables via $_GET['name_of_parameter'].
if(strcmp($_GET['category'], "navigation_bar") != 0)
{
echo '<div class="fb-comments"></div>';
}
In this case, only if index.php?category=navigation_bar the div will be echoed.
Take attention to your spelling, in your example you wrote catagory instead of category
EDIT:
You can do $_GET['category'] == 'navigation_bar', but for string comparisons it is sometimes safer to use the strcmp (string compare) function.
See String comparison using == vs. strcmp
try this
<?php
if($_REQUEST['category'] != 'navigation_bar'){
echo '<div class="fb-comments"></div>';
}else{ //do this }
?>
I think this is just basic PHP, a simple condition. But you seem to have also errors in our template. I would suggest running a w3c validator check, so you can see what you have wrong (wrongly nested tags, syntax mistakes etc..)
W3C Validator Online
UPDATE:
<div id="container">
<div id="pagina_text">
{{ CONTENT }}
<br />
<div class="rw-ui-container"></div>
<br /><br/>
<?php
if(strcmp($_GET['categorie'], "navigatie_bar") != 0)
{
echo '<div class="fb-comments" data-href="http://alledaagsetips.nl" data-numposts="10" data-colorscheme="light"></div>';
}
?>
</div> <!-- end pagina_text -->
</div><!-- end container -->

PHP: including 2 same php files in 1 page

i have a code
<div class="b4_content">
<div class="col_left3 FloatLeft">
<h3>Clients / Projects</h3>
<div id="left_menu" class="left_menu">
<?php
require("admin/Connection/connection.php");
require("admin/functions/functions.php");
$toplvl=0;
/* 1st include of linkCategory.php */
include "admin/functions/linkCategory.php";
?>
</div>
</div>
<div class="col_right3 FloatRight ">
<div class="cright_padd">
<?php
$uploader="admin/";
$catID = $_GET['catID'];
if($catID==""){
$toplvl=2;
/* 2nd include of linkCategory.php */
include "admin/functions/linkCategory.php";
} else {
$toplvl=0;
include "admin/functions/viewImages.php";
}
?>
</div>
</div>
<div class="ClearBoth"></div>
</div>
As you can see i have 2 includes (admin/functions/linkCategory.php) with same pages
My Problem is, the second include "admin/functions/linkCategory.php" does not show up.
Can you help me out
I guess $toplvl is a flag that's used within "admin/functions/linkCategory.php" to do different things. So, maybe the error isn't in this chunk, but in linkCategory.php itself.
My guess is that $catID is NOT equal to the empty string.
And btw, bolding doesn't work inside code blocks (if that's what you were trying to do).
As an alternative, wrap whatever linkCategory.php outputs in a function. Include linkCategory.php once and replace the includes with a call to the function.
if the problem still persists, you could set error reporting to show all and see if there's any output

Categories