简体   繁体   English

使用php消除选择中的重复项

[英]Eliminate duplicates in select with php

I want to eliminate all the duplicates in a select dropdown list created with PHP. 我想消除使用PHP创建的选择下拉列表中的所有重复项。

My PHP code that creates the dropdown list is the following: 我创建下拉列表的PHP代码如下:

public static function getDropdownlist($conn)
{
    //Initialize dropdown list
    //--------------------------------------------------
    $ddl_query = "select * from MIS_EMPLOYEES";
    $stmt_ddl = oci_parse($conn, $ddl_query);
    oci_execute($stmt_ddl);
    //A default value -- this will be the selected item in the dropdown ##
    $prosopiko = JRequest::getVar('bodies', 0);
    if ($prosopiko == 0)
        $default = 0;
    else  
        $default = $prosopiko;
    //Initialize array to store dropdown options ##

      $options = array();
     // $options = array_unique();
      $options[] = JHTML::_('select.option', '0', 'Επιλέξτε');
      while (($row = oci_fetch_array($stmt_ddl, OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
        $options[] = JHTML::_('select.option', $row['ID'], $row['POSITION']);
      }

    //Create <select name="month" class="inputbox"></select> ##
      $dropdown = JHTML::_('select.genericlist', $options, 'bodies', 'class="inputbox"', 'value', 'text', $default);

      return $dropdown; 
   }

}   

But it brings all the duplicates written from an Oracle table. 但这带来了从Oracle表写入的所有重复项。

How can I eliminate the duplicates? 如何消除重复项? I tried array_unique but I failed. 我尝试了array_unique但失败了。

Easiest option is to modify your query to SELECT DISTINCT ID, POSITION or GROUP BY ID, POSITION . 最简单的选择是将查询修改为SELECT DISTINCT ID, POSITIONGROUP BY ID, POSITION Other than that you'll need to build up an array and use array_unique on that. 除此之外,您还需要构建一个数组并在其上使用array_unique

In your SQL statement, simply change it to gather distinct elements you are interested in. 在您的SQL语句中,只需对其进行更改即可收集您感兴趣的不同元素。

Since you are only using two values in the above code for the value and text, something like this should work: 由于您在上面的代码中仅使用两个值作为值和文本,因此类似的事情应该起作用:

SELECT ID, POSITION
FROM MIS_EMPLOYEES
GROUP BY ID, POSITION

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM