0

So this is my foreach-loop

foreach($statusMessageResult as $row){
    $row['username']=$db->getUsername($db->getUserNameById($row['posterID']));
    $results[] = $row;
    echo "Status from: " . $row['username'] . " ID: " . $row['statusID'] . "<br>" . $row['dateTime'];
}

Which works without problems. But now I want to have the same in smarty. So I thought I'm going to do this in the .php file

$smarty->assign('results', $results);

and this in the .html file

{foreach from=$results item=statusID}
    id: {$statusID}<br>
{/foreach}

But it tells me

Notice: Array to string conversion in F:\xampp\htdocs\FinalYear\smarty\templates_c\ab89063f543bf0a8fe20c45b89aad63b616cd7c5.file.home.html.php on line 86
Array

Well the problem seems clear: I have an array and want to use it as a string, whiich isn't allowed. But how can I solve it ?

3 Answers 3

2

Try this :

{foreach from=$results item=result}
    id: {$result.statusID}<br>
{/foreach}

If you want user name : {$result.username}

Ref: http://www.smarty.net/docsv2/en/language.function.foreach

Sign up to request clarification or add additional context in comments.

2 Comments

id: {$result.statusID|stripslashes}
@DeppG : Is that really needed ? It will be a int field in Db usually rite?
0

statusID is an array, get value by key name like that:

{foreach from=$results item=statusID}
    id: {$statusID.username}<br>
{/foreach}

2 Comments

id: {username} makes no sense to me, I think he wants the id ;)
no problem: {foreach from=$results item=user} id: {$user.statusID}<br> {/foreach}
-1

$statusID seems to be an array :)

changing

{foreach from=$results item=statusID}
    id: {$statusID}<br>
{/foreach}

to

{foreach from=$results item=statusID}
    id: {$statusID['statusID']}<br>
{/foreach}

should solve it

Comments

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.