Помогите подключить БД к TagCould
Метки: [без меток]
Вот вообщем есть код который работает с тэгами.
Он их выводит из массива как я понял с уже заданными тэгами.
<?
/*
@wordCloud
Author: Derek Harvey
Website: www.lotsofcode.com
@Description
PHP Tag Cloud Class, a nice and simple way to create a php tag cloud, a database and non-database solution.
*/
class wordCloud
{
var $wordsArray = array();
/*
* PHP 5 Constructor
*
* @param array $words
* @return void
*/
function __construct($words = false)
{
if ($words !== false && is_array($words))
{
foreach ($words as $key => $value)
{
$this->addWord($value);
}
}
}
/*
* PHP 4 Constructor
*
* @param array $words
* @return void
*/
function wordCloud($words = false)
{
$this->__construct($words);
}
/*
* Assign word to array
*
* @param string $word
* @return string
*/
function addWord($word, $value = 1)
{
$word = strtolower($word);
if (array_key_exists($word, $this->wordsArray))
$this->wordsArray[$word] += $value;
else
$this->wordsArray[$word] = $value;
return $this->wordsArray[$word];
}
/*
* Shuffle associated names in array
*/
function shuffleCloud()
{
$keys = array_keys($this->wordsArray);
shuffle($keys);
if (count($keys) && is_array($keys))
{
$tmpArray = $this->wordsArray;
$this->wordsArray = array();
foreach ($keys as $key => $value)
$this->wordsArray[$value] = $tmpArray[$value];
}
}
/*
* Calculate size of words array
*/
function getCloudSize()
{
return array_sum($this->wordsArray);
}
/*
* Get the class range using a percentage
*
* @returns int $class
*/
function getClassFromPercent($percent)
{
if ($percent >= 99)
$class = 1;
else if ($percent >= 70)
$class = 2;
else if ($percent >= 60)
$class = 3;
else if ($percent >= 50)
$class = 4;
else if ($percent >= 40)
$class = 5;
else if ($percent >= 30)
$class = 6;
else if ($percent >= 20)
$class = 7;
else if ($percent >= 10)
$class = 8;
else if ($percent >= 5)
$class = 9;
else
$class = 0;
return $class;
}
/*
* Create the HTML code for each word and apply font size.
*
* @returns string $spans
*/
function showCloud($returnType = "html")
{
$this->shuffleCloud();
$this->max = max($this->wordsArray);
if (is_array($this->wordsArray))
{
$return = ($returnType == "html" ? "" : ($returnType == "array" ? array() : ""));
foreach ($this->wordsArray as $word => $popularity)
{
$sizeRange = $this->getClassFromPercent(($popularity / $this->max) * 100);
if ($returnType == "array")
{
$return[$word]['word'] = $word;
$return[$word]['sizeRange'] = $sizeRange;
if ($currentColour)
$return[$word]['randomColour'] = $currentColour;
}
else if ($returnType == "html")
{
$return .= "<span class='word size{$sizeRange}'> {$word} </span>";
}
}
return $return;
}
}
}
?>
<style>
<!--
.word {
font-family: Tahoma;
padding: 4px 4px 4px 4px;
letter-spacing: 3px;
}
span.size1 {
color: #000;
font-size: 2.4em;
}
span.size2 {
color: #333;
font-size:2.2em;
}
span.size3 {
color: #666;
font-size: 2.0em;
}
span.size4 {
color: #999;
font-size: 1.0em;
}
span.size5 {
color: #aaa;
font-size: 1.6em;
}
span.size6 {
color: #bbb;
font-size: 1.4em;
}
span.size7 {
color: #ccc;
font-size: 1.2em;
}
span.size8 {
color: #ddd;
font-size: .8em;
}
span.size0 {
color: #ccc;
font-size: .6em;
}
//-->
</style>
<?
$randomWords = array(
"webmasterworld", "Computer", "Skateboarding", "PC", "music", "music", "music", "music", "PHP", "C", "XHTML", "eminem", "programming", "forums", "webmasterworld",
"Chill out", "email", "forums", "Computer", "GTA", "css", "mysql", "sql", "css", "mysql", "sql",
"forums", "internet", "class", "object", "method", "music", "music", "music", "music", "gui", "encryption"
);
$cloud = new wordCloud($randomWords);
$cloud->addWord("music", 12);
$cloud->addWord("downloads", 8);
$cloud->addWord("internet", 17);
$cloud->addWord("PHP", 22);
$cloud->addWord("CSS", 32);
echo $cloud->showCloud();
?>
Вообщем в документации написанно что если вы хотите работать с ДБ то вот код подколючение.
<?php
$cloud = new wordcloud();
$getBooks = mysql_query("SELECT title FROM `tags`");
if ($getBooks)
{
while ($rowBooks = mysql_fetch_assoc($getBooks))
{
$cloud->addWord($rowBooks['title']);
}
}
}
$myCloud = $cloud->showCloud('array');
if (is_array($myCloud))
{
foreach ($myCloud as $key => $value)
{
echo ' <a href="tags_url/'.urlencode($value['word']).'" style="font-size: 1.'.($value['range']).'em">'.$value['word'].'</a> ';
}
?>
Но проблема в том как его подключить ? Что убрать и куда его вставить ?
Вот я создал таблицы с тегами и подколючаюсь так
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'mydb';
// This is an example opendb.php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
Где моя ошибка ? почему у меня не выводяться теги ? из таблицы ?
Вот сам урок - http://www.lotsofcode.com/php/tag-cloud.htm
Вот вам пример облака:
$tags_growth_range = 5;
// функция возвращает просто массив значений выборки
$tags_prepare = sql2array("SELECT SQL_CACHE tags.name,count(*) as cnt
FROM tags
GROUP BY tags.name ORDER BY tags.name");
$maxv=0;
for($i=0;$i < count($tags_prepare);$i++) {
$tags_prepare[$i]['growth'] = log($tags_prepare[$i]['cnt']);
if ($tags_prepare[$i]['growth'] > $maxv) $maxv = $tags_prepare[$i]['growth'];
}
for($i=0;$i < count($tags_prepare);$i++) {
$labels[$i]['growth'] = intval($tags_prepare[$i]['growth'] * $tags_growth_range / $maxv) ;
$labels[$i]['tag'] = $tags_prepare[$i]['name'];
$labels[$i]['count'] = $tags_prepare[$i]['cnt'];
}
print_r($labels);
это для меня наверно сложно очень. Я начинающий и не сильно понимаю что написанно... т.е кое что понимаю но не все. мне более понятен код который в уроке. но не пойму почему у меня не получаеться выбрать теги из бд..
![[logo]](/site/images/logo.jpg)
