2008-08-01 23 views
65

मैं निम्नलिखित सरणियों है:आकार बदलने के लिए h1 से h6 का उपयोग करके सरणी से टैग क्लाउड उत्पन्न करने का सबसे अच्छा तरीका क्या है?

$artist = array("the roots", "michael jackson", "billy idol", "more", "and more", "and_YET_MORE"); 
$count = array(5, 3, 9, 1, 1, 3); 

मैं एक टैग बादल न्यूनतम संलग्न h1 टैग $counth6 टैग में संलग्न में अधिक संख्या में और साथ कलाकारों होगा उत्पन्न करना चाहते हैं।

उत्तर

38

आप इसे भी एक लॉगरिदमिक कार्य जोड़ना चाहते हैं। (Tagadelic से लिया, मेरे Drupal मॉड्यूल टैग बादल बनाने के लिए http://drupal.org/project/tagadelic):

db_query('SELECT COUNT(*) AS count, id, name FROM ... ORDER BY count DESC'); 

$steps = 6; 
$tags = array(); 
$min = 1e9; 
$max = -1e9; 

while ($tag = db_fetch_object($result)) { 
    $tag->number_of_posts = $tag->count; #sets the amount of items a certain tag has attached to it 
    $tag->count = log($tag->count); 
    $min = min($min, $tag->count); 
    $max = max($max, $tag->count); 
    $tags[$tag->tid] = $tag; 
} 
// Note: we need to ensure the range is slightly too large to make sure even 
// the largest element is rounded down. 
$range = max(.01, $max - $min) * 1.0001; 

foreach ($tags as $key => $value) { 
    $tags[$key]->weight = 1 + floor($steps * ($value->count - $min)/$range); 
} 
आपके विचार या टेम्पलेट में

तब:

foreach ($tags as $tag) { 
    $output .= "<h$tag->weight>$tag->name</h$tag->weight>" 
} 
29

मेरे सिर के ऊपर बंद ...

$artist = array("the roots","michael jackson","billy idol","more","and more","and_YET_MORE"); 
$count = array(5,3,9,1,1,3); 
$highest = max($count); 
for (int $x = 0; x < count($artist); $x++) 
{ 
    $normalized = $count[$x]/$highest; 
    $heading = ceil($normalized * 6); // 6 heading types 
    echo "<h".$heading.">".$artist[$x]."</h".$heading.">"; 
} 
6

@Ryan

यह सही है लेकिन यह वास्तव में कम से कम संख्या, बड़ा के साथ टैग कर देता है। इस कोड को परीक्षण किया गया है:

$artist = array("the roots","michael jackson","billy idol","more","and more","and_YET_MORE"); 
$count = array(5,3,9,1,1,3); 
$highest = max($count); 
for ($x = 0; $x < count($artist); $x++) { 
    $normalized = ($highest - $count[$x]+1)/$highest; 
    $heading = ceil($normalized * 6); // 6 heading types 
    echo "<h$heading>{$artist[$x]}</h$heading>"; 
} 
22

शायद यह है एक छोटे से शैक्षणिक और विषय से दूर लेकिन hX टैग शायद दस्तावेज़ संरचना के कारणों और बात के सभी उस तरह के लिए एक टैग बादल के लिए सबसे अच्छा विकल्प नहीं हैं।

शायद span एस या ol उचित कक्षा विशेषताओं (साथ ही कुछ सीएसएस) के साथ?

8

थोड़ी देर के लिए इस स्निपेट का उपयोग किया है, क्रेडिट prism-perfect.net है। हालांकि

<div id="tags"> 
    <div class="title">Popular Searches</div> 
    <?php 
     // Snippet taken from [prism-perfect.net] 

     include "/path/to/public_html/search/settings/database.php"; 
     include "/path/to/public_html/search/settings/conf.php"; 

     $query = "SELECT query AS tag, COUNT(*) AS quantity 
     FROM sphider_query_log 
     WHERE results > 0 
     GROUP BY query 
     ORDER BY query ASC 
     LIMIT 10"; 

     $result = mysql_query($query) or die(mysql_error()); 

     while ($row = mysql_fetch_array($result)) { 

      $tags[$row['tag']] = $row['quantity']; 
     } 

     // change these font sizes if you will 
     $max_size = 30; // max font size in % 
     $min_size = 11; // 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 /search/search.php... 
      // (assuming your tag links to some sort of details page) 
      echo '<a href="/search/search.php?query='.$key.'&search=1" style="font-size: '.$size.'px"'; 
      // perhaps adjust this title attribute for the things that are tagged 
      echo ' title="'.$value.' things tagged with '.$key.'"'; 
      echo '>'.$key.'</a> '; 
      // notice the space at the end of the link 
     } 
    ?> 
</div> 
+0

यह मेरे लिए एक अच्छा दृष्टिकोण लगता है में प्रयोग किया जाता है। यदि आपका डेटा किसी सरणी में है तो बस डेटाबेस भाग को छोड़ दें। मैं आपको कलाकार नाम का संग्रह करने और एक सहयोगी सरणी में गिनने की सलाह दूंगा। उपर्युक्त कोड के साथ यह काम करने के लिए कुछ ऐसा उपयोग करें: $ टैग = सरणी ("जड़ों" => 5, "माइकल जैक्सन" = 3, "बिली मूर्ति" => 9, "मैडोना" => 1); मैं सहमत हूं कि एच टैग का उपयोग न करें क्योंकि यह आपके अर्थशास्त्र को गड़बड़ कर देता है। स्पैन मेरी पसंद होगी। अंत में, ज़ेंडर फ्रेमवर्क में एक सहायक मौजूद है जो आपको केवल वही कर सकता है जो आपको चाहिए। Http://framework.zend.com/manual/en/zend.tag.html – simonrjones

