Jump to content

[SOLVED] PHP Tag clouds


abch624

Recommended Posts

Hi Guys,

 

I want some help as I am completely stuck...

 

I have two pieces of code one is this

<?php

// connect to database at some point


$con = mysql_connect("localhost","zach","gad");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dbSTA", $con);



// In the SQL below, change these three things:
// thing is the column name that you are making a tag cloud for
// id is the primary key
// my_table is the name of the database table

$query = "SELECT name AS tag, COUNT(id) AS quantity
  FROM clouds
  GROUP BY name
  ORDER BY name ASC";

$result = mysql_query($query);

// here we loop through the results and put them into a simple array:
// $tag['thing1'] = 12;
// $tag['thing2'] = 25;
// etc. so we can use all the nifty array functions
// to calculate the font-size of each tag
while ($row = mysql_fetch_array($result)) {
    $tags[$row['tag']] = $row['quantity'];
print_r($tags);
}

// change these font sizes if you will
$max_size = 250; // max font size in %
$min_size = 100; // min font size in %

// get the largest and smallest array values
$max_qty = max(array_values($tags));
$min_qty = min(array_values($tags));

// find the range of values
$spread = $max_qty - $min_qty;
if (0 == $spread) { // we don't want to divide by zero
    $spread = 1;
}

// determine the font-size increment
// this is the increase per tag quantity (times used)
$step = ($max_size - $min_size)/($spread);

// loop through our tag array
foreach ($tags as $key => $value) {

    // calculate CSS font-size
    // find the $value in excess of $min_qty
    // multiply by the font-size increment ($size)
    // and add the $min_size set above
    $size = $min_size + (($value - $min_qty) * $step);
    // uncomment if you want sizes in whole %:
    // $size = ceil($size);

    // you'll need to put the link destination in place of the #
    // (assuming your tag links to some sort of details page)
    echo '<a href="#" style="font-size: '.$size.'%"';
    // perhaps adjust this title attribute for the things that are tagged
    echo ' title="'.$value.' profile(s) containing the word '.$key.'"';
    echo '>'.$key.'</a> ';
    // notice the space at the end of the link
}

mysql_close($con);

?>

This piece works perfectly fine and returns clouds.

 

Now I have a field in the database that stores a string like this "England|France|Poland|South Africa|Zambia|" Now I want to get this field for all users and then get the number of how may users have England and then diplay this as per that.

 

The code which I have tried to write is this

<?php

// connect to database at some point


$con = mysql_connect("localhost","zach","gad");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dbSTA", $con);



// In the SQL below, change these three things:
// thing is the column name that you are making a tag cloud for
// id is the primary key
// my_table is the name of the database table

$query = "SELECT countries AS tag
  FROM profiledetails";

$result = mysql_query($query);

// here we loop through the results and put them into a simple array:
// $tag['thing1'] = 12;
// $tag['thing2'] = 25;
// etc. so we can use all the nifty array functions
// to calculate the font-size of each tag
while ($row = mysql_fetch_array($result)) {
$country = $row['tag']; $countries = explode('|', $country);
}

// change these font sizes if you will
$max_size = 250; // max font size in %
$min_size = 100; // min font size in %

// get the largest and smallest array values
$max_qty = max(array_values($tags));
$min_qty = min(array_values($tags));

// find the range of values
$spread = $max_qty - $min_qty;
if (0 == $spread) { // we don't want to divide by zero
    $spread = 1;
}

// determine the font-size increment
// this is the increase per tag quantity (times used)
$step = ($max_size - $min_size)/($spread);

// loop through our tag array
foreach ($tags as $key => $value) {

    // calculate CSS font-size
    // find the $value in excess of $min_qty
    // multiply by the font-size increment ($size)
    // and add the $min_size set above
    $size = $min_size + (($value - $min_qty) * $step);
    // uncomment if you want sizes in whole %:
    // $size = ceil($size);

    // you'll need to put the link destination in place of the #
    // (assuming your tag links to some sort of details page)
    echo '<a href="#" style="font-size: '.$size.'%"';
    // perhaps adjust this title attribute for the things that are tagged
    echo ' title="'.$value.' profile(s) containing the word '.$key.'"';
    echo '>'.$key.'</a> ';
    // notice the space at the end of the link
}

mysql_close($con);
?>

but this does not work atm....

 

Please help

 

Thanks - Zahid

Link to comment
https://forums.phpfreaks.com/topic/113390-solved-php-tag-clouds/
Share on other sites

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.