Support » Allgemeine Fragen » Array aus DB füllen

  • Hallo zusammen

    Ich habe für mein Glossar diese Funktion geschrieben:

    function rad_glossar_frontend($wort){
        global $wpdb;
        // Alle Begriffe laden
        $sql = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."rad_glossar");
        $ersetzen = array(
    	    // Zu ersetzendes Wort => Neuer Text
            'Lorem' => '<a href="#">Lorem</a>',
            'ipsum' => '<a href="#">ipsum</a>',
    	    'dolor' => '<a href="#">dolor</a>',   
        );
        $wort = str_replace(array_keys($ersetzen), $ersetzen, $wort);
        return $wort;
    }

    Das funktioniert soweit, auch die darin enthaltene DB Abfrage führt zu keinem Fehler. Um das Array aus der Datenbank zu füllen, habe ich die Funktion angepasst:

    function rad_glossar_frontend($wort){
        global $wpdb;
        // Alle Begriffe laden
        $sql = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."rad_glossar");
        $ersetzen = array(
    	    // Zu ersetzendes Wort => Neuer Text
            foreach($sql as $result)
            {
              // Füllt das Arry mit den Begriffen
              $result->glossar_term => '<a href="#">Lorem</a>',
            }
    	    'sdcndjffkvhdht' => '<a href="#">dolor</a>'  
        );
        $wort = str_replace(array_keys($ersetzen), $ersetzen, $wort);
        return $wort;
    }

    Rein theoretisch müsste der Inhalt im Array stimmen. Extra auch die Alibizeile am Schluss, damit die letzte Zeile im Array ohne Komma abschliesst. Das bringt allerdings die Seite zum Absturz. Ich habe den Verdacht, dass das foreach im Array nicht möglich ist. Aber wie sonst?

    Gruß René

Ansicht von 6 Antworten - 16 bis 21 (von insgesamt 21)
  • Versuche mal

    function rad_glossar_frontend($wort)
    {
        global $wpdb;
        
        $src = array();
        $dest = array();
        
        // Alle Begriffe laden
        $results = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."rad_glossar", ARRAY_A);
        
        foreach ($results as $row)
        {
           $src[]  = row['glossar_term'];
           $dest[] = row['glossar_description'];
        }
           
        $wort = str_replace($src, $dest, $wort);
        
        return $wort;
    }
    Thread-Starter radarin

    (@radarin)

    Das liefert locker 50 Fehlermeldungen.

    Jetzt stell dir mal vor, ich würde die kennen 😀

    Thread-Starter radarin

    (@radarin)

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Illegal string offset ‚glossar_term‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Illegal string offset ‚glossar_description‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Illegal string offset ‚glossar_term‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Illegal string offset ‚glossar_description‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Illegal string offset ‚glossar_term‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Illegal string offset ‚glossar_description‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Illegal string offset ‚glossar_term‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Illegal string offset ‚glossar_description‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Illegal string offset ‚glossar_term‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Illegal string offset ‚glossar_description‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Illegal string offset ‚glossar_term‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Illegal string offset ‚glossar_description‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Illegal string offset ‚glossar_term‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Illegal string offset ‚glossar_description‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Illegal string offset ‚glossar_term‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Illegal string offset ‚glossar_description‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Illegal string offset ‚glossar_term‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Illegal string offset ‚glossar_description‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Illegal string offset ‚glossar_term‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Illegal string offset ‚glossar_description‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Illegal string offset ‚glossar_term‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Illegal string offset ‚glossar_description‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Illegal string offset ‚glossar_term‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 285

    Warning: Use of undefined constant row – assumed ‚row‘ (this will throw an Error in a future version of PHP) in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Warning: Illegal string offset ‚glossar_description‘ in /home/darinch/www/dummies.ch/wp-content/plugins/radglossar/glossar.php on line 286

    Fehler ist klar, ändere

    $src[]  = row['glossar_term'];
    $dest[] = row['glossar_description'];

    in

    $src[]  = $row['glossar_term'];
    $dest[] = $row['glossar_description'];

    Hätte dir aber auch auffallen können. 😉

    Thread-Starter radarin

    (@radarin)

    Die Lösung steht:

    function rad_glossar_frontend($wort){
        global $wpdb;
        // Alle Begriffe laden
        $sql = $wpdb->get_results("SELECT glossar_ID, glossar_term, glossar_description FROM ".$wpdb->prefix."rad_glossar");
        // Array Zeilenweise füllen
        foreach($sql as $result) {
          $ausgabe = "'<a href'#' ";
          $ausgabe .= "class='tooltips'";
          $ausgabe .= "alt=''>";
          $ausgabe .= $result->glossar_term;
          $ausgabe .= "<span>".$result->glossar_description."</span>";
          $ausgabe .= "</a>',";
          $ersetzen[$result->glossar_term] = $ausgabe;
        }  
            
        $wort = str_replace(array_keys($ersetzen), $ersetzen, $wort);
        return $wort;
    }

    Mit Arrays hab ich kaum Erfahrung. Letztendlich ist die Lösung aber relativ simpel. Im FOREACH musste das => und das glossar_term davor entfernt werden. Am Schluss wird für jede Zeile in der Tabelle der Reihen nach eine Zeile ins Array geschrieben. Ach ja, korrektes HTML statt der Ersatzzeichen ist auf diese Art auch nötig.

    Funktioniert einwandfrei, ein komplettes Glossar im Frontend, Index und die Formulare zur Bearbeitung der DB mit nicht mal 300 Zeilen Code. Kann theoretisch als Plugin angeboten werden. Muss vorerst aber noch per FTP installiert werden, und der Import der Tabelle muss auch noch manuell erfolgen.

    Jetzt hab ich noch ein kleines CSS Problem, das aber liegt dann wohl am Theme.

    Vielen Dank für Deine Hilfe.

Ansicht von 6 Antworten - 16 bis 21 (von insgesamt 21)
  • Das Thema „Array aus DB füllen“ ist für neue Antworten geschlossen.