hamza Posted February 6, 2010 Share Posted February 6, 2010 i have tags field in my table and tags field is optional when tags are addes so most of the time tag field is empty with no tag. and some time there are so many tags in a tag field of database. like in my artical table i have tags field which contain php, frameworks , advance php empty field java jquery how i can fetch all tags to show on user side and ignore empty field. and should be unique tags in the list. my fetch funtion is show as /** getting all tags */ function get_all_tags($db) { //cat array contain catagory names and id $tag = array(); //getting all catagories from database. $result = $db->query("SELECT tags FROM `articals`", $debug = -1); while($row = $db->fetchNextObject($result)) { if ( !empty($row->tags) ) { explode(',',$row->tags); $tag[] = $row->tags; } } //making unique array of print '<pre>'; print_r($tag); $tag = array_unique($tag); print_r($tag); return $tag; } Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 6, 2010 Share Posted February 6, 2010 function get_all_tags($db) { //cat array contain catagory names and id $tag = array(); $unique_tags = array(); //getting all catagories from database. $result = $db->query("SELECT tags FROM `articals`", $debug = -1); while($row = $db->fetchNextObject($result)) { if ( !empty($row->tags) ) { $tag = explode(',',$row->tags); foreach($tag as $checktag) { if(!in_array($checktag,$unique_tags)) { $unique_tags[] = $checktag; } } } } //making unique array of //print '<pre>'; //print_r($tag); //$tag = array_unique($tag); //print_r($tag); return $unique_tags; } Quote Link to comment Share on other sites More sharing options...
hamza Posted February 6, 2010 Author Share Posted February 6, 2010 function get_all_tags($db) { //cat array contain catagory names and id $tag = array(); $unique_tags = array(); //getting all catagories from database. $result = $db->query("SELECT tags FROM `articals`", $debug = -1); while($row = $db->fetchNextObject($result)) { if ( !empty($row->tags) ) { $tag = explode(',',$row->tags); foreach($tag as $checktag) { if(!in_array($checktag,$unique_tags)) { $unique_tags[] = $checktag; } } } } //making unique array of //print '<pre>'; //print_r($tag); //$tag = array_unique($tag); //print_r($tag); return $unique_tags; } any alternative way to fetch tags from database with unique list. this code is not working. Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 6, 2010 Share Posted February 6, 2010 What is not working? Are you getting errors, or just not what you expected? Quote Link to comment Share on other sites More sharing options...
hamza Posted February 6, 2010 Author Share Posted February 6, 2010 What is not working? Are you getting errors, or just not what you expected? no no now its working fine there is a syntax error there now solved but one thing you are missing in there n that is case sensitive. i am getting two php tags liek PHP and php. now i think while insertion i should make it strtolower. Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 6, 2010 Share Posted February 6, 2010 If you want the search to be case insenstiive then yes, you would use strtolower on the check and the insert if(!in_array(strtolower($checktag),$unique_tags)) { $unique_tags[] = strtolower($checktag); } Quote Link to comment Share on other sites More sharing options...
hamza Posted February 6, 2010 Author Share Posted February 6, 2010 If you want the search to be case insenstiive then yes, you would use strtolower on the check and the insert if(!in_array(strtolower($checktag),$unique_tags)) { $unique_tags[] = strtolower($checktag); } thanks for the help. Quote Link to comment 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.