PHP beginner here. Looking for a more efficient code.
I have a model, named Brands and I am trying to extract, sort and display the values alphabetically in my view.
For example:
Brands' name starting from the alphabet A
- Brand1
- Brand2 .....etc.
Brands' name starting from the alphabet K
- Brand1
- Brand2 .....etc.
I am able to do the same by the following code:
$brandsTable = TableRegistry::get('Brands');
$brandA = $brandsTable->find('all')->where([
'deleted =' => 0
])-> andWhere(['brand_name_eng LIKE :key'
])->bind(':key', 'A%'
)-> orWhere(['brand_name_eng LIKE :key'
])->bind(':key', 'a%');
$brandK = $brandsTable->find('all')->where([
'deleted =' => 0
])-> andWhere(['brand_name_eng LIKE :key'
])->bind(':key', 'K%'
)-> orWhere(['brand_name_eng LIKE :key'
])->bind(':key', 'k%');
$brandS = $brandsTable->find('all')->where([
'deleted =' => 0
])-> andWhere(['brand_name_eng LIKE :key'
])->bind(':key', 'S%'
)-> orWhere(['brand_name_eng LIKE :key'
])->bind(':key', 's%');
$brandT = $brandsTable->find('all')->where([
'deleted =' => 0
])-> andWhere(['brand_name_eng LIKE :key'
])->bind(':key', 'T%'
)-> orWhere(['brand_name_eng LIKE :key'
])->bind(':key', 't%');
$brandN = $brandsTable->find('all')->where([
'deleted =' => 0
])-> andWhere(['brand_name_eng LIKE :key'
])->bind(':key', 'N%'
)-> orWhere(['brand_name_eng LIKE :key'
])->bind(':key', 'n%');
$brandH = $brandsTable->find('all')->where([
'deleted =' => 0
])-> andWhere(['brand_name_eng LIKE :key'
])->bind(':key', 'H%'
)-> orWhere(['brand_name_eng LIKE :key'
])->bind(':key', 'h%');
$brandM = $brandsTable->find('all')->where([
'deleted =' => 0
])-> andWhere(['brand_name_eng LIKE :key'
])->bind(':key', 'M%'
)-> orWhere(['brand_name_eng LIKE :key'
])->bind(':key', 'm%');
$brandY = $brandsTable->find('all')->where([
'deleted =' => 0
])-> andWhere(['brand_name_eng LIKE :key'
])->bind(':key', 'Y%'
)-> orWhere(['brand_name_eng LIKE :key'
])->bind(':key', 'y%');
$brandR = $brandsTable->find('all')->where([
'deleted =' => 0
])-> andWhere(['brand_name_eng LIKE :key'
])->bind(':key', 'R%'
)-> orWhere(['brand_name_eng LIKE :key'
])->bind(':key', 'r%');
$brandW = $brandsTable->find('all')->where([
'deleted =' => 0
])-> andWhere(['brand_name_eng LIKE :key'
])->bind(':key', 'W%'
)-> orWhere(['brand_name_eng LIKE :key'
])->bind(':key', 'w%');
$this->set('brandsA', $brandA);
$this->set('brandsK', $brandK);
$this->set('brandsS', $brandS);
$this->set('brandsT', $brandT);
$this->set('brandsN', $brandN);
$this->set('brandsH', $brandH);
$this->set('brandsM', $brandM);
$this->set('brandsY', $brandY);
$this->set('brandsR', $brandR);
$this->set('brandsW', $brandW);
I believe it is not the most efficient way to call the model / execute query so many times. Tried looking for efficient solution (Hash:extract / sort, etc.) But unable to get a proper output. Would appreciate your help. Thanks!
Note: deleted flag is for logical delete.
Update: After ThinTank and ndm comment Sorry for not mentioning that, As it is a Japanese website I am looking for case sensitive brands' name starting from
[ A,E,I,O,U ],[ K ],[ S ],[ T ],[ N ],[ H ],[ M ],[ Y ],[ R ],[ W ]
respectively.Example : I have brands as follows
adidas, Allison, Edox, Reebok, Tissot, etc.
Output should be: Brands' name starting from ア [A,E,I,O,U]: adidas Allison Edox
Brands' name starting from ラ [R]: Reebok
Brands' name starting from タ [T]: Tissot ... and so on,