Good day.

I want to sort the news by date and name, but it does not work, or rather if you put ORDER BY $order_db in the query, then the output of the news stops altogether, but stops because of $order_db , an error occurs. Although this sorting is for another, everything works there. Here is the code:

  /* =====Π‘ΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²ΠΊΠ°===== */ $order_p = array( 'datea' => array('ΠΏΠΎ Π΄Π°Ρ‚Π΅ добавлСния - ΠΊ послСдним', 'date ASC'), 'dated' => array('ΠΏΠΎ Π΄Π°Ρ‚Π΅ добавлСния - с послСдних', 'date DESC'), 'titlea' => array('ΠΎΡ‚ А Π΄ΠΎ Π―', 'title ASC'), 'titled' => array('ΠΎΡ‚ Π― Π΄ΠΎ А', 'title DESC') ); $order_get = clear($_GET['order']); // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Ρ‹ΠΉ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ сортировки if(array_key_exists($order_get, $order_p)){ $order = $order_p[$order_get][0]; $order_db = $order_p[$order_get][1]; }else{ // ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ сортировка ΠΏΠΎ ΠΏΠ΅Ρ€Π²ΠΎΠΌΡƒ элСмСнту массива order_p $order = $order_p['datea'][0]; $order_db = $order_p['datea'][1]; } /* =====Π‘ΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²ΠΊΠ°===== */ $all_news = news($order_db); 

 function news($order_db){ global $connection; $query = "SELECT news_id, title, anons, date FROM news ORDER BY $order_db DESC LIMIT $start_pos, $perpage"; $res = mysqli_query($connection, $query); $all_news = array(); while($row = mysqli_fetch_assoc($res)){ $all_news[] = $row; } return $all_news; } 

Well, the sorting output itself:

 Π‘ΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ:&nbsp; <ul id="menuuu"> <li><?=$order?> <ul> <?php foreach($order_p as $key => $value): ?> <?php if($value[0] == $order) continue; ?> <li><a href="ссылку ΠΏΠΎΡ‚ΠΎΠΌ ΠΏΡ€ΠΎΠΏΠΈΡˆΡƒ" class="sort-bot"><?=$value[0]?></a></li> <?php endforeach; ?> </ul> </li> </ul> 

Who can find a mistake, tell me, please. )

    1 answer 1

    Of course it will not work, because you are in the ASC array

     'datea' => array('ΠΏΠΎ Π΄Π°Ρ‚Π΅ добавлСния - ΠΊ послСдним', 'date ASC') 

    and right there in the DESC request

     ORDER BY $order_db DESC 

    And the query also has a date , this is a reserved function in mysql, it needs to be wrapped in quotes:

     `date` 
    • Yeah stupid mistake, thank you very much) - Alextrue