Forum
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!
You can use the
each
callback option: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:
(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 [ ] ?
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
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
That's because you've switched from
perch_content()
toperch_content_custom()
.You can either do this:
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 ?
That's right.
great, thank you !