Zalozmy ze mamy takie szablon:
Teraz w jego srodku chcialbym wyswietlic tabelke ktora zawiera rekordy z MySQL, np. 5 wierszy? W jaki sposob Wy to rozwiazujecie?
P.S. Chodzi o jak najprostsze rozwiazania:)
<? $result = mysql_query("SELECT * FROM news") or die("BAD QUERY"); ?> <table border="1"> <tr> <!-- środek początek --> <td> <table border="1"> <? while($rek = mysql_fetch_array($result)) { ?> <tr> <td> <? echo $rek[0]; //nagłówek rekordu, np. data ?> </td> </tr> <tr> <td> <? echo $rek[1]; //rekord ?> </td> </tr> <? } ?> </table> </td> <!-- środek koniec --> </tr> </table>
<?php $tpl -> setValue( 'nazwa', 'Jakas tam wprowadzona nazwa' ); $tpl -> Display(); ?>
<?php // AdoDB $rs = $db->GetAll(\"SELECT...\"); ?>
Jak to mam przerobic zeby dzialalo poprawnie, tak jak bym chcial?
<?php class Templates { function setFile() //... function setValue( $name, $value ) { { // ... } else { $this -> array_name[] = \"{\" . $name . \"}\"; $this -> array_value[] = $value; } } function Display() { } } $query = \"SELECT * FROM calendar LIMIT 15\"; { $a[] = $row['day']; } $tpl = new Templates; $tpl -> setFile( 'index.html', 'tpl' ); $tpl -> setValue( 'title', 'Witamy!' ); $tpl -> setValue( 'a', $a ); $tpl -> Display(); ?>
<?php class Templates { function setFile() //... function setValue( $name, $value ) { $this->vars[$name] = $value; return true; } function Display() { // Patrz niżej } } ?>
Szablon:
<?php class Templates { //... function setSection( $name, $values ) { // Ta funkcja dziala narazie tylko i wylacznie z ponizszym szablonem :) // Rozbudowa pozniej... $new_line = ''; $a = \"{\" . $name . \"}\"; foreach( $values as $value ) { } $this -> set_file = preg_replace( '/({@wiersze})(.*?)({/@wiersze})/is', $new_line, $this -> set_file ); } //... } { $match_home[] = $row['match_home']; $match_guest[] = $row['match_guest']; } $tpl = new Templates; $tpl -> setFile( 'index.html', 'tpl' ); $tpl -> setSection( 'lewa', $match_home ); $tpl -> setSection( 'prawa', $match_guest ); $tpl -> Display(); ?>
<?php $tpl = new TemplatePower( \"szablon.tpl\" ); $tpl->prepare(); { $tpl->newBlock(\"blok\"); $tpl->assign(\"wiersz\", $odp['pole']); } $tpl->printToScreen(); ?>
<?php $ctrl = 0; { { } { } else { $ctrl = 1; } } if( $ctrl == 0 ) { //formularz } ?>
<?php $ctrl = 0; { { $err=\"Wpisz nick!\"; } { $err\"Wpisz haslo!\"; } else { $ctrl = 1; $err=\"Okey!\"; } } if( $ctrl == 0 ) { //formularz } ?>