Forum

Thread tagged as: Question, Configuration

write php in content templates ?

hello,

i have a page with one editable region ('teachers'), the region is set to "Allow multiple items"

i created a new HTML template ('teacher-box') and saved it in templates/content

everything works fine so far, but in addion to the name, the profile etc i need to display some php output for each teacher that comes from a database

like this

<?php
        $kuerzel = "#";
        $sql = "SELECT kurse.* FROM kurse WHERE LehrID = '$kuerzel' ORDER BY Wochentag, Uhrzeit_Von, Uhrzeit_Bis";
        $tage = array("Sonntag","Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag");
        $res = mysql_query( $sql );
        while ($row=mysql_fetch_object($res)) {
            echo "<h5>";
            echo $tage[$row->Wochentag] . " ";
            echo " " . date("H:i", strtotime($row->Uhrzeit_Von)) . " ";
            echo " " . $row->Kurstitel;
            echo "</h5>";
        }
        mysql_free_result($res);
?>

is there any way to do this ? how could I add this to my template, and how could i pass a value to the variable $kuerzel ?

thank you!

Katja Schwarz

Katja Schwarz 0 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You can use the each callback option:

perch_content_custom('teachers', [
    'template' => 'teacher-box.html',
    'each' => function($item) {
        $kuerzel = $item['id_of_field'];
        $sql = "SELECT kurse.* FROM kurse WHERE LehrID = '$kuerzel' ORDER BY Wochentag, Uhrzeit_Von, Uhrzeit_Bis";
        $tage = array("Sonntag","Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag");
        $res = mysql_query( $sql );
        $html = '';
        while ($row=mysql_fetch_object($res)) {
            $html .= "<h5>";
            $html .= $tage[$row->Wochentag] . " ";
            $html .= " " . date("H:i", strtotime($row->Uhrzeit_Von)) . " ";
            $html .= " " . $row->Kurstitel;
            $html .= "</h5>";
        }
        mysql_free_result($res);

        $item['courses_html'] = $html;
        return $item;
    }
]);

Update $item['id_of_field'] to be the ID of the field you need the data from.

Then in your template, you can use this to output the HTML you generated:

 <perch:content id="courses_html" encode="false" />

(You should also take steps to escape $kuerzel in your SQL query to protect against injection.)

thank you, this looks good

but it does not work ...

i put the first bit of code in my PHP file, the web-page where i want do display the teachers the second bit of code is in my template (teacher-box)

in dreamweaver, i got red markers in the first and last line, can it be the brackets [ ] ?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, it could be. Swap those for array() if you're using PHP 5.3.

i worked it out:

perch_content_custom('teachers', array(

thank you, best regards, katja

Drew McLellan

Drew McLellan 2638 points
Perch Support

Great. PHP 5.3 is pretty old, so you should think about upgrading if possible. You'll get immediate speed improvements.

hello again,

there is a new / another problem: after i cleared it up a bit, the page does not appear in the pages list (NEW) anymore

i did visit the page in my browser, though

Drew McLellan

Drew McLellan 2638 points
Perch Support

That's because you've switched from perch_content() to perch_content_custom().

You can either do this:

$null = perch_content('teachers', true);

to create the region as before, or you can use perch_content_create() to set it up in code.

i try it, like this ?

$null = perch_content('teachers', true); perch_content_custom('teachers', array( ...

one more line at the very start, i do not have to replace the array ?

Drew McLellan

Drew McLellan 2638 points
Perch Support

That's right.

great, thank you !