jarvis Posted February 24, 2010 Share Posted February 24, 2010 Hi All, Sorry I'm struggling with trying to get distinct values from an array. I have the following code which pulls a list of clients from a news table: list($client_newsRecords, $client_newsMetaData) = getRecords(array( 'tableName' => 'news', 'orderBy' => 'client ASC', )); I then use the list to create a drop down: <select name="fieldname" size="1" onChange="openDir( this.form )"> <option selected="selected">-- Please Select --</option> <?php foreach ($client_newsRecords as $record): ?> <option value="downloads.php?client=<?php echo urlencode($record['client']); ?>" selected><?php echo $record['client']; ?></option> <?php endforeach ?> </select> The problem is, if several news articles are added with the same client, the drop down list shows loads of the same client in the list. Hence the reason for the removal of any duplicates. Any help is much appreciated Thanks again in advanced Quote Link to comment https://forums.phpfreaks.com/topic/193186-get-distinct-values-from-an-array/ Share on other sites More sharing options...
danielgrieve Posted February 24, 2010 Share Posted February 24, 2010 Try using 'GROUP BY' in your MySQL statement, which I'm guessing will be something like so: list($client_newsRecords, $client_newsMetaData) = getRecords(array( 'tableName' => 'news', 'orderBy' => 'client ASC', 'groupBy' => 'client' )); Hopefully your getRecords() function is set up to handle 'groupBy' as the actual MySQL syntax is 'GROUP BY'. Quote Link to comment https://forums.phpfreaks.com/topic/193186-get-distinct-values-from-an-array/#findComment-1017292 Share on other sites More sharing options...
jarvis Posted February 24, 2010 Author Share Posted February 24, 2010 Thanks for the reply, unfortunately, that doesn't work. I get the error: Unknown option 'groupBy' specified. Valid option names are: (tableName, where, orWhere, orderBy, limit, offset, perPage, loadUploads, allowSearch, requireSearchMatch, loadCreatedBy, useSeoUrls, loadListDetails, joinTable, debugSql, leftJoin, useCache, pageNum, ignoreHidden, ignorePublishDate, ignoreRemoveDate, requireSearchSuffix, includeDisabledAccounts, addSelectExpr) Hope that helps? Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/193186-get-distinct-values-from-an-array/#findComment-1017293 Share on other sites More sharing options...
PravinS Posted February 24, 2010 Share Posted February 24, 2010 May array_unique() will help you. Quote Link to comment https://forums.phpfreaks.com/topic/193186-get-distinct-values-from-an-array/#findComment-1017295 Share on other sites More sharing options...
jarvis Posted February 24, 2010 Author Share Posted February 24, 2010 Thanks again, I'd tried that: list($client_newsRecords, $client_newsMetaData) = getRecords(array_unique( But got the following error: Parse error: syntax error, unexpected T_DOUBLE_ARROW in Or have I not used it in the correct way? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/193186-get-distinct-values-from-an-array/#findComment-1017296 Share on other sites More sharing options...
trq Posted February 24, 2010 Share Posted February 24, 2010 Your likely missing a ) Quote Link to comment https://forums.phpfreaks.com/topic/193186-get-distinct-values-from-an-array/#findComment-1017311 Share on other sites More sharing options...
jarvis Posted February 24, 2010 Author Share Posted February 24, 2010 Thanks thorpe, where abouts? list($client_newsRecords, $client_newsMetaData) = getRecords(array_unique( 'tableName' => 'news', 'orderBy' => 'client ASC', )); All brackets close correctly? Quote Link to comment https://forums.phpfreaks.com/topic/193186-get-distinct-values-from-an-array/#findComment-1017314 Share on other sites More sharing options...
PravinS Posted February 24, 2010 Share Posted February 24, 2010 Try this list($client_newsRecords, $client_newsMetaData) = getRecords(array_unique(array( 'tableName' => 'news', 'orderBy' => 'client ASC', ))); Quote Link to comment https://forums.phpfreaks.com/topic/193186-get-distinct-values-from-an-array/#findComment-1017316 Share on other sites More sharing options...
jarvis Posted February 24, 2010 Author Share Posted February 24, 2010 Thanks pbs, that seems to shift the error, however, the list still contains duplicates!? Why are the simple things always the most annoying! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/193186-get-distinct-values-from-an-array/#findComment-1017317 Share on other sites More sharing options...
trq Posted February 24, 2010 Share Posted February 24, 2010 I fear your applying array_unique to the wrong array. What exactly does getRecords() do and how does it work? You really need to pass it a GROUP BY clause but considering it is not standard php, we have no way of letting you know how to do that. Quote Link to comment https://forums.phpfreaks.com/topic/193186-get-distinct-values-from-an-array/#findComment-1017318 Share on other sites More sharing options...
jarvis Posted February 24, 2010 Author Share Posted February 24, 2010 Thanks thorpe. I would assume getrecords() retrieves the details from the db table, you then pass them into the code and reference each element. This code is from a CMS, not something I've done myself. However, I'm trying to modify a particular page hence this post. The GROUPBY didn't work as I tried that too. Apologies if this doesn't answer your questions or is a little vague! Quote Link to comment https://forums.phpfreaks.com/topic/193186-get-distinct-values-from-an-array/#findComment-1017323 Share on other sites More sharing options...
PravinS Posted February 24, 2010 Share Posted February 24, 2010 Yes it will be better if you fetch correct data from database using query as mentioned by thorpe. Quote Link to comment https://forums.phpfreaks.com/topic/193186-get-distinct-values-from-an-array/#findComment-1017326 Share on other sites More sharing options...
jarvis Posted February 24, 2010 Author Share Posted February 24, 2010 Thanks all for your replies. Unfortunately, I'm not able to use the GROUPBY option. What if I therefore go through all the elements in the array and count them? Then remove if the number is greater than 1? Something like: <?php $record_count_arr = array_count_values($client_newsRecords); ?> <?php foreach($client_newsRecords as $record => $client) { if($record_count_arr[$client] > 1) { unset($client_newsRecords[$record]); } } ?> Although that gives an error: Warning: array_count_values(): Can only count STRING and INTEGER values! in ....php So may need some work! Quote Link to comment https://forums.phpfreaks.com/topic/193186-get-distinct-values-from-an-array/#findComment-1017329 Share on other sites More sharing options...
trq Posted February 24, 2010 Share Posted February 24, 2010 Its 'GROUP BY' not 'GROUPBY' and you will need to find out how the getRecords() function works. Is there any documentation for this CMS? Otherwise, locate the function and take a look at it yourself. Quote Link to comment https://forums.phpfreaks.com/topic/193186-get-distinct-values-from-an-array/#findComment-1017345 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.