Re: object/array with repeating values

From: Date: Wed, 21 Feb 2001 01:26:39 +0000
Subject: Re: object/array with repeating values
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-40869@lists.php.net to get a copy of this message
From: "Jared Howard" <webbie@deanox.com> > I'm make an object or an array from the results of a query to my MySQL > database. The value returned is entered in <option>s and any repeated > values are unwanted. Is there a way to remove them easily? > > eg. > <select> > <? $qid = mysql_query("SELECT this FROM that WHERE here"); > while ($r = mysql_fetch_object($qid)) { ?> > <option><?=$r->this?></option> > <? } ?> > </select> > > I was thinking of creating an array before the while, then in the while add > each "$r->this" if "(! in_array($arr))". Then I would have to do > another > while with the <option> included. But I'm not really sure how to add > elements to existing arrays. And that might not even be the best way. Any > iteas? > IMHO: always make the database manipulate the data for you, if it can. It's usually much faster, and requires less code. In this case just do: SELECT DISTINCT this FROM that WHERE here and you won't get any duplicates. Great huh? :) Refer to the MySQL manual at http://www.mysql.com/docs/ for more details. Cheers Simon Garner

« previous php.general (#40869) next »