How to make the price sorting start from 1 and not from 0 when outputting from a database

$sorting = $_GET["sort"]; switch ($sorting) { case 'price-asc' ; $sorting = 'targeting ASC'; $sort_name = 'Дешевле'; break; case 'price-desc'; $sorting = 'targeting DESC'; $sort_name = 'Дороже'; break; case 'darom'; $sorting = 'darom DESC'; $sort_name = 'Даром'; break; case 'obmen'; $sorting = 'obmen DESC'; $sort_name = 'Обмен'; break; case 'new'; $sorting = 'datatime DESC'; $sort_name = 'Новинки'; break; default: $sorting = 'products_id DESC'; $sort_name = 'Нет сортировки!'; break; } 

 <?php $result = mysql_query("SELECT * FROM table_products ORDER BY $sorting",$connect); if (mysql_num_rows($result)>0) { $row = mysql_fetch_array($result); do{ if ($row["imagesad"] != "" && file_exists("ph_main/".$row["imagesad"])) { $img_path = 'ph_main/'.$row["imagesad"]; $max_width = 114; $max_height = 114; list($width, $height) = getimagesize($img_path); $ratioh = $max_height/$height; $ratiow = $max_width/$width; $ratio = min($ratioh, $ratiow); $width = intval($ratio*$width); $height = intval($ratio*$height); }else { $img_path = "images/no-image.png"; $width = 110; $height = 200; } echo ' <li> <div class="block-images-grid"> <img src="'.$img_path.'" width="'.$width.'" height="'.$height.'"> </div> <p class="style-title-grid"><a href="">'.$row["title"].'</a></p> <ul class="reviews-and-counts-grid"> <li><img src="images/eye-icon.png"><p>0</p></li> <li><img src="images/comment-icon.png"><p>0</p></li> </ul> <p class="style-price-grid"><strong>'.(!empty($row["darom"]) ? $row["darom"] : (!empty($row["obmen"]) ? $row["obmen"] : $row["targeting"])).'</strong></p> <div class="mini-features"> <p>Город:'.$row["gorod"].'</p> <p id="datatime">Дата:'.$row["datatime"].'</p> </div> </li> '; } while ($row = mysql_fetch_array($result)); } ?> 

  • 2
    And records with zeros where in the sample should be - at the end? Or maybe there shouldn't be any of them at all and it was worth filtering into where price>0 - Mike
  • ... and are these products really free, or are their prices just not there? Because in the latter case NULL should be there ... .-. - D-side
  • they just don’t have the price, and the value 0 is entered in the database, so I need the sorting to not affect the product with a value of 0 - Eugene
  • Tell someone how to do that? - Eugene

2 answers 2

It is not entirely clear what result you want to get. If you want the zeros to be at the end of the sample, you can use

 SELECT * FROM table ORDER BY targeting = 0; 

If you want the sample to have no data at all with a price of 0, then you must set the condition WHERE

 SELECT * FROM table WHERE targering > 0 ORDER BY targering ASC; 
  • I've found something similar in the internet too, I need the second option. Could you tell me where to write it? I apologize for such a question, I just recently began to study it myself) - Eugene
  • Find the original request or the moment of its formation in the code and substitute there in a condition (or add a condition if it is not there) targering> 0. I even having supernormal abilities cannot tell where to insert it in your code without the presence of code :) - Firepro
  • ("SELECT * FROM table_products WHERE targeting> 0 ORDER BY $ sorting", $ connect) here's the code that worked, but one thing appeared. Sort obmen and darom, how can I fix this guys? - Eugene

Alternatively, you can use conditional sorting, first letting values ​​greater than 0, and then those that take the value 0

 SELECT * FROM tbl ORDER BY IF(targering > 0, 0, 1), targering;