+0

क्षमा करें, उपर्युक्त टिप्पणी में कोई स्वरूपण ऐसा नहीं लगता है! – simonrjones

1

एच टैग का उपयोग नहीं करता है रेल में एक सहायक के रूप में:

<%= tag_cloud($artists, $counts) %> 

यह एक सरणी है कि हो जाएगा में <span style='font-size:_em'> तत्वों आउटपुट:

def tag_cloud (strings, counts) 
    max = counts.max 
    strings.map { |a| "<span style='font-size:#{((counts[strings.index(a)] * 4.0)/max).ceil}em'>#{a}</span> " } 
end 

दृश्य से यह कॉल आखिर में इस तरह प्रस्तुत करने के लिए दृश्य में एक स्ट्रिंग में परिवर्तित:

<span style='font-size:3em'>the roots</span> 
<span style='font-size:2em'>michael jackson</span> 
<span style='font-size:4em'>billy idol</span> 
<span style='font-size:1em'>more</span> 
<span style='font-size:1em'>and more</span> 
<span style='font-size:2em'>and_YET_MORE</span> 

class को ब्रेंडन द्वारा उल्लिखित स्टाइल शीट में कक्षाओं को विशेषता और संदर्भ देना बेहतर होगा। h1-h6 का उपयोग करने से काफी बेहतर है और <span> के साथ कम स्टाइल बैगेज है।

+0

किसी ने इसे -1 क्यों दिया? – berkes

2

इस विधि SQL/PostgreSQL कट्टरपंथियों के लिए है। यह डेटाबेस में पूरी नौकरी करता है, और यह "slugified" लिंक के साथ पाठ मुद्रित करता है। यह सिर्फ एसक्यूएल कॉल के लिए सिद्धांत ORM का उपयोग करता है, मैं वस्तुओं का उपयोग नहीं कर रहा हूं। मान लीजिए हम 10 आकार:

public function getAllForTagCloud($fontSizes = 10) 
{ 
    $sql = sprintf("SELECT count(tag) as tagcount,tag,slug, 
    floor((count(*) * %d)/(select max(t) from 
     (select count(tag) as t from magazine_tag group by tag) t)::numeric(6,2)) 
     as ranking 
     from magazine_tag mt group by tag,slug", $fontSizes); 

    $q = Doctrine_Manager::getInstance()->getCurrentConnection(); 
    return $q->execute($sql); 
} 

तो आप, कुछ सीएसएस वर्ग के साथ उन्हें प्रिंट .tagranking10 से (सर्वोत्तम) करने के लिए।tagranking1 (सबसे खराब):

<?php foreach ($allTags as $tag): ?> 
    <span class="<?php echo 'tagrank'.$tag['ranking'] ?>"> 
     <?php echo sprintf('<a rel="tag" href="/search/by/tag/%s">%s</a>', 
      $tag['slug'], $tag['tag'] 
     ); ?> 
    </span> 
<?php endforeach; ?> 

और इस CSS है:

/* put your size of choice */ 
.tagrank1{font-size: 0.3em;} 
.tagrank2{font-size: 0.4em;} 
.tagrank3{font-size: 0.5em;} 
/* go on till tagrank10 */ 

इस विधि सभी टैग प्रदर्शित करता है। यदि आपके पास बहुत सारे हैं, तो शायद आप नहीं चाहते कि आपका टैग क्लाउड टैग तूफान बन जाए। उस मामले में आप अपने SQL क्वेरी के लिए एक HAVING TO खंड संलग्न करेंगे:

-- minimum tag count is 8 -- 

HAVING count(tag) > 7 

बस इतना ही

0

मैं अपने बहुत पुराने पोस्ट पता है। मेरे विचार को पोस्ट करना क्योंकि यह भविष्य में किसी की मदद कर सकता है।

यहाँ tagcloud मैं अपनी वेबसाइट http://www.vbausefulcodes.in/

<?php 
$input= array("vba","macros","excel","outlook","powerpoint","access","database","interview questions","sendkeys","word","excel projects","visual basic projects","excel vba","macro","excel visual basic","tutorial","programming","learn macros","vba examples"); 

$rand_tags = array_rand($input, 5); 
for ($x = 0; $x <= 4; $x++) { 
    $size = rand (1 , 4); 
    echo "<font size='$size'>" . $input[$rand_tags[$x]] . " " . "</font>"; 
} 

echo "<br>"; 
$rand_tags = array_rand($input, 7); 
for ($x = 0; $x <= 6; $x++) { 
    $size = rand (1 , 4); 
    echo "<font size='$size'>" . $input[$rand_tags[$x]] . " " . "</font>"; 
} 

echo "<br>"; 
$rand_tags = array_rand($input, 5); 
for ($x = 0; $x <= 4; $x++) { 
    $size = rand (1 , 4); 
    echo "<font size='$size'>" . $input[$rand_tags[$x]] . " " . "</font>"; 
} 
?>