If statement to change Div id - php

I'm writing some php/html code. I have a css file that has several backgrounds. Is there any way to write an if statement to select a div id depending on a php variable?

You can generate CSS via PHP, first sort everything out and then print with text/css header.
<?php
header("Content-Type: text/css");
?>
*{margin:0;padding:0;}
Anyway you need to clarify your question.

could do it in-line in your html/php
<div id="<?= ($true_or_false) ? 'div_id_1' : 'div_id_2' ?>">div content</div>
Or... if you have more than 2 div id options just do an classic if statement
<?php
if($condition) {
$div_id = 'something';
}
else if ($another_condition) {
$div_id = 'something_else';
}
..etc
?>
<div id="<?php echo $div_id ?>">div content</div>

Is this what you mean?
<?php
$divId = $someBoolean ? "peaches" : "pineapples";
?>
<div id="<?php echo $divId ?>">Hello</div>

// file: style.php
<?php
header("Content-Type: text/css");
?>
#mydiv {
background: url('<?php
switch($_GET['background'])
{
case 1:
echo 'blah1.jpg';
//etc...
}
?>');
}

There are a few different ways you can do this. You can do it in php and assign a different class or id. Or you do it with javascript and change the class or id after the page loads.

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";
}
?>

php tag within css does not work

I have a code in which i want to show all the categories from database table. But it only gives the first row data and it is also not displaying the image which i have given in my css.
Code
<div id="col1">
<?php
$query="Select * from category";
$res=mysqli_query($con,$query);
while($row=mysqli_fetch_array($res)){
?>
<a class="maskA" href= <?=$row['pages'];?> >
<span class="span2"><?=$row['cat_name'];?> Collection</span></a>
</div>
css
#col1{
background:url(<?= "images/ ".$row["poster"]; ?>);
background-size:100% 100%;
}
<?php
}
?>
To put PHP in a CSS file, make it a PHP file.
The only thing you need to do extra is tell the receiving end it's a css file by using header as the very first thing in the file.
<?php header('content-type: text/css'); ?>

How to display:none of data row in a for each array?

im displaying list of results through for each array where i need to hide a button for only specific items in list ,
i tried it using an if statement , but its hiding button of whole list items , please advice me on this?
here the place i have put code. this will hide whole buttons even though the id is not equal to 83
<div class="jd-items-button-details">
<?php if(($item->categories_id)==83) { ?>
<style type="text/css">
.jd-button-details {display:none !important}
</style>
<?php
} else {
echo "test2";
}
echo $item->categories_id;
?>
<?= JHTML::_('link', $link , JText::_('COM_JOMDIRECTORY_DETAILS'), 'class="jd-button-details"') ?>
You can use something like:
<div class="jd-items-button-details" <?= $item->categories_id == 83 ? 'style="display: none"' : ''?>>
Even if written once, you css style will be applied to every links with the .jd-button-details class. Instead you should conditionnaly apply this class to your button:
<style type="text/css">
.jd-button-details
{display: none!important;}
</style>
<div class="jd-items-button-details">
<?php
if(($item->categories_id)==83){
$class = 'jd-button-details';
}
else {
$class = '';
}
echo $item->categories_id;
?>
<?= JHTML::_('link', $link , JText::_('COM_JOMDIRECTORY_DETAILS'), 'class="<?php echo $class; ?>"') ?>
Or even shorter:
<?= JHTML::_('link', $link , JText::_('COM_JOMDIRECTORY_DETAILS'), 'class="'.(($item->categories_id)==83 ? "jd-button-details" : "").'"') ?>
the easiest to do is : dont use tag <styles> since it will applied to whole html area you're displayed. use inline styling :
<div class="jd-items-button-details">
<?php if(($item->categories_id) !== 83){ ?>
<button>this is button</button>
<?php
}

My PHP condition statement display two results for if and else, what's wrong my my code?

I created a conditional statement for my custom theme in Concrete5. My codes goal is to toggle layout. If the current page has a child pages under it, it will display an additional sidebar (<div class="grid_3">) to list the subpages items. If there's no child page it would display a full layout (<div class="grid_13">).
Unfortunately I get a different result. there's something I probably had missed on my condition statement. Instead of just display one layout, It is rendering the two layout.
Below is what my code look like:
<? if($c->getNumChildren()) { ?>
<div class="grid_3">
<?php
$bt_sidenav = BlockType::getByHandle('autonav');
$bt_sidenav->controller->orderBy = 'display_asc';
$bt_sidenav->controller->displayPages = 'below';
$bt_sidenav->controller->displaySubPages = 'all';
$bt_sidenav->render('view');
?>
</div>
<div id="main-content-container" class="grid_10">
<div id="main-content-inner">
<?php
$a = new Area('Main');
$a->display($c);
?>
</div>
</div>
<? } else { ?>
<div id="main-content-container" class="grid_13">
<div id="main-content-inner">
<?php
$a = new Area('Main');
$a->display($c);
?>
</div>
</div>
<? } ?>
While your content generation portions of PHP use proper PHP tags (<?php … ?>), your if/else statements use short tags (<? … ?>) which are often disabled.
Use <?php instead.
try to do like this may this will solve the issue
<?php if($c->getNumChildren()!='') { ?>
...
<?php } else { ?>
...
<?php } ?>

Show class only specific page

I'm using Drupal 7 and my theme is Omega. I have got a class "mask" and its css code:
.mask {
background:url("../img/header_mask.png") repeat-x scroll 0 0 transparent;
height:200px;
position:absolute;
top:0;
width:100%!important;
z-index:101
}
I'm create a class on Omega theme options for show content top but my div show every page. So, I want show this class only node pages.
This is my node.tpl.php:
<div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>
<?php print $user_picture; ?>
<?php print render($title_prefix); ?>
<?php if (!$page): ?>
<h2<?php print $title_attributes; ?>><?php print $title; ?></h2>
<?php endif; ?>
<?php print render($title_suffix); ?>
<?php if ($display_submitted): ?>
<div class="submitted">
<?php print $submitted; ?>
</div>
<?php endif; ?>
<div class="content"<?php print $content_attributes; ?>>
<?php
// We hide the comments and links now so that we can render them later.
hide($content['comments']);
hide($content['links']);
print render($content);
?>
</div>
<?php print render($content['links']); ?>
<?php print render($content['comments']); ?>
</div>
Where add my "mask" class in this code?
If you only want this rule to apply ONLY to node types then change it to:
.node .mask {
...
}
Drupal adds information about the specific node being viewed as well as the node type in a div in the html. You can use Firebug or just viewing the page html to figure out how you may want to restrict your rule based on the classes present for different content types.
If the content type you want this rule to apply to has a machine name of 'event', for example, you'll see that Drupal adds the class 'node-event' to each node of that type, and you can use that to restrict your rule even further:
.node-event .mask {
...
}
Hope that puts you in the right direction!

Categories