Jump to content

tags fetching


hamza

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
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.