How to make and if else / conditional division in cakephp - php

How to make a condition in CakePHP?
In WordPress I can only make it using
<?php if(is_page('1')){ ?>
<section id="flash-area">
<div class="flash oswald">
.....................
</div>
</section>
<?php }else {?>
//i will removed this div
<?php } ?>

Since Wordpress and CakePHP are both build with PHP you can just use plain PHP for this. In your Template file (e.g. src/Template/Pages/home.ctp) do:
<?php if ($condition) { ?>
<section id="flash-area">
<div class="flash oswald">
.....................
</div>
</section>
<?php } else { ?>
<div>Another div</div>
<?php } ?>
You can also use alternative control structures for cleaner code:
<?php if ($condition): ?>
<section id="flash-area">
<div class="flash oswald">
.....................
</div>
</section>
<?php endif; ?>
Since you didn't wrote, which condition you want to test, I assume you want to check if you're on a specific (static) page of the PagesController. Your Condition would just look like:
<?php if ($page == 'home'): ?>
You can set the conditional variables in a Controller.

Related

How to Wrap A Condition Inside Another Condition in PHP

Can anyone please help me wrap the condition inside another condition in php.
I have this code #1 that I want to be inside code #2.
Here's code #1
<?php if( get_field('highlights') ): ?>
<div class="overview">
<h3>Quick Overview</h3>
<?php the_field('highlights'); ?>
</div>
<?php endif; ?>
Here's code #2
<?php if(strstr($_SERVER['HTTP_REFERER'],'www.example.com'))
{
echo '**CODE #1 should be placed here**';
}
?>
Sorry, I don't haev any knowledge in PHP.
Wrapping code 1 inside code 2
After several trial and error, here's what I have to make it work. Please correct me if there's something wrong or to improve.
<?php if (strstr($_SERVER['HTTP_REFERER'], 'www.google.com')){ ?>
<?php if( get_field('highlights') ):?>
<div class="overview">
<h3>Quick Overview</h3>
<?php the_field('highlights'); ?>
</div>
<?php endif; ?>
<?php } ?>
This should work in theory. Though I'm unsure what the highlights field is for.
<?php
if(strstr($_SERVER['HTTP_REFERER'],'www.example.com')){
if( get_field('highlights') ){ ?>
<div class="overview">
<h3>Quick Overview</h3>
<?php the_field('highlights'); ?>
</div>
<?php
}
}
?>
You may want to replace <?php the_field('highlights'); ?> with something like <?=highlight ?> and iterate through the highlights in a loop. That depends on the situation though.

How to show a specific <div> for a specific product?

I have setup a small webstore and I want to show this <div class ="data-plan"></div> only for one single product. My database table from where I want to match this id is called sb_files and it has these fields id table_id path type slide_link, so I am trying to get my code to go trough that table, search up the id ( Its 13040100 ) and if it matches then to show the div, else it shows an empty div. I am using the Yii2 framework, which is php based. So far I have tried this
<?php if($product->$this->id('13040100')): ?>
<ul>
<?php
$index = 1;
foreach($product->() as $prd):
if(strpos($prd->getPath(), '13040100') == true) {
?>
<div class="wrap">
<div class="data-plan" style="height:20px; width:65px; background-color:#00a651; float:right;color:white;margin:10px;text-align:center;">A+++ </div>
<div class="data-plan" style="height:20px; width:65px; background-color:#ed1c1c; float:right;color:white;margin:10px;">Datu lapa </div>
</div>
<?php
$index++;
}
endforeach;
?>
</ul>
<?php else: ?>
<div class="wrap">
</div>
<?php endif; ?>
I'm not that familiar with Yii2 framework myself, but are you sure that $product->$this->id('13040100') is correct? It looks weird to me, shouldn't it be something like $product->id('13040100')?
The solution was way easier than I tought, just had to think simple
<?php if ($product->id == '13001100'): ?>
<div class="wrap">
<div class="data-plan" style="height:20px; width:65px; background-color:#00a651; float:right;color:white;margin:10px;text-align:center;">A+++ </div>"
<div class="data-plan" style="height:20px; width:65px; background-color:#ed1c1c; float:right;color:white;margin:10px;">Datu lapa </div>
</div>
<?php else: ?>
<div class="wrap">
</div>
<?php endif;

Joomla Error Message File Directory

