code

크기 조정을 위해 h1 ~ h6을 사용하여 어레이에서 태그 클라우드를 생성하는 가장 좋은 방법은 무엇입니까?

codestyles 2020. 10. 27. 08:16
반응형

크기 조정을 위해 h1 ~ h6을 사용하여 어레이에서 태그 클라우드를 생성하는 가장 좋은 방법은 무엇입니까?


다음 배열이 있습니다.

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

더 높은 숫자의 아티스트를 태그 $count묶고 h6가장 낮은 태그 묶는 태그 클라우드를 생성하고 싶습니다 h1.


로그 함수도 추가하고 싶을 것입니다. (태그 구름 http://drupal.org/project/tagadelic 을 만들기 위해 내 Drupal 모듈 인 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>"
}

내 머릿속에서 ...

$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.">";
}

아마도 이것은 약간 학문적이며 주제에서 벗어난 주제이지만 hX문서 구조 및 모든 종류의 이유로 태그 클라우드에 대한 최상의 선택은 아닐 것입니다.

아마도 spans 또는 ol적절한 클래스 속성 (및 일부 CSS)이 있습니까?


잠시 동안이 스 니펫을 사용했으며 크레딧은 prism-perfect.net입니다. 그래도 H 태그를 사용하지 않습니다.

<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>

라이언

맞지만 실제로는 가장 적은 수의 태그를 더 크게 만듭니다. 이 코드는 테스트되었습니다.

$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>";
}

이 방법은 SQL/PostgreSQL광신자 들을위한 것 입니다. 데이터베이스에서 전체 작업을 수행하고 "slugified"링크로 텍스트를 인쇄합니다. ORMSQL 호출에만 Doctrine을 사용하며 개체를 사용하지 않습니다. 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 (최악)까지 CSS 클래스로 인쇄합니다.

<?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 */

이 방법은 모든 태그를 표시합니다. 그것들이 많으면 태그 클라우드가 태그 스톰 이되는 것을 원하지 않을 것입니다 . 이 경우 HAVING TOSQL 쿼리에 절을 추가 합니다.

-- minimum tag count is 8 --

HAVING count(tag) > 7

그게 다야


Rails의 도우미로서 :

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

Call this from the view:

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

This outputs <span style='font-size:_em'> elements in an array that will be converted to a string in the view to ultimately render like so:

<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> 

It would be better to have a class attribute and reference the classes in a style sheet as mentioned by Brendan above. Much better than using h1-h6 semantically and there's less style baggage with a <span>.


I know its very old post. posting my view as it may help someone in future.

Here is the tagcloud I used in my website 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>";
}
?>

참고URL : https://stackoverflow.com/questions/227/whats-the-best-way-to-generate-a-tag-cloud-from-an-array-using-h1-through-h6-fo

반응형