Table of Contents

Ersetzen mit DB-Inhalt

Aufgabenstellung:
...

In diesem (sinnfreien) Beispiel wird exemplarisch die DB-Tabelle “phpwcms_country” verwendet, die in jeder Installation vorhanden ist.

Ich setzte die DB-Spalte “country_id” gleich mit der “Artikel-ID” um das Ergebnis zu erhalten.

Vorgehensweise:
Ein “frontend_render” Script reagiert auf einen betimmten TAG und liefert das Ergebnis aus der DB an dieser Stelle aus. Mit dem TAG wird auch die Artikel-ID an das Script übergeben (Artikellistenmodus). Im Artikeldetailmodus wird die ID in der Systemvariablen “$aktion[1]” geliefert.

Im Artikeldetailtemplate wird eine Markierung <!--ARTICLE_COMMENT--> gesetzt um die betroffenen Artikel zu kennzeichnen. Alle Artikel die dieses Template verwenden werden also vom Script untersucht.

Im Artikellistentemplate wird der TAG <!--[%ARTICLE_LIST:{ARTICLEID}%]--> am gewünschten Ausgabeort für das Ergebnis (DB-Inhalt) gesetzt. Dieser TAG übernimmt hier zwei Aufgaben, zum einen die Kennzeichnung ob das Script überhaupt tätig wird und zum anderen die Ortsbestimmung für die Ergebnisausgabe.

Für den Modus Artikeldetail wird in der Hauptvorlage der Seite direkt hinter {CONETNT} der TAG <!--%ARTICLE_COMMENT%--> gesetzt. So ist gewährleistet, dass die Ergebnisausgabe (DB-Inhalt) hinter dem letzten CP steht. (Sonderfälle mit CUSTOM_BLOCKS bitte beachten).

Frontend:

Artikel-Liste

Artikel-Detail

Templates:

Artikel-Liste

Datei: temples/inc_cntpart/articlesummary/list/my_sample_list.tmpl

my_sample_list.tmpl

<!--
sample template for article summary listing
===========================================
 
TITLE       = article title
SUB         = article subtitle
SUMMARY     = article summary text
IMAGE       = summary image (only the <img src="..."> tag)
ZOOMIMAGE   = if the image is click-zoomable (<a href...><img...></a>)
CAPTION     = summary image caption
ARTICLELINK = url to full article
MORE        = the "more..." text/image like defined in template_defaults
BEFORE      = show before listing entry
AFTER       = show after listing entry
SPACE       = show this between listings
DATE        = show article date {DATE:FORMAT:LANG} or {DATE:FORMAT}
EDITOR      = name of editor
ARTICLEID   = ID of list entry
 
//-->
[SPACE]<hr />[/SPACE]
 
<div>
    <table cellspacing="0" cellpadding="0" border="0" width="100%" summary="">
        <tr>
            <td width="150" valign="top">
                [IMAGE]<div><a href="{ARTICLELINK}">{IMAGE}</a></div>[/IMAGE]
                [CAPTION]<div align="center">{CAPTION}</div>[/CAPTION]
            </td>
            <td width="10"><img src="img/leer.gif" border="0" width="10" height="1" alt="" /></td>
            <td width="98%" valign="top">
                [TITLE]<h1 style="margin:0 0 5px 0;">{TITLE}  (Article-ID={ARTICLEID})</h1>[/TITLE]
                [SUB]<h3 style="margin:0 0 5px 0;">{SUB}</h3>[/SUB]
                [SUMMARY]<p style="margin:0"><strong>{DATE:F d.m.Y lang=DE}</strong> {SUMMARY}</p>[/SUMMARY]
                [MORE]<a href="{ARTICLELINK}"{TARGET}>{MORE}</a>[/MORE]
            </td>
        </tr>
    </table>
    <!--[%ARTICLE_LIST:{ARTICLEID}%]-->
</div>


Artikel-Detail

