H
H
hello world2015-10-25 16:01:57
PHP
hello world, 2015-10-25 16:01:57

How to display records without repetition?

Good day to all! There is a question:
There is a request of the following form

$stmt = $pdo->query('SELECT price FROM middle_price');
  $price = $stmt->fetchAll();
  return $price;

As you can see, we select all prices from the table. How to make the cost up to 250 rubles shown as "up to 250 rubles", and not "100, 150, 200, etc.), and the cost above 250 and up to 500 rubles as "250 rubles - 500 rubles" instead of listing prices from the columns ( 430, 270, etc.)
Wrote in the output document like this
<span class="filter_name"><?php 
    if ($price['price'] <= 250) { 
       echo 'до 250'; 
   } else if ($price['price'] >= 250) { 
     echo '250-500';  
   }
?></span>

and displays it like this - https://i.imgur.com/Y5lzWXf.jpg Let me remind you once again that there should be only 2 entries - up to 250, 250-500

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Aleksey Ratnikov, 2015-10-25
@hello

SELECT
  CASE
    when price < 250 then 'до 250'
    when price  between 250 and 500 then 'от 250 до 500'
  ELSE price	
  END as price
FROM middle_price

S
Schoolboy., 2015-10-25
@viphorizon

else if in my opinion should be merged, in your case you don’t need to use elseif at all

<span class="filter_name"><?php 
    if ($price['price'] <= 250) { 
       echo 'до 250'; 
   } 
if ($price['price'] >= 250 and $price['price'] <= 500) { 
     echo '250-500';  
   }
?></span>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question