Problem with PHP array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andy1989
Forum Newbie
Posts: 8
Joined: Sat Sep 24, 2011 12:36 pm

Problem with PHP array

Post by andy1989 »

Hey guys,

I want to fetch all tags from my Database and make a "Tags" page (containing all of the tags on the site) with count. something like: "Music (32) Music video (2) guitar (4)" etc..
problem is when I fetch the tags I get a list like this: "music, guitar, videos" "guitar lesson, how to play guitar, jimi hendrix" etc. which means I can only count "music, guitar, videos" as a tag and "guitar lesson, how to play guitar, jimi hendrix" as a tag and not "music" as a tag, "guitar" as a tag etc. separately...

I believe I need to use the "explode" function to split them up, then "array_unique" to remove duplicates and then "count" to count them. Am I right?
this is the code I got:

Code: Select all

 <? 
$result = mysql_query("SELECT keywords FROM content ORDER BY keywords ASC") 
or die(mysql_error()); 
$arr_keywords = array();

while($row = mysql_fetch_array($result)) 
	
		 
       $arr_keywords[$row['keywords']] = $row['keywords'];


$clean = array_unique($arr_keywords);
$count = count($clean);

  $json = json_encode($clean);

 echo $json[$count] ;

?>

All I get is the page full of thousands of tags and they are not separated nor counted

Thanks in advance for the help!
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Problem with PHP array

Post by Eric! »

count() just tells you how many items are in the array. Using the value as an index in your echo $json[$count], surprises me that it even works.

If you use array_unique you're not going to be able to count the number of times "music" appears because duplicates are going to get removed.

I suggest you use preg_split (or perhaps explode) to separate your tags into words with either the space or comma characters. Then you can count each of the duplicates $tag_count['tag']++ each time it occurs. After you've counted them you can just use the $tag array as the final list of tags which will also have the count numbers.
andy1989
Forum Newbie
Posts: 8
Joined: Sat Sep 24, 2011 12:36 pm

Re: Problem with PHP array

Post by andy1989 »

OK thanks man
I'm kind of a noob so I'm not sure I got you right
Can you help me fix my code please?

This is what I got after your help:

Code: Select all

  <? 
$result = mysql_query("SELECT keywords FROM content ORDER BY keywords ASC") 
or die(mysql_error()); 
$arr_keywords = array();

while($row = mysql_fetch_array($result)) 
	
		 
       $arr_keywords[$row['keywords']] = $row['keywords'];


$tag = preg_split(",", $arr_keywords);
$count = count($tag);

  $json = json_encode($arr_keywords);


 echo $json ;

?>

Thanks a lot!
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Problem with PHP array

Post by Eric! »

I left out the json stuff, but this is the basic idea. I tried not to do anything too tricky so you can learn from the concepts. There are many things you can do to build on this. For example you might want to convert your $arr_keywords string to all lower case to avoid a key word like Beer and beer being counted differently.

Code: Select all

$result = mysql_query("SELECT keywords FROM content ORDER BY keywords ASC")
        or die(mysql_error());
$arr_keywords="";
while ($row = mysql_fetch_array($result)) {
    $arr_keywords.= $row['keywords']." "; // make a big string from each post of the keywords
}

$tag = preg_split("/[\s,]+/", $arr_keywords); //make array of your keywords, splitting based on commas or white spaces (returns, space, tab, etc.)
foreach($tag as $keyword)
{
    //loop through the list of all the words and build an array of them counting the number
    //of times they occur so the final array will have a keyword list and a count.
    if(!isset($list[$keyword])) $list[$keyword]=0; // initialize value
    $list[$keyword]++; // increment count for that keyword
}
// remove words you don't care about
if(isset($list["and"])) unset($list["and"]);
if(isset($list["or"])) unset($list["or"]);
if(isset($list["the"])) unset($list["the"]); // etc....

foreach($list as $keyword=>$count){
    echo $keyword." (".$count.")<br>";
}
andy1989
Forum Newbie
Posts: 8
Joined: Sat Sep 24, 2011 12:36 pm

Re: Problem with PHP array

Post by andy1989 »

Ok Great man thanks a lot!
I really filtered it into lower case and and removed dots, also I put the tags in columns and built links.
Your advice was really helpful

I have one last step and I should be done: When I queried my DB I ordered it ASC. Problem is it ordered the tags before they where split using preg_split. Now after splitting them I have to display them Alphabetically
I tried asort($keyword); but no luck

any ideas?

Code: Select all

 <? 
 $column=0;
$result = mysql_query("SELECT keywords FROM content ORDER BY keywords ASC")
        or die(mysql_error());
$arr_keywords="";

while ($row = mysql_fetch_array($result)) {
	
    $arr_keywords.= $row['keywords']." "; // make a big string from each post of the keywords
	$arr_keywords = preg_replace('/\.[^.]+$/','',$arr_keywords);
	$arr_keywords = strtolower($arr_keywords);
	
}

$tag = preg_split("/[\s,]+/", $arr_keywords); //make array of your keywords, splitting based on commas or white spaces (returns, space, tab, etc.)

foreach($tag as $keyword)
{

    //loop through the list of all the words and build an array of them counting the number
    //of times they occur so the final array will have a keyword list and a count.
    if(!isset($list[$keyword])) $list[$keyword]=0; // initialize value
    $list[$keyword]++; // increment count for that keyword
	
}
// remove words you don't care about
if(isset($list["and"])) unset($list["and"]);
if(isset($list["or"])) unset($list["or"]);
if(isset($list["the"])) unset($list["the"]); // etc....


foreach($list as $keyword=>$count){
  
   	$column=$column+1;
	$rest = $column % 620;
	 $link='/search/'.$keyword.'/page1.html'; 

	
	if ($rest == 1) { echo '<div style="width:220px; float:left; margin-left:15px; line-height:17px;">'; } 
	
	asort($keyword);
?>

<a href="<? echo $link; ?>" style="text-decoration:none;"><? echo $keyword. " (".$count.")<br>";  ?>

	<? 	if ($rest == 0) { echo '</div>'; } }?>


Thanks!
andy1989
Forum Newbie
Posts: 8
Joined: Sat Sep 24, 2011 12:36 pm

Re: Problem with PHP array

Post by andy1989 »

Anyone? Please!!
This will help me complete my goal!!

Thanks :)
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Problem with PHP array

Post by Eric! »

Try ksort to sort by the keys in an array. And use it on $list before you start the foreach loop.
Post Reply