3

I'm new to smarty. I tried to loop a mysql query.

My view.php:

require 'libs/smarty.class.php';
$smarty = new Smarty;
include("connection.inc.php");

$query = "SELECT * FROM quickcount group by regency";
$result= mysql_db_query($db, $query, $connection) or
die("query error!");


while($res=mysql_fetch_array($sql))
{
    $data[]=$res;
}

$smarty->assign('qc',$data);
$smarty->display('view.tpl');
?>

My view.tpl:

{section name=i loop=$qc}
<tr>
<td>{$qc[i].regency}</td>          
<td>{$qc[i].vote1}</td>
<td>{$qc[i].vote2}</td>
<td>{$qc[i].vote3}</td>
<td>{$qc[i].vote4}</td>
</tr>
{/section}

Those two files above worked well, but the problem came up when I wanted to add one more column in my output table. Let's say the new column is "total" (vote1+vote2+vote3+vote4).

I tried this in my view.php and failed:

$query = "SELECT * FROM quickcount group by regency";
$result= mysql_db_query($db, $query, $connection) or
die("query error!");

while($res=mysql_fetch_array($sql))
{
    $data[]=$res;
    $vote1 = $res[3];
    $vote2 = $res[4];
    $vote3 = $res[5];
    $vote4 = $res[6];

    $totals=$vote1+$vote2+$vote3+$vote4;
    $total=array($totals);
}

$smarty->assign('qc',$data);
$smarty->assign('total',$total);
$smarty->display('view.tpl');

I want to add percentages as well.

Can anyone help?

Michael Roland
  • 39,663
  • 10
  • 99
  • 206
Daddi Al Amoudi
  • 770
  • 1
  • 5
  • 16
  • 2
    You **really** should not be writing code that relies on `mysql_` functions anymore. The MySQL extension has been deprecated for years (ever notice these red warning boxes in the documentation?) and is about to be dropped in the upcoming PHP7 release, which can be released any time now. Also see [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). On an up-to-date server, this code has a life span of about a month, after which it will **stop working**. – Oldskool Nov 24 '15 at 19:42
  • What do you mean by "failed"? It returns an error, doesn't show anything, the cell is empty,....? – Borgtex Nov 25 '15 at 09:15
  • It shows only one row (the last row) – Daddi Al Amoudi Nov 25 '15 at 12:54

0 Answers0