I was trying to learn how to create a joomla template. I successfully override some modules and components on my template however I can't override the error message especially on form validation.
I would like to know where the files are located and is it possible to override? My purpose is to add some bootsrap classes and add more tags on it.
The actual generated system message is in /layouts/joomla/system/message.php
<div id="system-message-container">
<?php if (is_array($msgList) && !empty($msgList)) : ?>
<div id="system-message">
<?php foreach ($msgList as $type => $msgs) : ?>
<div class="alert alert-<?php echo $type; ?>">
<?php // This requires JS so we should add it trough JS. Progressive enhancement and stuff. ?>
<a class="close" data-dismiss="alert">×</a>
<?php if (!empty($msgs)) : ?>
<h4 class="alert-heading">
<?php echo JText::_($type); ?>
</h4>
<div>
<?php foreach ($msgs as $msg) : ?>
<div class="alert-message">
<?php echo $msg; ?>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
You can fix template CSS at /template/(your_template)/css/template.css
Or you can make new CSS with /template/(your_template)/css/my.css
and include it in the /template/(your_template)/index.php:
$doc->addStyleSheetVersion($this->baseurl . '/templates/' . $this->template . '/css/my.css');
If you need to temporary override CSS class from your CSS you can use " !important" about the overrided attribe in the CSS file.

How to make the menu my sticky header in wordpress

So far I have the following header on my site:
Right now, when you scroll down the page, the sticky header is the LOGO, Phone numnber and the Request service button in red. How could I change this to use the Services menu below to be my sticky header instead?
I checked the code of the Header.php and found the following lines:
// Sticky header
if(isset($theme_options['enable_sticky_header']) && $theme_options['enable_sticky_header']) {
$header_add_class = ' class="'.esc_attr('sticky-header main-header').'"';
} else {
$header_add_class = ' class="'.esc_attr('main-header').'"';
}
?>
<header<?php echo $header_add_class; ?>>
<div class="container<?php echo esc_attr($header_container_add_class); ?>">
<div class="row">
<div class="col-md-12">
<div class="header-left logo">
<?php
// Center logo
if(isset($theme_options['header_logo_position']) && $theme_options['header_logo_position'] == 'center'): ?>
<?php magnium_header_center_show(); ?>
<?php else: ?>
<?php magnium_header_left_show(); ?>
<?php endif;
// Center logo END
?>
</div>
<div class="header-center">
<?php
// Center logo
if(isset($theme_options['header_logo_position']) && $theme_options['header_logo_position'] == 'center'):
?>
<?php magnium_header_left_show(); ?>
<?php else: ?>
<?php magnium_header_center_show(); ?>
<?php
endif;
// Center logo END
?>
</div>
<div class="header-right">
<?php magnium_header_right_show(); ?>
</div>
</div>
</div>
</div>
<?php magnium_menu_below_header_show(); ?>
</header>
I guess this line magnium_menu_below_header_show() is responsible for displaying the menu, but I really do not know where to search it. Although I looked in functions.php, I could not find it.
I know I could use a plugin, but I just do not want to always rely on plugins if I can make it by myself.

HTML and PHP between PHP if Tags

Im a beginner in php and want to get help from you guys here,
I have a php include tag which i want it to be between a html tag and both of them between a php if tag ... I have tried this and is giving erro, please help.
<?php if ($menu->getActive() == $menu->getDefault()) ?>
<section class="content">
<?php { include '_include-content.php'; } ?>
</section>
<?php endif ?>
Do like this [Enclose any HTML content inside echo statement.]
<?php
if ($menu->getActive() == $menu->getDefault())
{
echo "<section class='content'>";
include '_include-content.php';
echo "</section>";
}
?>
According to
http://www.php.net/manual/en/control-structures.if.php
http://www.php.net/manual/en/control-structures.alternative-syntax.php
<?php if ($menu->getActive() == $menu->getDefault()): ?>
<section class="content">
<?php include '_include-content.php'; ?>
</section>
<?php endif ?>
or
<?php if ($menu->getActive() == $menu->getDefault()){ ?>
<section class="content">
<?php include '_include-content.php'; ?>
</section>
<?php } ?>
should work fine..
The PHP if function works as follow
if (conditions) {
// do sth
}
or
if (conditions):
// do sth
endif;
You made a mix using the brackets and endif
so for your Problem, try:
<?php if ($menu->getActive() == $menu->getDefault()) { ?>
<section class="content">
<?php include '_include-content.php'; ?>
</section>
<?php } ?>
I suggest this syntax because it is normally used.
For your problem, see the note in http://php.net/manual/en/control-structures.alternative-syntax.php
Note: Mixing syntaxes in the same control block is not supported.
<?php if ($menu->getActive() == $menu->getDefault()){ ?>
<section class="content">
<?php include '_include-content.php'; ?>
</section>
<?php } ?>

Categories