I don't know TemplatePower at all, but it looks like the template engine gets all blocks with the given name at once and renders them. If this is true you probably would need to construct the complete archive for a certain date in one block.
Structuring news overview
-
29-06-2022 - |
Frage
I need some help with this problem, I'm having a total blackout, and can't figure out what I'm doing wrong.
I use TemplatePower as template engine. I'm tring to display my news archiv like this:
23-09-2013
> Update nieuwssectie
21-09-2013
> Nog toe te voegen
20-09-2013
> Nieuwe functie nieuws
> Eerste nieuwsbericht
But this is what I get
23-09-2013
21-09-2013
20-09-2013
> Update nieuwssectie
> Nog toe te voegen
> Nieuwe functie nieuws
> Eerste nieuwsbericht
This is the template:
<!-- START BLOCK : newsArchiv -->
<h6 class="newsItem">Ons nieuwsarchief:</h6>
<ul class="newsArchiv">
<!-- START BLOCK : newsArchivDateRow -->
<li class="newsArchivRow"><span>{date}</span></li>
<!-- END BLOCK : newsArchivDateRow -->
<!-- START BLOCK : newsArchivItemRow -->
<li><a href="pagina/nieuws/{url}/">{title}</a></li>
<!-- END BLOCK : newsArchivItemRow -->
</ul>
<!-- END BLOCK : newsArchiv -->
And the PHP
<?
$tpl2->newBlock( 'newsArchiv');
$item_sql = $sql->query( "SELECT * FROM news ORDER BY id DESC" );
if( $sql->num( $item_sql ) > 0 )
{
$date1 = "";
while( $item = $sql->fetch( $item_sql ) )
{
$date2 = date( "d-m-Y", $item['date'] );
if( $date2 != $date1 )
{
$tpl2->newBlock( 'newsArchivDateRow');
$tpl2->assign( "date", $date2 );
$date1 = $date2;
}
$tpl2->newBlock( 'newsArchivItemRow');
$tpl2->assign( array(
"url" => $item['url'],
"title" => $item['title']
));
}
}
?>
I just know there is a logic solution, I just can't see it and need a push in the right direction.
[EDIT]
Based on Bart's answer I constructed the code like this and fixed the problem.
Template:
<!-- START BLOCK : newsArchiv -->
<h6 class="newsItem">Ons nieuwsarchief:</h6>
<ul class="newsArchiv">
<!-- START BLOCK : newsArchivRow -->
<li class="newsArchivRow"><span>{date}</span></li>
<!-- START BLOCK : newsArchivItemRow -->
<li><a href="pagina/nieuws/{url}/">{title}</a></li>
<!-- END BLOCK : newsArchivItemRow -->
<!-- END BLOCK : newsArchivRow -->
</ul>
<!-- END BLOCK : newsArchiv -->
PHP:
<?php
$tpl2->newBlock( 'newsArchiv');
$item_sql = $sql->query( "SELECT * FROM news ORDER BY id DESC" );
if( $sql->num( $item_sql ) > 0 )
{
$itemsArr = array();
while( $itemdata = $sql->fetch( $item_sql ) )
$itemsArr[ date( "d-m-Y", $itemdata['date'] ) ][] = array( "url"=> $itemdata['url'], "title" => $itemdata['url'] );
foreach( $itemsArr as $date => $items )
{
$tpl2->newBlock( 'newsArchivRow');
$tpl2->assign( "date", $date );
foreach( $items as $item )
{
$tpl2->newBlock( 'newsArchivItemRow');
$tpl2->assign( array(
"url" => $item['url'],
"title" => $item['title']
));
}
}
}
?>
Lösung
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow