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; } Link to comment https://forums.phpfreaks.com/topic/191195-tags-fetching/ 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; } Link to comment https://forums.phpfreaks.com/topic/191195-tags-fetching/#findComment-1008083 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. Link to comment https://forums.phpfreaks.com/topic/191195-tags-fetching/#findComment-1008094 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? Link to comment https://forums.phpfreaks.com/topic/191195-tags-fetching/#findComment-1008095 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. Link to comment https://forums.phpfreaks.com/topic/191195-tags-fetching/#findComment-1008100 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); } Link to comment https://forums.phpfreaks.com/topic/191195-tags-fetching/#findComment-1008102 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. Link to comment https://forums.phpfreaks.com/topic/191195-tags-fetching/#findComment-1008103 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.