Skip to main content
Code Review

Return to Answer

added 824 characters in body
Source Link
lukas.j
  • 244
  • 2
  • 4

Since the example code shows for each of the 3 product results 4 identical entries, the first step would be to replace the 4 div with a for loop.

And because the structure of the 3 product tables are almost identical – they differ only in one key (namely 'Size', 'Weight', and 'Dimension') –, the second step would be to simplify that into another, outer for loop.

...
// after querying the database:
$all = [ $products, $product, $productt ];
...
// replacement for all 12 divs:
<section class="product-list-wrapper">
 <?php foreach ($all as $entry) : ?>
 <?php for ($j = 0; $j < 4; $j++) : ?>
 <div class="div-box">
 <input type="checkbox" class="delete-checkbox" />
 <table>
 <tbody>
 <?php foreach ($entry as $item) : ?>
 <tr class="content">
 <td><?php echo $item['SKU']; ?> </td>
 <td><?php echo $item['Name']; ?> </td>
 <td><?php echo $item['Price']; ?> </td>
 <td><?php echo $item['Size'] ?? $item['Weight'] ?? $item['Dimension']; ?> </td>
 </tr>
 <?php endforeach; ?>
 </tbody>
 </table>
 </div>
 <?php endfor; ?>
 <?php endforeach; ?>
</section>
...

Edit: Based on @mickmackusa's comment below this could be a way to reduce the three database trips to one:

$statement = $pdo->prepare('SELECT * FROM skandi WHERE id = IN(1, 2, 3)');
$statement->execute();
$products = $statement->fetchAll(PDO::FETCH_ASSOC);
$all = [
 array_filter($products, fn($item) => $item['id'] === 1),
 array_filter($products, fn($item) => $item['id'] === 2),
 array_filter($products, fn($item) => $item['id'] === 3)
];

Depending on the count of rows returned from the database, this might be faster or slower. My assumption would be that for very large amount of rows it would be faster to query three times, because database engines excel at filtering, while for smaller amounts of rows, one query with three array_filter calls would be faster.

Since the example code shows for each of the 3 product results 4 identical entries, the first step would be to replace the 4 div with a for loop.

And because the structure of the 3 product tables are almost identical – they differ only in one key (namely 'Size', 'Weight', and 'Dimension') –, the second step would be to simplify that into another, outer for loop.

...
// after querying the database:
$all = [ $products, $product, $productt ];
...
// replacement for all 12 divs:
<section class="product-list-wrapper">
 <?php foreach ($all as $entry) : ?>
 <?php for ($j = 0; $j < 4; $j++) : ?>
 <div class="div-box">
 <input type="checkbox" class="delete-checkbox" />
 <table>
 <tbody>
 <?php foreach ($entry as $item) : ?>
 <tr class="content">
 <td><?php echo $item['SKU']; ?> </td>
 <td><?php echo $item['Name']; ?> </td>
 <td><?php echo $item['Price']; ?> </td>
 <td><?php echo $item['Size'] ?? $item['Weight'] ?? $item['Dimension']; ?> </td>
 </tr>
 <?php endforeach; ?>
 </tbody>
 </table>
 </div>
 <?php endfor; ?>
 <?php endforeach; ?>
</section>
...

Since the example code shows for each of the 3 product results 4 identical entries, the first step would be to replace the 4 div with a for loop.

And because the structure of the 3 product tables are almost identical – they differ only in one key (namely 'Size', 'Weight', and 'Dimension') –, the second step would be to simplify that into another, outer for loop.

...
// after querying the database:
$all = [ $products, $product, $productt ];
...
// replacement for all 12 divs:
<section class="product-list-wrapper">
 <?php foreach ($all as $entry) : ?>
 <?php for ($j = 0; $j < 4; $j++) : ?>
 <div class="div-box">
 <input type="checkbox" class="delete-checkbox" />
 <table>
 <tbody>
 <?php foreach ($entry as $item) : ?>
 <tr class="content">
 <td><?php echo $item['SKU']; ?> </td>
 <td><?php echo $item['Name']; ?> </td>
 <td><?php echo $item['Price']; ?> </td>
 <td><?php echo $item['Size'] ?? $item['Weight'] ?? $item['Dimension']; ?> </td>
 </tr>
 <?php endforeach; ?>
 </tbody>
 </table>
 </div>
 <?php endfor; ?>
 <?php endforeach; ?>
</section>
...

Edit: Based on @mickmackusa's comment below this could be a way to reduce the three database trips to one:

$statement = $pdo->prepare('SELECT * FROM skandi WHERE id = IN(1, 2, 3)');
$statement->execute();
$products = $statement->fetchAll(PDO::FETCH_ASSOC);
$all = [
 array_filter($products, fn($item) => $item['id'] === 1),
 array_filter($products, fn($item) => $item['id'] === 2),
 array_filter($products, fn($item) => $item['id'] === 3)
];

Depending on the count of rows returned from the database, this might be faster or slower. My assumption would be that for very large amount of rows it would be faster to query three times, because database engines excel at filtering, while for smaller amounts of rows, one query with three array_filter calls would be faster.

