Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/193186-get-distinct-values-from-an-array/
Share on other sites

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'.

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!

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!

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.