Tagcloud doppelte Werte und hervorheben

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Tagcloud doppelte Werte und hervorheben

    Hallo,

    für meinen Blog arbeite ich gerade an einer Tagcloud (Wortwolke)
    In der Tabelle lexicon_post wo sich die Artikel befinden habe ich für jeden Artikel noch die Spalte post_tags.
    In dieser befinden sich zum bsp. PHP, AJAX, HTML, CSS und im 2ten Artiel nur PHP

    Wenn ich diese nun Augebe gibt er mir nun 2x PHP aus. Wie kann ich dies verhindern? Mit SELECT DISTINCT will dies schon mal nicht.

    PHP-Quellcode

    1. $DB->set_sql('SELECT p.post_tags, p.post_depend_cat_id, p.post_activ, c.ID, c.usergroups FROM ' . PREFIX . 'lexicon_post AS p, ' . PREFIX . 'lexicon_cat AS c WHERE p.post_depend_cat_id = c.ID AND p.post_activ = 1');
    2. $DB->execute();
    3. while ($row = $DB->fetch_assoc()) {
    4. $perm_temp = array();
    5. // Befugnisgruppe passt nicht?
    6. $perm_temp = explode(',', $row['usergroups']);
    7. if (!is_array($perm_temp) || !in_array($user_auth, $perm_temp)) {
    8. continue;
    9. }
    10. $tagbox[] = $row;
    11. }
    12. $DB->free();
    Alles anzeigen
  • In diesem Fall wird GROUP BY, DISTINCT oder auch DISTINCT ON nicht funktionieren.
    Dies tut es nur wenn gleiche Werte vorhanden sind.

    Gehen wir von der Spalte tags_post aus. Dort sind 4 Einträge

    1) php, html, css
    2) php, html, css
    3) html, css
    4) css

    Mit GROUP BY, DISTINCT oder auch DISTINCT ON würde er 1,3 und 4 Anzeigen. 2 Ignoriert er da der gleiche Eintrag schon mal vorhanden ist, siehe 1)

    Ich muss aber jeden Kommagetrenten Wert einzeln haben und dann die Doppelten ausblenden. Mit

    PHP-Quellcode

    1. ​$temp = explode(',', $row['post_tags']);
    2. foreach ($temp as $temp_html) {
    3. $tag_out = str_replace(' ', '', $temp_html);
    4. $tags_out .= '<a href="index.php?mode=lexicon_search&var=' . $tag_out . '">' . $tag_out . '</a> ';
    5. }


    ist dies kein Problem allerdings habe ich noch keine Ahnung wie ich doppelte Werte nun entfernen kann so das am Ende nur php html css übrig bleibt.
  • Sorry mein Fehler
    Hier der SQL Kram

    SQL-Abfrage

    1. CREATE TABLE IF NOT EXISTS `cf300_lexicon_post` (
    2. `post_id` mediumint(8) NOT NULL AUTO_INCREMENT,
    3. `post_subject` varchar(255) NOT NULL,
    4. `post_tags` text NOT NULL,
    5. PRIMARY KEY (`post_id`)
    6. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=653 ;
    7. INSERT INTO `cf300_lexicon_post` (`post_id`, `post_subject`, `post_tags`) VALUES
    8. (653, 'Beendet eine if', 'PHP, Java, Javascript, CSS, MySQL,'),
    9. (652, 'Dies ist ein Testthema', 'PHP, MySQL, HTML, ');


    Hab es jetzt geschafft. Allerdings habe ich nun immer noch das Komma zwischen den Werten

    PHP-Quellcode

    1. $tags_out = '';
    2. $DB->set_sql('SELECT post_tags FROM ' . PREFIX . 'lexicon_post LIMIT 50');
    3. $DB->execute();
    4. while ($rows = $DB->fetch_assoc()) {
    5. $rowl[] = $rows;
    6. }
    7. foreach(array_unique($rowl) as $value) {
    8. $tags_out .= $value['post_tags'] ;
    9. }


    Die Ausgabe ist nun PHP, MySQL, CSS, Ajax, sollte aber PHP MySQL CSS Ajax sein, ebanfalls liest er nur aus einem einzigen Eintrag die Tags aus

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von Grommel ()

  • Das komme kannst du entfernen, indem du die Werte nach einem Komma explodest. Also:

    Dein Array sieht ja so aus:

    arr[0]["post_tags"] = "PHP, Java, ..."

    Wenn du in der foreach das nun explodest erhälst du ein neues Array, wo jeder Einzelwert getrennt drin steht.
    Aus "PHP, Java, ..." wird also ein neues Array:

    arr[] = "PHP";
    arr[] = "Java";
    arr[] = "...";

    Dies kannst du dann in $tags_out speichern lassen. Musst kein foreach da machen, es reicht auch ein "implode".

    ODER
    Du entfernst das Komma einfach mit einem str_replace nach der foreach.

    //EDIT

    Deine Gesamttags sind ja "PHP, Java, Javascript, CSS, MySQL,PHP, MySQL, HTML". Da nach einem Unique alles doppelte gestrichen wird, sollte nur noch "PHP, Java, Javascript, CSS, Mysql, HTML" übrig bleiben.
  • Hi,

    wenn Du das wie folgt machst hast Du am Ende ein neues Array $tags welches als key den Tag hat und als Value die Gewichtung, also wie oft der Tag vorkommt. Vielleicht hilft Dir das ja weiter.

    Quellcode

    1. $tags = [];
    2. // while rows as row
    3. $temp = explode(',', $row['post_tags']);
    4. foreach ($temp as $tag) {
    5. $tag = trim($tag);
    6. if(empty($tag)) continue;
    7. if(isset($tags[$tag]))
    8. {
    9. $tags[$tag]++;
    10. }
    11. else
    12. {
    13. $tags[$tag] = 1;
    14. }
    15. }
    Alles anzeigen


    P.S. ist jetzt nicht getestet, müsste aber funktionieren.
  • Moin,

    ich danke euch für eure Hilfe. Ich habe jetzt eine Kombination aus allen Vorschlägen und möchte die Lösung nicht vorenthalten. Vieleicht braucht das ja in Zukunft auch jemand.Im Anhang befindet sich die DB Struktur.
    Der Code entfernt die Komma und filtert dann alle doppelten Einträge

    PHP-Quellcode

    1. // Tags auslesen
    2. $DB->set_sql('SELECT post_tags FROM ' . PREFIX . 'blog_post GROUP BY post_tags LIMIT 50');
    3. $DB->execute();
    4. while ($row = $DB->fetch_assoc()) {
    5. $tags = $row['post_tags'];
    6. $row_tags = explode(",", $tags);
    7. $all_tags = array_merge($all_tags, $row_tags);
    8. }
    9. // Doppelte verwerfen
    10. $all_tags = array_unique($all_tags);
    11. sort($all_tags);
    12. foreach($all_tags as $value){
    13. $tags_out .= '<a href="index.php?mode=blogsearch&id=1&amp;tag=' . $value . '">' . $value . '</a> ';
    14. }
    Alles anzeigen
    Bilder
    • tag.png

      15,96 kB, 728×437, 288 mal angesehen