Alphabetic Pagination in CakePHP

Open your controller file,

function index($page = 1) {
 $alphabets = range('A', 'Z');
 $alphagroups = array_chunk($alphabets,3);
 $alphagroups[] = array(0,9);
 $cur_alpha = (isset($page))?($page-1):0;
 if($cur_alpha >= 9){
  $reg_cond = '0123456789';
 } else {
  $reg_cond = trim(implode('', $alphagroups[$cur_alpha]));
 }
 $this->set('cur_alpha', $cur_alpha);
 $this->set('alphas', $alphagroups);
 $total = $this->Vendor->find('count');
 $this->set('total', $total);
 $this->Vendor->recursive = -1;
 $vendors = $this->Vendor->find('all', array(
  'conditions' => array('Vendor.name REGEXP' => '^['.$reg_cond.']')
));
 $this->set('vendors', $vendors);
}

Open index.ctp

foreach ($alphas as $key=>$alpha):
 $alphacode = implode("-",$alpha);
 if($key == $cur_alpha):
    echo $alphacode;
 else: 
    echo $html->link(__($alphacode, true), array('action'=>'index', $key+1));
 endif;
endforeach;

Categories followed by their subcategories in dropdown – Yii2.0

For Example

Parent1
__Child1-Parent1
__Child2-Parent1
__|__Child1-Child2-Parent1
Parent2
__Child1-Parent2
__Child2-Parent2
__|__Child1-Child2-Parent2
Parent3
Parent4
__Child-parent4

Create category table.

CREATE TABLE IF NOT EXISTS `category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`slug` varchar(255) NOT NULL,
`description` text NOT NULL,
`image` varchar(250) NOT NULL,
`order` int(11) NOT NULL,
`parent_id` int(11) NOT NULL,
`status` int(11) NOT NULL DEFAULT '0' COMMENT '0=> inactive, 1=>active',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Open models/Category.php. Then add below code,

public function getCategories($parent_id = 0, $exclude = '', $space = '', $categories='')
{
if($parent_id == 0){
$space = '';
$categories = array();
}else{
$space .='- ';
}

$model = Category::findAll([
'parent_id' => $parent_id,
]);
if(!empty($model)){
foreach ($model as $key) {
if($key->id == $exclude) continue;
$categories[] = array('id'=>$key->id, 'name'=>$space.$key->name);
$categories = $this->getCategories($key->id, $exclude, $space, $categories);
}
}

return $categories;
}

Open views/category/_form.php

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use app\models\Category;
?>

<?php 
 $category = new Category;
 $categories = $category->getCategories(0, $model->id);
 $items = ArrayHelper::map($categories, 'id', 'name');
?>
<?php echo $form->field($model, 'parent_id')->dropDownList($items,['prompt'=>'--None--']); ?>
'btn btn-success']) ?>
<?php ActiveForm::end(); ?> </div>