SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Data array help needed

    I am trying to do a tagcloud/weighed listing based on keywords given in an article seperated by commas.

    I can get everything unseperated but sorting the data and adding the duplicated keywords is my problem

    Partial code:
    $labels = explode(',', $r['keywords_meta']);
    for ($i=0; $i <= 4; $i++); //problem??
    $labels[$i]; // problem??
    $arr = $labels;
    }
    ksort($arr);
    return($arr);

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by centered effect View Post
    I am trying to do a tagcloud/weighed listing based on keywords given in an article seperated by commas.

    I can get everything unseperated but sorting the data and adding the duplicated keywords is my problem

    Partial code:
    $labels = explode(',', $r['keywords_meta']);
    for ($i=0; $i <= 4; $i++); //problem??
    $labels[$i]; // problem??
    $arr = $labels;
    }
    ksort($arr);
    return($arr);
    PHP Code:
    function EntryExist$Haystack$Needle) {
        foreach( 
    $Haystack as $tmp ) {
            if ( 
    $tmp == $Needle ) { return TRUE; }
        }
        return 
    FALSE;
    }

    Function 
    No_Duplicate_List$r ) {
        
    $out = array();
        
    $labels explode(','$r['keywords_meta']);
        foreach( 
    $labels as $value ) {
            if ( !
    EntryExist$out$value )) {
                if ( 
    count$out ) == ) {
                    
    $out[0] = $value;
                } else {
                    
    $idx count$out );
                    
    $out[$idx] = $value;
                }
            }
        }
        return 
    $out;

    This gives you a no duplicate list, is that what you want or do you want a list of duplicate entries ?
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  3. #3
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Let me see if I can explain better:


    Current code:
    Code:
    // TAGS
    function getTags () {
    	connect_to_db();
    	$db = new Database(s('dbname'));
    	$query = "SELECT * FROM " .s('prefix'). "articles WHERE published = 1";          
    	$result = $db->query($query);
    	while ($r = $result->fetch_array()) {
    		$labels = explode(',', $r['keywords_meta']);
    		foreach ($labels as $labels) {
    	        //        srand ((double) microtime( )*1000000);
    	        //        $tagnum = (rand()%5);
    	                echo '<a class="t'.$tagnum.'" href="index.php?search='.$labels.'">'.$labels.'</a> '; 
                    }
    	}
    }
    In index.php
    Code:
    		<div id="tagsbox">
    		<h2>TagCloud</h2>
    		<? getTags() ; ?>			
    
    		</div>
    The user will input keywords as such lorem ipsum, lorem, ipsum which is then inputted into the database. The code currently takes the keywords and forms the cloud and radomizes the output of the wieghts. But I would like it to be based from the wieght of the keywords used. As in the picture "lorem" is used twice so it should get a value of 2 making it a heavier weight, giving it a bigger font.

    Is that clearer?

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function EntryExist$Haystack$Needle) {
        foreach( 
    $Haystack as $K => $v ) {
            if ( 
    $K == $Needle ) { return TRUE; }
        }
        return 
    FALSE;
    }

    Function 
    Weighted_Word_List$r ) {
        
    $out = array();
        
    $labels explode(','$r['keywords_meta']);
        foreach( 
    $labels as $value ) {
            if ( !
    EntryExist$out$value )) {
                
    $out[$value] = 1;
            } else {
                
    $out[$value] += 1;
            }
        }
        return 
    $out;

    The above functions will take an array:
    $mydata = array( "apple","bannana","pear","orange","apple","olive","orange");
    and output :
    Array ( [apple] => 2 [bannana] => 1 [pear] => 1 [orange] => 2 [olive] => 1 )

    You do realize words such as "lorem ipsum" will still need to be split on the " ".
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  5. #5
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Oh ok, I didn't fully read the code.

    Now the question is where do I put the DB information? in the second fucntion?

    connect_to_db();
    $db = new Database(s('dbname'));
    $query = "SELECT * FROM " .s('prefix'). "articles WHERE published = 1";
    $result = $db->query($query);
    while ($r = $result->fetch_array()) {

    then what would be the needle and haystack variables be? lol (actually serious question though)

  6. #6
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    Function Weighted_Word_List( ) {
        
    $out = array(); // Declare an array variable
        
        /* Your database code goes here */
        
    connect_to_db();
        
    $db = new Database(s('dbname'));
        
    $query "SELECT * FROM " .s('prefix'). "articles WHERE published = 1";
        
    $result $db->query($query);
        while (
    $r $result->fetch_array()) { // Fetch row from resultset
            
    $labels explode(','$r['keywords_meta']); // Explode comma seperated values
            /* My code starts here */
            
    foreach( $labels as $value ) { // Set up loop
                /* The first function is used here. The haystack is the array that will be returned */
                /* The needle is the value you are looking for in the output array */ 
                
    if ( !EntryExist$out$value )) { // Is there an entry for this word yet?
                    
    $out[$value] = 1// Does not exist, so create and set value to 1
                
    } else {
                    
    $out[$value] += 1// Exist already so increment by 1
                
    // End if
            
    // End foreach
        
    // End while
        
    return $out// Return array of keywords  
    // End function 
    To use :
    PHP Code:
    $keywords Weighted_Word_List();

    foreach( 
    $keywords as $K => $V ) {
        echo 
    '<a class="t'.$V.'" href="index.php?search='.$K.'">'.$K.'</a> ';

    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  7. #7
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Wow, thanks for that.

    The code works as it should, but the duplicate entries still show..



    Code:
    function EntryExist($out, $value) {
    	foreach ($out as $K => $v) {
            	if ($K == $value) { return TRUE; }
        	}
        	return FALSE;
    }
    
    function keyword_array () {
        	$out = array();
        	connect_to_db();
        	$db = new Database(s('dbname'));
        	$query = "SELECT * FROM " .s('prefix'). "articles WHERE published = 1";
        	$result = $db->query($query);
        	while ($r = $result->fetch_array()) {
            	$keywords = explode(',', $r['keywords_meta']);
            	foreach ($keywords as $value ) {
                		if ( !EntryExist( $out, $value )) {
                    		$out[$value] = 1; 
                		} else {
                    		$out[$value] += 1;
                		}
            	} 
        	} 
        	return $out;   
    }  
    
    function keyword_cloud() {
    	$keywords = keyword_array();
    	foreach( $keywords as $K => $V ) {
    	    echo '<a class="t'.$V.'" href="index.php?search='.$K.'">'.$K.'</a> ';
    	}
    }
    Also looking at the output, "lorem" is not highlighted as well. I believe there is a space there, since each articles has keywords seperated by a comma, but naturally a human would include a space to be more readable: "<meta name="keywords" content="lorem ipsum, lorem, ipsum, history" />" Content is what is inputted. I guess I need to explode some more... but exploding a blank space gives me an error output of ","... should of read the manual more.. it seems I can only explode 2 things...

    EDIT - duh!
    $keywords = explode(', ', $r['keywords_meta']); // space after comma
    Script works now!
    Last edited by centered effect; Jun 13, 2007 at 17:00. Reason: edited for overlooking something


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •