1

I'm trying to print an array on smarty template which I made on php. And I get this error :

Notice: Array to string conversion in C:\wamp\www\smarty\templates_c\e43b807af9cc8df7d350c3baf9e47f167c9520a0.file.index.tpl.php on line 87

I couldn't figure how to solve it. Can you tell me where is the error?

Here is the part where I make the array in php

function get_db_results()
{
    $mysql = "SELECT COUNT(*) FROM question";
    $myresult = mysql_query($mysql);
    $myr = mysql_fetch_row($myresult);
    SmartyPaginate::setTotal($myr[0]);
    $index = SmartyPaginate::getCurrentIndex();
    $limit =SmartyPaginate::getLimit();
    $rsql = "SELECT question_title FROM question ORDER BY question_id DESC LIMIT $index, $limit ";

    $rresult = mysql_query($rsql);
    //$column = array();

    while($rrow = mysql_fetch_array($rresult , MYSQL_ASSOC))
    {
        $column[]=$rrow;

    }


    return $column;
}
$smarty->assign('results', get_db_results());

And here is the smart part :

{section name=res loop=$results}
    {$results[res]}
{/section}

2 Answers 2

2

Change {$results[res]} to {$results.res} Assuming you have key "res" in your array

By looking at your function I guess it should be :

{$results.question_title}

Here is the solution :

{foreach from=$results item=results name=results}
    {$results.question_title}
{/foreach}
Sign up to request clarification or add additional context in comments.

9 Comments

At this time I got this : Undefined index: question_title in C:\wamp\www\smarty\templates_
can you add your get_db_results() functions out put, i can explain based on that
( ! ) Notice: Array to string conversion in C:\wamp\www\smarty\myindex.php on line 57 -- Line 57 is : echo get_db_results(); -- Thanks
Try this : echo "<pre>";print_r(get_db_results());
Array ( [0] => Array ( [question_title] => who is this? ) [1] => Array ( [question_title] => soru 23823 ) [2] => Array ( [question_title] => Deneme 1-2? ) [3] => Array ( [question_title] => Yeni görünüm? ) [4] => Array ( [question_title] => which flower? ) [5] => Array ( [question_title] => what is this? ) [6] => Array ( [question_title] => saat şimdi? ) .....
|
2

First delete all cache file from C:\wamp\www\smarty\templates_c\, after that try like this {$results[res].YOUR FIELD NAME} because you used {section}{/section}.

e.g. {$results[res].question_title}

Hope help you.

3 Comments

This time I get this : ( ! ) Notice: Undefined index: res in C:\wamp\www\smarty\templates_c\e43b807af9cc8df7d350c3baf9e47f167c9520a0.file.index.tpl.php on line 87
First print get_db_results(), in that you get the data?
@AdvaitAmin : +1 for your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.