|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Snowtuss Smarty n00b
Joined: 01 Dec 2016 Posts: 1
|
Posted: Fri Dec 02, 2016 9:47 am Post subject: Have a problem using Foreach and PDO |
|
|
Hello , this is my loop in the smarty file :
Code: | {foreach $reponse as $r}
{assign var=resultat value=0}
<div align="center"><h2><a href="viewpost.php?id={$r.ID}">{$r.TITRE}</a></h2><img src="img/{$r.ID}.jpg" height="300" width="250"><BR>{$r.TEXTE}<BR>{$r.DATE}<BR>
{if $connecte == true}
<a href="article.php?id={$r.ID}">Modifier</a>
{/if}
{foreach $sth as $s}
{if $s.id_article == $r.ID}
{assign var=resultat value=$resultat+1}
{/if}
{/foreach}
{$resultat}
<BR><BR></div>
{/foreach}
<div class ="pagination">
<ul>
<li><a>Page : </a></li> |
And here is the associated code in my php file
Code: | $reponse = $db->query("SELECT ID,TEXTE,TITRE,DATE FROM articles LIMIT $debut , $nbarticlepage");
$reponse->setFetchMode(PDO::FETCH_LAZY);
$smarty->assign('reponse',$reponse);
$smarty->assign('connecte',$connecte);
$sth = $db->query("SELECT id_article FROM commentaires");
$sth->setFetchMode(PDO::FETCH_LAZY);
$smarty->assign('sth',$sth);
$smarty->debugging = true;
include 'inc/menu.inc.php';
$smarty->display('Index.tpl'); |
Basically i'm creating my own blog , and in each page i list 2 articles and i want to know how many comments are in each post but it only works for the first article in each page and the second article shows the value "0" so i assume he don't enter the if loop the second time but i don't know where is the error because logically everything seems alright ... |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Fri Dec 02, 2016 5:42 pm Post subject: |
|
|
First, use prepared statements already. Your use of PDO is no better than using php_mysql.
Code: | <?php
$db = new \PDO('mysql:host=...;dbname=...;charset=utf8mb4', $user, $pass, [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_EMULATE_PREPARES => false
]);
$reponse = $db->prepare("SELECT ID,TEXTE,TITRE,DATE FROM articles LIMIT ?, ?");
$reponse->execute([$debut, $nbarticlepage]);
$smarty->assign('reponse', $reponse); |
Second, what [censored] Quote: | include 'inc/menu.inc.php'; | is supposed to mean?
Remove it and use Smarty already, since you are using it.
Third, what the hell is Quote: | {foreach $sth as $s}
{if $s.id_article == $r.ID}
{assign var=resultat value=$resultat+1}
{/if}
{/foreach} | supposed to mean?
You don't know how to use JOIN in SQL? Or how to use aggregate functions, like COUNT() ?
In short, fix your code, it is badly written and unlikely to get you anywhere. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|