Datei: temples/inc_cntpart/articlesummary/article/my_sample_detail.tmpl

my_sample_detail.tmpl

<!--
sample custom full article summary template
===========================================
 
TITLE             = article title
SUB               = article subtitle
SUMMARY           = article summary text
IMAGE             = summary image, also click zoomable
CAPTION           = summary image caption
DATE              = show article date {DATE:FORMAT lang=LANG} or {DATE:FORMAT}
CURRENT_ARTICLEID = ID of list entry
 
//-->
<div>
 
[PHP]//dumpVar($GLOBALS['aktion']);[/PHP]
[TITLE]<h1 style="margin:0 0 5px 0;">{TITLE}  (Article-ID={CURRENT_ARTICLEID})</h1>[/TITLE]
[SUB]<h3 style="margin:0 0 5px 0;">{SUB}</h3>[/SUB]
<p style="margin:0">[IMAGE]<span style="float:left;margin:2px 10px 8px 0;">{IMAGE}[CAPTION]<br>{SPACER:1x3}<br />{CAPTION}[/CAPTION]</span> [/IMAGE]
<strong>{DATE:Y-m-d}</strong> {SUMMARY}</p>
</div>
<div style="clear:both";><!-- nix //--> </div>
 
<!--ARTICLE_COMMENT-->


Vorlage in Haupt:

Die Ausgabe im Modus Artikeldetail.

....
 {CONTENT}
 <!--%ARTICLE_COMMENT%-->
....

PHP Script

Datei: template/inc_script/frontend_render/replace_articleid_with_db_query.php

(Die Debug-Einträge “dumpVar($xxx);” sind im Script noch auskommentiert vorhanden).

replace_articleid_with_db_query

<?php
/**
*********************************************************************************************
* 25.10.10 KH: http://planmatrix.de V1.0
* Einfügen von DB-Inhalt in bestimmte Artikel mit ID Selektion.
*********************************************************************************************
*/
 
 
// -------------------------------------------------------------------------------------------
// obligate check for phpwcms constants
   if (!defined('PHPWCMS_ROOT')) {die("You Cannot Access This Script Directly, Have a Nice Day."); }
// -------------------------------------------------------------------------------------------
 
 
function my_dbquery ($param=0) {
// ---------------------------------------------------
// Auslesen der Tabelle country_table
// Input: Integer - Array oder Variable
//----------------------------------------------------
 
//    dumpVar($param);
 
    // Example SQL statement for country table
    $sql  = "SELECT country_id, country_iso, country_iso3, country_isonum, country_continent_code, country_name, country_continent ";
 
    $sql .= "FROM ".DB_PREPEND."phpwcms_country ";
    // ---- Select by ....
    // Is it an array or an var
    if (is_array($param))
        $sql .= "WHERE country_id IN ('".implode("','", $param)."') ";    // Variant array
    else $sql .= "WHERE country_id =". $param ." ";    // Variant integer
 
    // ---- Sort order
    $sql .= "ORDER BY country_id ASC";
 
//    --- Test
//    dumpVar($sql);
 
    $result = _dbQuery($sql);
 
//    dumpVar($result);
 
return $result;
 
}
// END function ---------------------------------------
 
 
 
// Artikel-Liste
// -----------------------------------------------------------------------------
// TEST Artikel-Listenmodus: Artikel-ID String auslesen und ersetzen
// <!--[%ARTICLE_LIST:{ARTICLEID}%]-->
 
