how to use cakePHP params to set unordered list items as active - cakephp

I want to be able to set a navigational list item as active by using cakePHP params. This would visually tell the visitor which page they're currently on. I've figured out how to do it for the current controller but, I'm routing specific rows in the post controller for pages such as 'about us' and it's not working even though I'm trying to check for the 'pass' param. Here's the code:
In an element called "leftNavBar.ctp' I've got the following:
<?php
$current_pass = $this->params['pass'];
$current_controller = $this->params['controller'];
?>
<ul class="nav">
<li class="<?php if(in_array($current_pass, array(2))){echo 'active';} ?>">
<?php echo $this->Html->link(__("About Us",true),"/about-us") ?>
</li>
<li class="<?php if(in_array($current_controller, array('galleries'))){echo 'active';} ?>">
<?php echo $this->Html->link(__("Galleries",true),"/galleries") ?>
</li>
</ul>
This is the router instructions for the 'about us' page:
Router::connect('/about-us',array
('controller' => 'posts', 'action' => 'view', 2));
Because the Posts controller has other rows/list items that I also want to set as active in the left nav bar, what I want to figure out is how to be able to do that?
thanks

You can try setting variable inside the controller and access in view like
In Controller
function view($id) {
..........
..........
$this->set('current_pass',$id);
}
In View
<?php
$current_controller = $this->params['controller'];
?>
<ul class="nav">
<li class="<?php if(in_array($current_pass, array(2))){echo 'active';} ?>">
<?php echo $this->Html->link(__("About Us",true),"/about-us") ?>
</li>
<li class="<?php if(in_array($current_controller, array('galleries'))){echo 'active';} ?>">
<?php echo $this->Html->link(__("Galleries",true),"/galleries") ?>
</li>
</ul>

What I ended up doing with luck was the following:
<?php
$url = $this->Html->url() ;
$current_controller = $this->params['controller'];
?>
<ul class="nav">
<li <?php if($url == '/NAME-OF-URL-HERE/about-us') echo 'class="active"';?>>
<?php echo $this->Html->link(__("About Us",true),"/about-us") ?>
</li>
<li class="<?php if(in_array($current_controller, array('galleries'))){echo 'active';} ?>">
<?php echo $this->Html->link(__("Galleries",true),"/galleries") ?>
</li>
</ul>
This works well since not all URLS are handled the same way. Some URLs are controllers, others are specific IDs within controllers.
Hope this helps others.
Paul

Related

Use PaginatorHelper in Cells CakePhp 3

Hi I'm trying to use Paginator within a Cell the Paginator works rigth but the PaginatorHelper in the Template of the cell just doesn't work.
I'm using CakePhp 3.5
Here is my code
src/View/Cell/FinderCell.php
namespace App\View\Cell;
use Cake\View\Cell;
use Cake\Datasource\Paginator;
class FinderCell extends Cell {
public function display() {
$this->loadModel('Pokemon');
$paginador = new Paginator();
$pokemons = $paginador->paginate(
$this->Pokemon->find()
);
$this->set(compact('pokemons'));
}
}
src/Template/Cell/Finder/display.ctp
<?php
foreach ($pokemons as $pokemon) :
?>
<div class="tipo form large-2 medium-2 columns content">
<?php echo $this->Html->image($pokemon->pokemon_image) ?>
</div>
<?php endforeach; ?>
<div class="paginator">
<ul class="pagination">
<?= $this->Paginator->prev('<< Anterior') ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next('Siguiente >>') ?>
</ul>
<p><?= $this->Paginator->counter() ?></p>
</div>
I get this
If you want to use the helper, then you need to populate either the request object with the pagination parameters, which is what the paginator component would normally do for you:
// ...
$this->request = $this->request->withParam(
'paging',
$paginator->getPagingParams() + (array)$this->request->getParam('paging')
);
$this->set(compact('pokemons'));
or the paginator helper, which in turn sets the parameters on the request object:
// ...
$pagingParams = $paginator->getPagingParams();
$this->set(compact('pokemons', 'pagingParams'));
$this->Paginator->options(['paging' => $pagingParams]);
See also
API > \Cake\Datasource\PaginatorInterface::getPagingParams()
API > \Cake\View\Helper\PaginatorHelper::options()

Cakephp working whit Cell

Hi I have a trouble whit Cell in my application I follow the tutorial but did not work for me
here is my code https://github.com/boguda/okpionir.git
I bake cell for News and add display function
I want to use this cell on my static page src/Template/Pages/pocetna.ctp
but it doesn't work
just change
<?php $cell = $this->cell('News'); ?>
<?= $cell ?>
to
<?= $this->cell('News::display'); ?>
Call Cell has to refer to specific method name
EDIT:
Also you have an error in your src/View/Cell/NewsCell
public function display()
{
$this->loadModel('News');
----> $top_news= $this->News->find()
->select('title')
->order(['created'=>'DESC'])
->limit(3)
->toArray();
$this->set('top_news' => $news); <-----
}
should be:
$this->set('top_news', $top_news);
or
$this->set(compact('top_news'));
And your view Template/Cell/News/display.ctp might look like this
<ul class="list-group">
<?php foreach ($top_news as $news): ?>
<li><?= h($news['title']); ?></li>
<?php endforeach; ?>
<li class="list-group-item">Second item</li>
<li class="list-group-item">Third item</li>
</ul>

How to get controller view inside an element?

