2

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

  1. Brand1
  2. Brand2 .....etc.

Brands' name starting from the alphabet K

  1. Brand1
  2. 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,

2
  • Do you want all the brands ? Or only this concerns by the letters 'A', 'K', 'S', 'T', 'N', 'H', 'M', 'Y', 'R', 'W' ? Commented Feb 22, 2016 at 10:00
  • 1
    Please be more specific as to what exactly you are trying to do, for example by adding an example of the data in your database, the final data that you are looking for, and the actual conditions that are required to retrieve it. Also since you are somehow trying to take case sensitivity into account, you should make clear if that is really required, ie whether your database/table/column is actually using a case sensitive collation. Commented Feb 22, 2016 at 19:18

1 Answer 1

2

Here is one way to factorize it :

$listBrandsLetter = array('A', 'K', 'S', 'T', 'N', 'H', 'M', 'Y', 'R', 'W');

$brandsTable = TableRegistry::get('Brands');

foreach($listBrandsLetter as $letter)
{
    $currentBrand = $brandsTable->find('all')->where([
                        'deleted =' => 0
                        ])-> andWhere(['brand_name_eng LIKE :key'
                        ])->bind(':key', $letter.'%'
                        )-> orWhere(['brand_name_eng LIKE :key'
                        ])->bind(':key', strtolower($letter).'%');

    $this->set('brands'.$letter, $currentBrand);
}

If your objective is to reduce the number of query due to performance issues, thanks to notify it. If performance is okay, just factoring should be enough

Sign up to request clarification or add additional context in comments.

1 Comment

$listBrandsLetter may be obtained from the database, too, like this: SELECT DISTINCT SUBSTR(brand_name_eng,1,1) FROM ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.