N
N
nevesomostjke2022-02-14 02:57:53
PHP
nevesomostjke, 2022-02-14 02:57:53

How to speed up foreach?

Hello, I have a foreach in a laravel application, or rather a foreach in a foreach.
All that happens is checking the availability of goods at the price list in the database. Because of this, the page loads very slowly.

view:

@foreach ($parts as $part)
      <?php $aval = 0; ?>
      @foreach ($prices as $k=>$v)
      <?php
        
        // Поиск наличия
        $coem = str_replace(' ', '', $part->part_number);
        $poem = str_replace(' ', '', $v->oem);

        $coem = str_replace('-', '', $coem);
        $poem = str_replace('-', '', $poem);

        $coem = str_replace('-', '.', $coem);
        $poem = str_replace('-', '.', $poem);

        $coem = str_replace('(', '.', $coem);
        $poem = str_replace('(', '.', $poem);

        $coem = str_replace(')', '.', $coem);
        $poem = str_replace(')', '.', $poem);	

        $sim = similar_text($coem, $poem, $perc);
        if ($perc==100) { 
          if ($v->count >= 1) {
            $aval = 1;
            break;
          }
        }
        ?>
      @endforeach


Here is the controller:
// Получаем характеристики продукта
                    $attributes = DB::table('article_attributes')
                    ->where('datasupplierarticlenumber', '=', $request->part_number)
                    ->where('supplierid', '=', $supplier->id)
                    ->get();

                    // Название изделия (есть еще статус изделия и не только см. таблицу articles)
                    $article = DB::table('articles')
                    ->select('NormalizedDescription', 'ArticleStateDisplayValue')
                    ->where('DataSupplierArticleNumber', '=', $request->part_number)
                    ->where('supplierid', '=', $supplier->id)
                    ->first();

                    // Файлы продукта
                    $files = DB::table('article_images')
                    ->select('Description', 'PictureName')
                    ->where('DataSupplierArticleNumber', '=', $request->part_number)
                    ->where('supplierId', '=', $supplier->id)
                    ->get();

                    // Применимость изделия
                    $result = [];
                    $rows = DB::table('article_li')
                    ->select('linkageTypeId', 'linkageId')
                    ->where('DataSupplierArticleNumber', '=', $request->part_number)
                    ->where('supplierId', '=', $supplier->id)
                    ->get();
                    
                    // разбор
                    foreach ($rows as &$row) {
                        $result[]= DB::select("SELECT DISTINCT p.id, mm.description make, m.description model, p.constructioninterval, p.description FROM passanger_cars p 
                        JOIN models m ON m.id=p.modelid
                        JOIN manufacturers mm ON mm.id=m.manufacturerid
                        WHERE p.id=" . $row->linkageId);
                    }

                    // Массив с информацией о товаре
                    $info = array(
                        "part_number" => $request->part_number, // OEM номер
                        "supplier_name" => $request->supplier_name, // Имя производителя
                        "supplier_id" => $supplier->id, // ID производителя
                    );

                    // Выгружаем прайсы
                    $prices = DB::table('prices')
                    ->select('oem', 'price', 'count', 'stock')
                    ->get();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur Maslov, 2022-02-14
@tmaslov22

You need to check the availability of goods by price in the database through an SQL query. In this example, data is pulled from several database tables and then calculations are made through php. It is necessary that the calculations would be done by the DBMS, not PHP.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question