0

Hello I have the Array to string conversion erro with this code:

index.php:

Code:

$hoArr = array(1,2,3); 
foreach ($hoArr as $hid) { 
    $mysqli = mysqli_connect("localhost", "root", "", "zabbixtest"); 
    if (!$mysqli) { 
        die("Connection failed: " . mysqli_connect_error()); 
    } 
    $sql = "SELECT errors_from FROM hosts WHERE hostid = '$hid'"; 
    $re = mysqli_query($mysqli, $sql); 
    while ($row = mysqli_fetch_assoc($re)) { 
        $query_times[] = $row['errors_from']; 


    } 

     mysqli_free_result($re); 
    mysqli_close($mysqli); 


} 
 $smarty->assign('query_times',$query_times);

and index.tpl:

Code:

<p>{$query_times}</p>

I have no idea whats wrong with it..

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345

2 Answers2

0

You try to assign an array with $smarty->assign but you can only assign a string.

Use this instead:

$query_string = implode ( "," , $query_times );
$smarty->assign('query_times',$query_string);
Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
  • Thanks for the answer but it throws the same error :/ –  Sep 22 '15 at 09:55
  • Sorry i tried it again after deleting the cache now it works thank you very much! –  Sep 22 '15 at 09:57
  • But the target of this was to get this records into a table like this: {$query_times} which is now not working, because its one string do you have any idea to solve that? –  Sep 22 '15 at 10:00
  • Use this `$query_string = implode ( "" , $query_times );` – Niki van Stein Sep 22 '15 at 10:05
  • I tried that before but that gives me this output: 144282508814422222081443333376 –  Sep 22 '15 at 10:11
  • Yes you are right. Check this answer instead: http://stackoverflow.com/questions/2244319/how-to-assign-an-array-within-a-smarty-template-file – Niki van Stein Sep 22 '15 at 11:12
0

I have a simple solution:

index.php:

$query_string = implode ("</td><td>", $query_times );

index.html:

<table>

    <td>{$query_times}</td>
</table>

Now it's in a table :)

  • sure it works, because u get this from the Query string: record1record2 and in the template you add the start and end tag. –  Sep 23 '15 at 08:54
  • I see.. but it is exactly the same as I said in my comment and you said it did not work.. anyways glad it works for you :) – Niki van Stein Sep 23 '15 at 19:38