// Nur wenn in Artikel-Listenansicht und zu ersetzender String vorhanden
if( ( empty($aktion[4])  AND  !strpos($content["all"],'<!--[%ARTICLE_LIST:') === false) ) {
 
    // ---- Custom var
    $no_result = 'Kein Eintrag gefunden!!';
 
 
    $article_id = array();
    preg_match_all('/\<!--\[%ARTICLE_LIST:(\d+)%\]--\>/s', $content["all"], $article_id);
    //dumpVar($article_id);
 
    // Nun anhand der IDs die DB Abfragen und in einem zweiten Durchlauf jeweils einsetzen
    // .... DBabfrage
 
    //Layout fuer Ergebnis setzen
    $layout01 = '<div style="padding: 5px; margin: 10px 0 0 13px; width:570px;
                            border: 1px solid blue; background: lightblue;">';
    $layout02 = '</div>';
 
 
    // <!--[%ARTICLE_LIST:XX%]--> esetzen
    $result = '';
 
    if (isset($article_id[1][0])) {   // TAG vorhanden?
 
    //    dumpVar($article_id[1]);
 
 
        // DB Abfrage
        $result = my_dbquery ($article_id[1]);
        // dumpVar($result);
 
        // Array: Mache country_id zu Schluessel
        foreach ( $result as $key=>$value) {
            $trans[$value['country_id']] = $result[$key];
        }
 
        //dumpVar($trans);
 
        // Gebe Resultat aus
        foreach ( $article_id[1] as $key=>$value) {
 
            // Gebe das Resultat aus wenn vorhanden
            if (array_key_exists($value, $trans)) {
 
                $result  = 'ID:'       .$trans[$value]['country_id'].' - ';
                $result .= 'ISO3:'     .$trans[$value]['country_iso3'].' - ';
                $result .= 'Name:'     .$trans[$value]['country_name'].' - ';
                $result .= 'Continent:'.$trans[$value]['country_continent'].LF;
            }
            else $result = $no_result;
 
//            $result = 'Ergebnis zu DB-Abfrage mit der Nr. '.$value. '  //  Result for db query with the No '.$value.LF;
            $content['all'] = str_replace('<!--[%ARTICLE_LIST:'.$value.'%]-->',$layout01.$result.$layout02,$content['all']);
        }
 
 
    }
}
 
// Artikel-Detail
// -----------------------------------------------------------------------------
// TEST Artikel-Detailmodus: Artikel mit Kommentar ist mit <!--ARTICLE_COMMENT--> versehen
// In der Vorlage an der entsprechenden Stelle den Platzhalter: <!--%ARTICLE_COMMENT%-->
 
// Nur wenn in Artikel-Detailansicht und Artikel als Kommentarartikel gekennzeichnet
if( !empty($aktion[4])  AND !( strpos($content["all"],'<!--ARTICLE_COMMENT-->') === false) ) {
 
    // ---- Custom var
    $no_result = 'Kein Eintrag gefunden!!';
 
    //Layout fuer Ergebnis setzen
    $layout01  = '<hr>';
    $layout01 .= '<div style="padding: 5px; margin: 15px 0 15px 0; width:570px;
                    border: 1px solid blue; background: lightblue;">';
 
    $layout02  = '</div>';
    $layout02 .= '<hr>';
 
    // ......
    // Den/die Werte aus der DB holen mit
    $article_id = $aktion[1];
 
    //.... DB-Abfrage
 
    // Ergebnis einfuegen
 
//    $result .= '- Ergebnis aus DBabfrage fuer Artikel-ID='.$article_id. '<br />'.LF.
//                '- Result from db query for the article ID '.$article_id.LF;
 
 
    $trans = my_dbquery ($article_id);
 
    // dumpVar($trans);
 
    // Gebe das Resultat aus wenn vorhanden
 
    if (array_key_exists('0', $trans)) {
 
        $result  = 'ID:'       .$trans[0]['country_id'].' - ';
        $result .= 'ISO3:'     .$trans[0]['country_iso3'].' - ';
        $result .= 'Name:'     .$trans[0]['country_name'].' - ';
        $result .= 'Continent:'.$trans[0]['country_continent'].LF;
    }
    else $result = $no_result;
 
    $content["all"] = str_replace('<!--%ARTICLE_COMMENT%-->', $layout01.$result.$layout02, $content["all"]);
 
 
}
 
?>

Knut Heermann (flip-flop) 2010/10/25 23:39