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";
}
?>
Related
I have a block of code to generate a user's profile that looks like this:
<?php
if($user_isbanned) {
echo 'User is banned.';
exit(1);
}
?>
<!-- Content to show if the user is not banned -->
This works for what I want it to do (stop the profile from appearing if the user's account is banned), but if I were to add in a footer at the bottom of the page, it would not appear on a banned user's page since the exit() statement aborted generating the rest of the page. I could reformat my code to look like this:
<?php
if($user_isbanned) {
echo 'User is banned.';
} else {
// Content to show if the user is not banned
}
?>
This would make my footer appear regardless of whether or not the user is banned, but I don't want to wrap the entire page in one large PHP echo statement. Is there anyway to prevent a certain block of HTML from appearing if a PHP condition is or is not satisfied, without putting the entire page in an echo statement?
What you could do is something like that:
<html>
<head>
</head>
<body>
<div id="header"></div>
<?php if($user_isbanned){ ?>
<div id="content-banned"></div>
<?php } else { ?>
<div id="content-not-banned"></div>
<?php } ?>
<div id="footer"></div>
</body>
Just render different kind of content, when user is banned.
In your first example, a footer will never be displayed. In fact, not even a closing </html> tag, that doesn't seem very desirable.
You don't have to literally echo the HTML, you could just:
<?php
if($user_isbanned) {
echo 'User is banned.';
} else {
?>
<!-- html goes here -->
<?php
}
?>
Alternatively, you could just redirect a banned user to a separate page entirely.
My preferred solution would be an HTTP redirection to an informative page:
<?php
if($user_isbanned) {
header('Location: http://example.com/banned');
exit(1);
}
<!DOCTYPE html>
If you prefer to keep your current design:
<?php
if($user_isbanned) {
echo 'User is banned.';
} else {
?>
<p>Content to show if the user is not banned</p>
<?php
}
N.B. Closing ?> tag is optional.
I'm trying to show a bootstrap module dialog box as a confirm delete on a database.
So the code I have:
<?php
//Sample pop up
if ($delete_category_id > 0) {
<div class="source-code runnable">
BootstrapDialog.alert('I want banana!');
</div>;
//$da->delete_category($delete_category_id);
}
?>
I've tried inserting echo's into the divs but the BootstrapDialog box doesn't work as its supposed to (or at all actually).
How can I insert this and have it work inside PHP?
There are a couple of solutions to this, one would be:
<?php if ($delete_category_id > 0) { ?>
<div class="source-code runnable">
BootstrapDialog.alert('I want banana!');
</div>
<?php } ?>
Another one would be:
<?php if ($delete_category_id > 0) {
echo <<<HTML
<div class="source-code runnable">
BootstrapDialog.alert('I want banana!');
</div>
HTML;
} ?>
For my new wordpress site I need some help. I got 4 pages and 2 different sidebars. On 2 pages there shouldn't be a border-left and on the other 2 pages there should be a border-left. So I got following code:
<div id="sidebar" class="sidebar">
<?php if(is_page('Willkommen') || is_page('Angebot'))
{
dynamic_sidebar('Angebot');
}
else
{
dynamic_sidebar('Anfahrt');
if(is_page('Anfahrt'))
{
echo "<script type='text/javascript'>
function removeBorder()
{
$(document).ready(function() {
if($('#sidebar').hasClass('sidebar'))
{
$(this).removeClass('sidebar')
$(this).addClass('secondsidebar')
}
});
}
</script>";
}
}
?>
</div><!-- sidebar -->
I want to remove the class "sidebar" and add the class "secondsidebar" if there is a div#sidebar on the page. But the code won't work. I wrote this code for myself and I'm a beginner in jQuery :) So please be patient.
I hope someone can give me a hint.
Cheers
A pure PHP solution should be possible as well.
<div id="sidebar" class="<?php if (is_page('Anfahrt')) : ?>secondsidebar<?php else : ?>sidebar<?php endif; ?>">
<?php
if (is_page('Willkommen') || is_page('Angebot')) {
dynamic_sidebar('Angebot');
}
else {
dynamic_sidebar('Anfahrt');
}
?>
</div>
NOTE: try not to use IDs and classes with the same name. #sidebar and .sidebar might get confusing.
Explanation for the classes:
<div id="sidebar" class="<?php if (is_page('Anfahrt')) : ?>secondsidebar<?php else : ?>sidebar<?php endif; ?>">
When the server reads this (PHP is code that is executed by the server before rendering the result back to the user) it comes to "class" and it notices an if-statement. "what text should I put in the class="" of this element?" The server then sees the instructions:
If the page is "Anfahrt": the text to return should be secondsidebar
Else (in all other cases): the text should be sidebar
"Text" isn't really a good name here. But you get what I mean. This "text" is then placed where the if-statement is. The result then is (as it is returned to a user):
If the page is "Anfahrt":
<div id="sidebar" class="secondsidebar">
If the page is anything else:
<div id="sidebar" class="sidebar">
I have an opencart category page that I am trying to hide the title DIV for. Out of all the categories and sub cats there is (for now) only one that I am trying to hide.
The following code I have used does not work:
<?php if ($categories && $heading_title!="HEADING TITLE HERE") { ?>
<?php elseif : ?>
<div id="headerhide" style="width:800px;">
<h1><?php echo $heading_title; ?></h1>
</div>
<?php } ?>
<?php endif ?>
<?php if ($categories && $heading_title!="HEADING TITLE HERE") { ?>
<?php }
else {
?>
<div id="headerhide" style="width:800px;">
<h1><?php echo $heading_title; ?></h1>
</div>
<?php } ?>
<?php if ($categories && $heading_title!="HEADING TITLE HERE") { } else { ?>
<div id="headerhide" style="width:800px;">
<h1><?=$heading_title?></h1>
</div>
<?php } ?>
I have this setup on my OpenCart information.tpl page:
<?
// If FAQs page or content show AJAX
// We check for case sensitive titles - the title is case sensitive
if($heading_title=="FAQs" || $heading_title=="Faqs") { ?>
<script type="text/javascript">
$(document).ready(function(){
// ETC
});
</script>
<?
// Not the FAQs page so do nothing
}
?>
I've recently made a commercial vQmod that will be of use to you for this. It's called Custom Templates and will allow you to assign it to as many individual category pages as you want. It works for more than just categories too (manufacturer, product and info pages). While it may be a bit overkill for one page, if you plan on using it for multiple pages in the future it will be better in the long run in my opinion
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 } ?>