Loop From DB For Cloud Tags

Discussion in 'PHP' started by IGiveMoney, Jun 30, 2008.

  1. #1
    Ok so I grabbed some code for a tag cloud.

    Was trying to get it to connect to a database
    and loop thru the results in the database to dynamically
    place the words into the tag cloud.

    Having abit of difficulty.

    here is the present code:

    
     <?
    
      $dbname     = "XXXXXXX";
      $dbserver   = "localhost";
      $dbuser     = "XXXXX";
      $dbpass     = "XXXX";
      $table      = "cloud"; 
      
    $db = mysql_connect("$dbserver", "$dbuser", "$dbpass");
    mysql_select_db("$dbname",$db); 
      
    
    
        class wordCloud
        {
        var $wordsArray = array();
    
    
    
        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;
        }
        }
        }
    
        $cloud = new wordcloud();
    
    		$sql= "SELECT word FROM cloud";
    				$result = mysql_query($sql) or die ('I cannot connect to the database because: ' . mysql_error());
    
    				while ($row = mysql_fetch_array($result)) {
    
    				$id = $row["word"]; 
    				}
    					$cloud = new wordCloud($row["word"]);
    						echo $cloud->showCloud();
    
    	
    	?>
    
    Code (markup):

    Now this is the end of the same script that basically
    just ads some 'random' words from the array. This section works:

    
    
    <?
    $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();
    ?>
    
    
    Code (markup):

    But as you can see at the end I've tried to make some adjustments
    to get it loop properly through the results and display accordingly:

    
        $cloud = new wordcloud();
    
    		$sql= "SELECT word FROM cloud";
    				$result = mysql_query($sql) or die ('I cannot connect to the database because: ' . mysql_error());
    
    				while ($row = mysql_fetch_array($result)) {
    
    				$id = $row["word"]; 
    				}
    					$cloud = new wordCloud($row["word"]);
    						echo $cloud->showCloud();
    
    
    
    Code (markup):
    With that present code I'm getting:

    Presumably because its not pulling information from the database.

    I had it working, but it was only pulling the first word from the
    row, and repeating it infinitely.

    Any help is appreciated.
     
    IGiveMoney, Jun 30, 2008 IP
  2. IGiveMoney

    IGiveMoney Peon

    Messages:
    116
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    thousands of users and no one has a response :(
     
    IGiveMoney, Jun 30, 2008 IP
  3. lfhost

    lfhost Peon

    Messages:
    232
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You either need to make

    				$id = $row["word"];
    Code (markup):
    an array like
    				$id[] = $row["word"];
    Code (markup):
    then change

    					$cloud = new wordCloud($row["word"]);
    Code (markup):
    to
    					$cloud = new wordCloud($id);
    Code (markup):
    or put

    					$cloud = new wordCloud($row["word"]);
    Code (markup):
    inside the while loop.
     
    lfhost, Jun 30, 2008 IP
  4. IGiveMoney

    IGiveMoney Peon

    Messages:
    116
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    MANY MANY MANY KUDOS!

    (hope you dont get fat)

    thank you
     
    IGiveMoney, Jun 30, 2008 IP
  5. IGiveMoney

    IGiveMoney Peon

    Messages:
    116
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    ok this is really driving me nuts!!!


    
    
    
    $sql= "SELECT word FROM cloud";
    	$result = mysql_query($sql) or die ('I cannot connect to the database because: ' . mysql_error());
    
    	while ($row = mysql_fetch_array($result)) {
    
    		$id[] = $row["word"];
    	}
    $cloud = new wordCloud($id);
    	echo $cloud->showCloud();
    
    
    Code (markup):
    That code works perfectly, it pulls from the database without a problem
    and displays the data from the rows.

    The problem I have now is trying to get the 'style' into it.

    Here is the original static version:
    
    <?
    $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();
    ?>
    
    Code (markup):
    as you can see $cloud->addWord("music", 12); sets the words music and then sets a 'style' to it.

    I am trying to get that integrated with the loop.

    Im probably just missing it, but ive been doing this way to long
    and my eyes need another set im sure.

    $10 for quick fix in paypal.

    First person that can get the randomization into the loop
    and it works and pms me with their paypal email and their working
    fix - gets $10 in paypal from me.

    Thanks so much
     
    IGiveMoney, Jun 30, 2008 IP
  6. IGiveMoney

    IGiveMoney Peon

    Messages:
    116
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    anyone... id appreciate some input, really trying to figure this out and
    it seems like its getting the best of me :(
     
    IGiveMoney, Jun 30, 2008 IP
  7. lfhost

    lfhost Peon

    Messages:
    232
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Tag clouds are usually based on what is searched on a site and then the word is saved into a mysql DB table.
    Then, if the word is searched again and is already stored, you can use an incremental key and add to it everytime it is searched. (say we call this used)

    Then after the $id[] field, you can add

    $cloud->addWord($row["word"], $row["used"]);
    Code (markup):
    but in your initial query, remember to add
    ,used
    Code (markup):
    after
    select word
    Code (markup):
    else the result will be blank.
     
    lfhost, Jul 1, 2008 IP