Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [klasa] Generator tagów
Balon
post
Post #1





Grupa: Zarejestrowani
Postów: 422
Pomógł: 0
Dołączył: 14.12.2005
Skąd: Wałbrzych

Ostrzeżenie: (0%)
-----


  1. <?php
  2.  
  3. class tags {
  4. public $tags;
  5. public $tagscount;
  6. public $prior;
  7. public $fsizes;
  8. public $cache;
  9.  
  10. function __construct( $tags, $fsizes ) {
  11. $this->tags = $tags;
  12. $this->tagscount = $this->getAllTagsCount( $tags );
  13. $this->fsizes = $fsizes;
  14. }
  15.  
  16. function getAllTagsCount( $tags ) {
  17. $count = 0;
  18. foreach( $tags as $tag ) {
  19. $count += $tag[key( $tag )]['count'];
  20. }
  21. return $count;
  22. }
  23.  
  24. function definePrior() {
  25. $temp = array();
  26. foreach( $this->tags as $tag ) {
  27. $temp[key( $tag )] = round( ( $tag[key( $tag )]['count'] * 100 ) / $this->tagscount );
  28. }
  29. $this->prior = $temp;
  30. }
  31.  
  32. function defineSizes() {
  33. $i = 0;
  34. foreach( $this->tags as $tag ) {
  35. array_push( $this->tags[$i][key( $tag )], $this->sizeByPrior( $this->prior[key( $tag )] ) );
  36. $i++;
  37. }
  38. }
  39.  
  40. function sizeByPrior( $prior ) {
  41. $sizes = array_keys( $this->fsizes );
  42. $ca = array();
  43. foreach( $sizes as $size ) {
  44. if( $prior > $size ) {
  45. $ca[] = $this->fsizes[$size]['size'];
  46. }
  47. else if( $prior == $size ) {
  48. return $this->fsizes[$size]['size'];
  49. }
  50. }
  51. return count( $ca ) > 0 ? max( $ca ) : false;
  52. }
  53.  
  54. function convertArray() {
  55. $new = array();
  56. foreach( $this->tags as $tag ) {
  57. $new[key( $tag )] = array( 'size' => $tag[key( $tag )][0] );
  58. }
  59. return $new;
  60. }
  61.  
  62. function genTagSizes() {
  63. $this->definePrior();
  64. $this->defineSizes();
  65. return $this->convertArray();
  66. }
  67. }
  68. ?>


użycie

  1. <?php
  2. /**
  3.  * Może być to np wyciąg z bazy danych
  4.  */
  5. $tag_list = array(  array( 'balon' => array( 'count' => 10 ) ),
  6. array( 'jagiełło' => array( 'count' => 450 ) ),
  7. array( 'krzysiek' => array( 'count' => 60 ) ),
  8. array( 'master' => array( 'count' => 34 ) ) );
  9.  
  10. /**
  11.  * Najniższą wartością musi być 1 bo inaczej będzie zwracało puste wyniki ( wielkości czcionek )
  12.  */
  13. $font_sizes = array(  '1' => array( 'size' => 10 ),
  14. '60' => array( 'size' => 20 ) );
  15.  
  16. $tags = new tags( $tag_list, $font_sizes );
  17. $smarty->assign( 'TAGS', $tags->genTagSizes() );
  18. ?>


taka klasa zwraca np tablice
Kod
Array
(
    [balon] => Array
        (
            [size] => 10
        )

    [jagiełło] => Array
        (
            [size] => 20
        )

    [krzysiek] => Array
        (
            [size] => 10
        )

    [master] => Array
        (
            [size] => 10
        )

)

wiem ze nakombinowane jest ile tylko mozna, ale pogubilem sie w polowie i zamieszałem troche (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg) ale działać działą a to najwazniejsze

prosze o oceny
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 12:00