I have this little code which lists all matches of items that contains the key that haz been searched for.
The code I currently have lists all thing no matter how many times it occurs in the array.
for ($i = 0; $i < count($return); $i++) {
$herb = explode('<+>',$return[$i]);
$inf = explode('<+>', $njdbData[$herb[0]]);
echo '<div class="herbListItem" onclick="goToHerb(' . $herb[0] . ')">
<img src="../img/herbs/' . $inf[2] . '-2.jpg" />
<span>'.$inf[0].'<i>'.$herb[1].'</i></span></div>';
}
I’ve made so that all item ids and why the item ids are listen are in an array.
For example, if I search for “Red dragon.”.
- The item with id 1 got listed because I searched for “dragon”.
- The item with id 3 got listed because I searched for “dragon”.
- The item with id 4 got listed because I searched for “China”.
- The item with id 1 got listed again because I searched for “red”.
Because there was 2 hits for item 1 I want item 1 to show up only once and on the top…
In the code herb[0] returns the id of the item and herb[1] returns why the item is in the array.
Can anyone help me making it so it echos out a list where, in the example, instead of echoing this out when searching for “Red dragon”:
- Item 1, Because you searched for Dragon
- Item 3, Because you searched for Dragon
- Item 4, Because you searched for China
- Item 1, Because you searched for red
it echoes out this:
- Item 1, Because you searched for Dragon and red
- Item 3, Because you searched for Dragon
- Item 4, Because you searched for China
I hope you understand what I’m trying to achieve here.
Thanks in advance, enji
Add another array that keeps track of which ones have been echoed out.
Adding multiple reasons why a line was printed doesn’t seem possible with how you are doing this because the $inf variale gets overwrriten each time through the for loop. With some tinkering in your process you’ll be able to print out multiple reasons why a value was returned. But this should eliminate your muultiple responses.
EDIT: Give this a shot. I’m not sure how the array_count_values will act because I don’t know what you have in $return[n]. Code has not been tested.
EDIT#2: I’m an idiot and forgot to sort the array from High to Low based on value. That’s been added.