Remove the values with array_unique

5 posts by 2 authors in: Forums > CMS Builder
Last Post: April 23, 2009   (RSS)

By Djulia - April 19, 2009 - edited: April 20, 2009

Hi,

I would like to sort a list and remove the values which are identical in a foreach.

On my list.php page I have 5 records.

record 1 => 1200
record 2 => 100
record 3 => 1300
record 4 => 1200
record 5 => 1100


I would like to create a Select and to remove the identical values :

<select name="Price" size="1">
<option value="record 1">1200</option>
<option value="record 2">100</option>
<option value="record 3">1300</option>>
<option value="record 5">1100</option>
</select>


<select name="type" class="dropdown" id="type" >
<option value="">Any</option>
<?php foreach ($mytableRecords as $listing): ?>
<option value="<?php echo $listing['price'] ?>"><?php echo $listing['price'] ?></option>
<?php endforeach; ?>
</select>



I tested :

<?php foreach ($mytableRecords as $listing): ?>
<?php
$tableau = array($listing['name']);
$result = array_unique($tableau);
?>
<?php print_r($tableau); ?><br />
<?php endforeach ?>

...but that does not function.

You have an idea ?

Thanks for your suggestion !

Djulia

Re: [Djulia] Remove the values with array_unique

By ross - April 20, 2009

Hi Djulia

Thanks for posting!

You would need to make several modifications to your code before it would start working so I have a different idea.

First, you'll need to make sure your list is sorted on this particular field. Next, you would try something like:

$lastValue = 0;

<select>

foreach( ... ) {
if ($lastVaue == $listing['currentNumber'] {
<option>$listing['currentNumber']</option>
}
}

</select>


That's the basic idea. Does it make sense? You'll still need to modify that code a bit.

Let me know what you think ;).
-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

Re: [Djulia] Remove the values with array_unique

By ross - April 22, 2009

Hi Djulia

There are going to be lots of ways to do what you need here so I think the main thing is making sure it works properly. If the code you found works the way you want, then it's perfect.

So does that code actually work? Let me know.

Keep me up to date with how you are making out :).
-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

Re: [ross] Remove the values with array_unique

By Djulia - April 23, 2009 - edited: April 23, 2009

Hi Ross,

Thank you for your answer. :)


That functions well.

But, as I am not a programmer, I wonder whether this code will not use much a resource on the server and if it does not present a risk for security of cmsB. ?

For the users of cmsBuilder who could be interested, it is possible to remove a value with unset :

unset($result['0']); // remove index 0
or
unset($result[array_search("xxx", $result)]); // remove "xxx" (a word)

Djulia