Jump to content

[SOLVED] Unique foreach result


l!m!t

Recommended Posts

 

I have a function to build a generic tag cloud..my source pulls from a database where the values are separated by commas.  I then have a function to explode each commas into a link, which works fine.

 

Is there anyway to get it so my function only returns unique values?.. Right now its returning duplicates .. I tried implementing array_unique() , but had no luck.. I am still learning.  I cant do this from the database as each result is separated by a command as opposed to separate table.

 

 

function explodeTagCloud($tags) {
$build_tags=$tags;
$tags_result = explode(",",$build_tags);
foreach($tags_result as $key => $value){
echo '  <a rel="'.rand(1, 10).'" href="'.tep_href_link(VIDEO_SEARCH.'?tag='.$value.'','').'">' .$value. '</a>  ';
  }
}

Link to comment
Share on other sites

Hi haku,

 

Thanks for the reply.. I added $tags_result = array_unique($tags_result); to the function, but its still bringing up duplicates.

 

This function goes into a mySQL while loop (eg ) ..

while($tag_result = db_fetch_array($tag_query)){ 
echo explodeTagCloud($tag_result['my_tag']);

 

Any suggestions would be great.

 

Link to comment
Share on other sites

You are going to have to help us by providing information as to why array_unique() did not work. Are there spaces before or after some of the entries in the list? Is the capitalization different?

 

I checked for spaces typos etc..I too thought array_unique would work.  I think the problem is that the function explodes all commas, but only for that individual mySQL result. When its being looped from each row, there is no way for it know all duplicates as a total. So rows that have duplicate names are being ignored. I think I have to somehow pull all the sql results into a giant foreach and then use array unique to sort those results..

 

I have no clue how to do this, but will try to explain. any thoughts on this would be great.

 

Something like this -

while($tag_result = db_fetch_array($tag_query)){    
$result.=array($tag_result['my_tag']);
}
foreach($result as $key => $value){
 echo explodeTagCloud($value);
}

Link to comment
Share on other sites

I don't suppose we could talk you into using a separate row for each tag? That would solve this problem (because the database could enforce distinct values or you could query for just the distinct values) and all the other problems you had in getting the data into and out of the database.

 

If the duplicates are in different rows in the result set, the easiest way to remove the duplicates is to make a single array that holds all the values. This has to be done at the point where all the rows of data are available, either in the while() loop or you would need make an array of all the rows and pass it to the function.

 

If you want or need to involve your function is up to you, but the following minimal logic accomplishes what you are trying to do (untested) -

 

$tags = array(); // define empty array
while($tag_result = db_fetch_array($tag_query)){
$tags = array_merge($tags,explode(",",$tag_result['my_tag']));
}
$tags = array_unique($tags); // remove duplicates
foreach($tags as $value){
echo '  <a rel="'.rand(1, 10).'" href="'.tep_href_link(VIDEO_SEARCH.'?tag='.$value.'','').'">' .$value. '</a>  ';
}

Link to comment
Share on other sites

I don't suppose we could talk you into using a separate row for each tag? That would solve this problem (because the database could enforce distinct values or you could query for just the distinct values) and all the other problems you had in getting the data into and out of the database.

 

If the duplicates are in different rows in the result set, the easiest way to remove the duplicates is to make a single array that holds all the values. This has to be done at the point where all the rows of data are available, either in the while() loop or you would need make an array of all the rows and pass it to the function.

 

If you want or need to involve your function is up to you, but the following minimal logic accomplishes what you are trying to do (untested) -

 

$tags = array(); // define empty array
while($tag_result = db_fetch_array($tag_query)){
$tags = array_merge($tags,explode(",",$tag_result['my_tag']));
}
$tags = array_unique($tags); // remove duplicates
foreach($tags as $value){
echo '  <a rel="'.rand(1, 10).'" href="'.tep_href_link(VIDEO_SEARCH.'?tag='.$value.'','').'">' .$value. '</a>  ';
}

 

 

Cool this worked! .. Yeah I would have used mySQL distinct, but it would result in multiple rows for each keyword, multiple inputs etc.

 

Thanks a ton for your time and for the help!

Link to comment
Share on other sites

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.