I have created an element having a text field(Status Bar same as Facebook) which i extended in home/display view.
Code for element:
<?php
echo $this->Html->css('notes');
echo $this->Html->script(array('jquery','jquery.elastic','notes'));
?>
<div class="notescontainer">
<div class="share">Share:</div>
<div class="notes">Notes</div>
<div class="loading"></div>
<div class="img_top"></div>
<div class="text_status">
<?php
echo $this->form->create('Note', array('action' => 'notes'));
echo $this->form->input('notes',array('class' => 'notesbar', 'label'=>'', 'placeholder' => 'Write down you notes ...'));
?>
</div>
<div class="button_outside_border" id="share">
<div class="button_inside_border">
<?php
echo $this->form->submit('Share',array('alt' => 'Add', 'class' => 'sharebutton'));
echo $this->form->end();
?>
</div>
</div>
<div class="clear"></div>
<div class="load_status_out"></div>
</div>
I want that when user shares the status it should re-appear on the same page that is display/home and for that i created a controller for retrieving the status entered by the user.
Controller:
<?php public function notes()
{
$this->set('allnotes', $this->Note->find('all'));
}?>
Controller View:
<?php
foreach($allnotes as $viewnotes):
{
echo $viewnotes['Note']['notes'];
echo "<br>";
}
endforeach;
?>
Now i want this controller view to be displayed on home/display view. Is there any way i can extend it in that element? I don`t want to add it directly on my home/display view as it already fully loaded.
You can use requestAction() to accomplish this, but in my experience it is buggy and a sign of design failure if you need it.
Alternatively you can turn all your view into elements, which can be embedded in each other in any arbitrary way. Then you can just call some shared code in the controller to populate all required models.

Category / sub-category loop for drop down menu

I'm trying to dynamically populate a drop down menu with categories and subcategories from a database table. My problem is that I can't figure out a way to loop through the subcategories so they display under the correct category.
In my SQL database I have a table called 'Categories' with 3 columns: id, category, subcategory. The subcategories in the subcategory column are separated by 3 colon marks (Artists:::Childcare:::Classes:::Event).
I'm using unordered lists in html to populate the drop down menu.
<ul id="menu">
<li>button
<ul>
<?php
while($allCategories=mysql_fetch_assoc($resultre1)){ ?>
<li><a href="#"><div class="whatever"<?php print
$relanguage_tags[$allCategories['category']];?>
<?php if(in_array($relanguage_tags[$allCategories['category']],$reCategory))
print "selected='selected'"; ?> >
<?php print $relanguage_tags[$allCategories['category']]; ?></div></a>
<?php } ?>
<ul>
<?php
if($ptype=="showOnMap" || $ptype=="viewFullListing" || $ptype=="home" || $ptype==""){
$reCategoryString=getCommaStringFromArray($reCategory);
$reqr2="select * from $categoryTable where id like '%' ".getRealValue($reCategoryString,"reCategory");
$resultre2=mysql_query($reqr2);
?>
<?php
while($allSubCategories=mysql_fetch_assoc($resultre2)){
$subCatList=explode(":::",$allSubCategories['subcategories']);
$subCatSize=sizeof($subCatList);
for($i=0;$i<$subCatSize;$i++){
?>
<li><a href="#"><div class="whatever"<?php print $relanguage_tags[$subCatList[$i]];?>'
<?php if(in_array($relanguage_tags[$subCatList[$i]],$reSubcategory)) print "selected='selected'"; ?> ><?php print $relanguage_tags[$subCatList[$i]]; ?></div></a></li>
<?php }
} ?>
<?php } ?>
</ul>
</li>
</ul>
</li>
</ul>
$subCatList=explode(":::",$allSubCategories['subcategories']);
$subCatSize=count($subCatList);
for($i=0;$i<$subCatSize;$i++){
echo "<li>".$subCatList[$i]."</li>";
?>
if this isn't the answer means I didn't understood your question well.
EDIT
Make a function that shows all the subcategories from a category table:
function subCategories($category){
$subCategories="";
while($r=mysql_fetch_assoc(mysql_query("select * from ".$category.""))){
$subCategories.="<li>".$r['subCategory']."</li>";
}
return $subCategories;
}
and call this function like this echo subCategories("Housing"); in each category tag.
Hope it helped!

Is it possible to get a different header.php on language switch with qtranslate (Wordpress)?

the problem is that on home page I have a lot of code which is added through header.php and it is not amendable through admin panel. What I want to do is to switch header files on language switch. Whenever a user press on the language icon, lets say on the English language icon the header should also switch from <?php get_header('mylanguage') ?> to <?php get_header('english') ?> or etc. Is it possible to do like this?
Regards,
In your page template instead of calling header through:
get_header();
use this to enable header switching when changing languages;
<?php if (qtrans_getLanguage() == 'de'): ?>
<?php include(TEMPLATEPATH.'/header-home-de.php' ); ?>
<?php else : ?>
<?php include(TEMPLATEPATH.'/header-home-en.php' ); ?>
<?php endif; ?>
In this example there are 2 headers depending on selected language.
Surely, it is possible in qTranslate. Even I've done qTranslate Swicher in bootstrap navigation.
<?php } if (qtranxf_getLanguage() == 'en') { ?>
English
<ul class="dropdown-menu">
<?php echo qtranxf_generateLanguageSelectCode('code'); ?>
</ul>
<?php } elseif (qtranxf_getLanguage() == 'fr') { ?>
Français
<ul class="dropdown-menu">
<?php echo qtranxf_generateLanguageSelectCode('code'); ?>
</ul>
<?php } ?>

Resources