Jump to content

Explode array (Tag cloud)


GamerGun

Recommended Posts

Hello y'all

 

I'm having a problem which I'm not able to fix, so that's why I'm asking for your help. I have a database, which contains a table "games".

 

In games, there is a field called "tagging". Those records (from "tagging") are being used for creating a tag cloud.

 

The problem is that every record contains multiple words. For example:

 

3 foot, ninja, kickass, fight, sword, blood

 

They are all comma separated. Now i use the following code:

 

<?php
$tags = Array();

$tagquery = mysql_query("SELECT COUNT(tagging) AS count, tagging
    FROM games
    GROUP BY tagging
    ORDER BY tagging
") or die( mysql_error() );

while( $tagging = mysql_fetch_assoc( $tagquery ) )
{
    $temp = explode(",",$tagging['tagging']);
    $tags[$tagging['tagging']] = $tagging['count'];
}

$max = max($tags);

foreach ($tags as $tagging => $hits)
{
    $percent = round(($hits / $max) * 100);
    
    echo "<a href='link.com/tag/".$tagging."' style='font-size: ".$percent."%'>".$tagging."</a> ";
} 
?>

 

This creates an cloud based on all the records, but these should be exploded. If i print the array, it shows me this:

 

echo "<pre>";
print_r ($tags);
echo "</pre>";

 

Array

(

    [* math, balls, lines, jet, game, games, flash, fun, awesome, win, fupa, puzzle, puzzles] => 1

    [* word, text, puzzle, action, small, casual, family, friendly, dictionary, fast, quick, time] => 1

    [3 foot, ninja, kickass, fight, sword, blood] => 1

    [3d, ball, drop, plate] => 1

)

 

But what i want is every word separate. When i have that, i can use each word for the foreach to create the cloud.

 

So to make a long story short: the cloud is now based on each record, instead of every word from each record.

 

Hope that I'm clear.

 

Thanks for any help.

 

Pz -T

 

Link to comment
https://forums.phpfreaks.com/topic/146856-explode-array-tag-cloud/
Share on other sites

It is doing exactly what you told it to do.

 

while( $tagging = mysql_fetch_assoc( $tagquery ) )
{
    $temp = explode(",",$tagging['tagging']);
    $tags[$tagging['tagging']] = $temp;
}

 

 

You explode it, then do nothing with the exploded value. And why are you using the original tagging data as an index, I would use the game name for the index, but that is just me.

It is doing exactly what you told it to do.

 

while( $tagging = mysql_fetch_assoc( $tagquery ) )
{
    $temp = explode(",",$tagging['tagging']);
    $tags[$tagging['tagging']] = $temp;
}

 

You explode it, then do nothing with the exploded value. And why are you using the original tagging data as an index, I would use the game name for the index, but that is just me.

 

You were right, but with using this, the count part is gone?

 

try

while( $tagging = mysql_fetch_assoc( $tagquery ) )
{
    str_replace(',', ',', $tagging['tagging'], $tmp);
    $tags[$tagging['tagging']] = $tmp+1;
}

 

I don't get what this is doing, seems like it's creating more lines, but still no separate words.

 

Try exploding twice, once for the commas then again for the spaces

 

while( $tagging = mysql_fetch_assoc( $tagquery ) )
{
    $temp = explode(",",$tagging['tagging']);
    $temp2 = explode(" ",$temp);
    $tags[$tagging['tagging']] = $tagging['count'];
}

 

I see what you mean, and this i think is the most logical, but i don't know how to use the data from $temp2, without loosing the $tagging['count'];

 

Because now, it's again doing nothing with that data.

 

Thanks all.

 

Pz -T

Testing a bit, while waiting for an solution.

 

With this code;

 

$tags = Array();

$tagquery = mysql_query("SELECT COUNT(tagging) AS count, tagging
    FROM games
    GROUP BY tagging
    ORDER BY tagging
") or die( mysql_error() );

while( $tagging = mysql_fetch_assoc( $tagquery ) )
{
    $temp = explode(",",$tagging['tagging']);
    $temp2 = explode(" ",$temp);
    $tags[$tagging['tagging']] = $tagging['count'];
}

print_r ($temp);

 

I get this:

 

Array ( [0] => zombies [1] => shooting [2] => ninja kiwi [3] => assault [4] => guns [5] => rifle [6] => shooting [7] => creeps [8] => walls )

 

With this;

 

$tags = Array();

$tagquery = mysql_query("SELECT COUNT(tagging) AS count, tagging
    FROM games
    GROUP BY tagging
    ORDER BY tagging
") or die( mysql_error() );

while( $tagging = mysql_fetch_assoc( $tagquery ) )
{
    $temp = explode(",",$tagging['tagging']);
    $temp2 = explode(" ",$temp);
    $tags[$tagging['tagging']] = $tagging['count'];
}

print_r ($temp2);

 

It outputs Array ( [0] => Array )

 

1. Why is temp2 empty?

2. Why is temp only outputting one line?

3. Most important, how to combine the while part with this:

 

//$max = max($tags);

foreach ($tags as $tagging => $hits)
{
    $percent = round(($hits / $max) * 100);  
    echo "<a href='link.com/tag/".$tagging."' style='font-size: ".$percent."%'>".$tagging."</a> ";
} 

 

Thanks. Pz -T

hey dude, got your PM

 

just realized we are dumb. $tagging['tagging'] is an array, so we shouldn't be exploding it to convert to a string.

 

Try this, going to convert to a string, then do an str_replace, then put it back into an array u can use on the cloud

 

$tags = Array();

$tagquery = mysql_query("SELECT COUNT(tagging) AS count, tagging
    FROM games
    GROUP BY tagging
    ORDER BY tagging
") or die( mysql_error() );

while( $tagging = mysql_fetch_assoc( $tagquery ) )
{
      $temp = implode(",",$tagging['tagging']);
      $temp2 = str_replace(" ",",",$temp);
      $final_temp = explode(",",$temp2);
    //$temp = explode(",",$tagging['tagging']);
    //$temp2 = explode(" ",$temp);
    //$tags[$tagging['tagging']] = $tagging['count'];
}

print_r ($temp2);

 

ok

 

Hello mate,

 

Thanks for your response.

 

Now i have:

 

$tags = Array();

$tagquery = mysql_query("SELECT COUNT(tagging) AS count, tagging
    FROM games
    GROUP BY tagging
    ORDER BY tagging
") or die( mysql_error() );

while( $tagging = mysql_fetch_assoc( $tagquery ) )
{
      $temp = implode(",",$tagging['tagging']);
      $temp2 = str_replace(" ",",",$temp);
      $final_temp = explode(",",$temp2);
}

print_r ($final_temp);

//$max = max($tags);

//foreach ($tags as $tagging => $hits)
//{
//    $percent = round(($hits / $max) * 100);    
//    echo "<a href='link.com/tag/".$tagging."' style='font-size: ".$percent."%'>".$tagging."</a> ";
//} 

 

Which gives (a lot):

 

Warning: implode() [function.implode]: Invalid arguments passed in index.php on line 162

 

I think the reason is because of the [ ] inside the implode function. I don't know a way around it tho.

 

I tried this but that doesn't work of course hehe.

 

      $bla = $tagging['tagging'];
      $temp = implode(",",$bla);

 

Thanks! Pz -T

No output, except for the same error message.

 

Tried to create output like this:

 

$tags = Array();

$tagquery = mysql_query("SELECT COUNT(tagging) AS count, tagging
    FROM games
    GROUP BY tagging
    ORDER BY tagging
") or die( mysql_error() );

while( $tagging = mysql_fetch_assoc( $tagquery ) )
{

$tags[$tagging['tagging']] = $tagging['count'];

      //$temp = implode(",",$tagging['tagging']);
      //$temp2 = str_replace(" ",",",$temp);
      //$final_temp = explode(",",$temp2);
}

print_r($tagging['tagging']);

 

But then it's all empty, no output.

You mean like this?

 

//tagcloud proberen

$tags = Array();

$tagquery = mysql_query("SELECT COUNT(tagging) AS count, tagging
    FROM games
    GROUP BY tagging
    ORDER BY tagging
") or die( mysql_error() );

//while( $tagging = mysql_fetch_assoc( $tagquery ) )
{

    //$tags[$tagging['tagging']] = $tagging['count'];

      $temp = implode(",",$tagging['tagging']);
      $temp2 = str_replace(" ",",",$temp);
      $final_temp = explode(",",$temp2);
}

print_r($tagging['tagging']);

//$max = max($tags);

//foreach ($tags as $tagging => $hits)
//{
//    $percent = round(($hits / $max) * 100);    
//    echo "<a href='link.com/tag/".$tagging."' style='font-size: ".$percent."%'>".$tagging."</a> ";
//} 

//einde tagcloud proberen

 

Gives:

 

Warning: implode() [function.implode]: Invalid arguments passed in /home/mqxfaokl/domains/domain.nl/public_html/index.php on line 165

 

Just once.

 

Thx

//tagcloud proberen

$tags = Array();

$tagquery = mysql_query("SELECT COUNT(tagging) AS count, tagging
    FROM games
    GROUP BY tagging
    ORDER BY tagging
") or die( mysql_error() );

/*
//while( $tagging = mysql_fetch_assoc( $tagquery ) )
{

    //$tags[$tagging['tagging']] = $tagging['count'];

      $temp = implode(",",$tagging['tagging']);
      $temp2 = str_replace(" ",",",$temp);
      $final_temp = explode(",",$temp2);
}
*/
$tagging = mysql_fetch_assoc( $tagquery )
print_r($tagging['tagging']);

//$max = max($tags);

//foreach ($tags as $tagging => $hits)
//{
//    $percent = round(($hits / $max) * 100);    
//    echo "<a href='link.com/tag/".$tagging."' style='font-size: ".$percent."%'>".$tagging."</a> ";
//} 

//einde tagcloud proberen

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

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