deleted 6 characters in body
Source Link
lukas.j
  • 244
  • 2
  • 4

Since the example code shows for each of the 3 product results 4 identical entries, the first step would be to replace the 4 div with a for loop.

And because the structure of the 3 product tables are almost identical – they differ only in one key (namely 'Size', 'Weight', and 'Dimension') –, the second step would be to simplify that into another, outer for loop.

...
// after querying the database:
$all = [ $products, $product, $productt ];
...
// replacement for all 12 divs:
<section class="product-list-wrapper">
 <?php foreach ($all as $entry) : ?>
 <?php for ($j = 0; $j < 4; $j++) : ?>
 <div class="div-box">
 <input type="checkbox" class="delete-checkbox" />
 <table>
 <tbody>
 <?php foreach ($entry as $i => $item) : ?>
 <tr class="content">
 <td><?php echo $item['SKU']; ?> </td>
 <td><?php echo $item['Name']; ?> </td>
 <td><?php echo $item['Price']; ?> </td>
 <td><?php echo $item['Size'] ?? $item['Weight'] ?? $item['Dimension']; ?> </td>
 </tr>
 <?php endforeach; ?>
 </tbody>
 </table>
 </div>
 <?php endfor; ?>
 <?php endforeach; ?>
</section>
...

Since the example code shows for each of the 3 product results 4 identical entries, the first step would be to replace the 4 div with a for loop.

And because the structure of the 3 product tables are almost identical – they differ only in one key (namely 'Size', 'Weight', and 'Dimension') –, the second step would be to simplify that into another, outer for loop.

...
// after querying the database:
$all = [ $products, $product, $productt ];
...
// replacement for all 12 divs:
<section class="product-list-wrapper">
 <?php foreach ($all as $entry) : ?>
 <?php for ($j = 0; $j < 4; $j++) : ?>
 <div class="div-box">
 <input type="checkbox" class="delete-checkbox" />
 <table>
 <tbody>
 <?php foreach ($entry as $i => $item) : ?>
 <tr class="content">
 <td><?php echo $item['SKU']; ?> </td>
 <td><?php echo $item['Name']; ?> </td>
 <td><?php echo $item['Price']; ?> </td>
 <td><?php echo $item['Size'] ?? $item['Weight'] ?? $item['Dimension']; ?> </td>
 </tr>
 <?php endforeach; ?>
 </tbody>
 </table>
 </div>
 <?php endfor; ?>
 <?php endforeach; ?>
</section>
...

Since the example code shows for each of the 3 product results 4 identical entries, the first step would be to replace the 4 div with a for loop.

And because the structure of the 3 product tables are almost identical – they differ only in one key (namely 'Size', 'Weight', and 'Dimension') –, the second step would be to simplify that into another, outer for loop.

...
// after querying the database:
$all = [ $products, $product, $productt ];
...
// replacement for all 12 divs:
<section class="product-list-wrapper">
 <?php foreach ($all as $entry) : ?>
 <?php for ($j = 0; $j < 4; $j++) : ?>
 <div class="div-box">
 <input type="checkbox" class="delete-checkbox" />
 <table>
 <tbody>
 <?php foreach ($entry as $item) : ?>
 <tr class="content">
 <td><?php echo $item['SKU']; ?> </td>
 <td><?php echo $item['Name']; ?> </td>
 <td><?php echo $item['Price']; ?> </td>
 <td><?php echo $item['Size'] ?? $item['Weight'] ?? $item['Dimension']; ?> </td>
 </tr>
 <?php endforeach; ?>
 </tbody>
 </table>
 </div>
 <?php endfor; ?>
 <?php endforeach; ?>
</section>
...
Source Link
lukas.j
  • 244
  • 2
  • 4

Since the example code shows for each of the 3 product results 4 identical entries, the first step would be to replace the 4 div with a for loop.

And because the structure of the 3 product tables are almost identical – they differ only in one key (namely 'Size', 'Weight', and 'Dimension') –, the second step would be to simplify that into another, outer for loop.

...
// after querying the database:
$all = [ $products, $product, $productt ];
...
// replacement for all 12 divs:
<section class="product-list-wrapper">
 <?php foreach ($all as $entry) : ?>
 <?php for ($j = 0; $j < 4; $j++) : ?>
 <div class="div-box">
 <input type="checkbox" class="delete-checkbox" />
 <table>
 <tbody>
 <?php foreach ($entry as $i => $item) : ?>
 <tr class="content">
 <td><?php echo $item['SKU']; ?> </td>
 <td><?php echo $item['Name']; ?> </td>
 <td><?php echo $item['Price']; ?> </td>
 <td><?php echo $item['Size'] ?? $item['Weight'] ?? $item['Dimension']; ?> </td>
 </tr>
 <?php endforeach; ?>
 </tbody>
 </table>
 </div>
 <?php endfor; ?>
 <?php endforeach; ?>
</section>
...
default

AltStyle によって変換されたページ (->オリジナル) /