0

I know that's common problem with display data in html with smarty. I just started today this morning using smarty, and was checking net /here also/ for solution, but after 3 hours I gave up.

I have error Array to string conversion and have no other ideas what to do. I know that was on forum,but I checked them.

<?php
require_once('Smarty.class.php');
//include_once('project_config.php');

$link    = mysqli_connect("localhost", "test", "test", "tcg_test");
$smarty  = new Smarty();


$q = "SELECT person, id FROM person
      WHERE person.type_id = 
      (
          SELECT type_id FROM tcg_test.type 
          WHERE type_key = 'company.owner'
      )";

if($result = mysqli_query($link, $q))
{
    printf("Select returned %d rows.\n", mysqli_num_rows($result));
}

$rrows     = array();
while($row = mysqli_fetch_row($result))
{
     $rrows[] = $row;
}

$rows_string = implode( "," , $rrows);
$smarty->assign('rrows',$rows_string);

$smarty->display('compare.tpl');
mysqli_free_result($result);

?>

HTML

        {foreach $rrows as $row}
            {$row}
        {/foreach}

And $rrows

array(4) {
  [0]=>
  array(2) {
    [0]=>
    string(33) "number 1 ....."
    [1]=>
    string(3) "906"
  }
  [1]=>
  array(2) {
    [0]=>
    string(19) "number 2...."
    [1]=>
    string(3) "906"
  }
  [2]=>
  array(2) {
    [0]=>
    string(29) "number 3 ....."
    [1]=>
    string(3) "906"
  }
  [3]=>
  array(2) {
    [0]=>
    string(25) "number 4....."
    [1]=>
    string(3) "906"
  }
}

I came to this:

            {section name=record loop=$rrows}
                {foreach from=$rrows[record] item=entry key=name}

                    <option> {$entry} </option>
                 {/foreach}
            {/section}

but i dont need number 906

4
  • stop using mysqli_* function, they are deprecated since php5.5 and removed from php7.0 (prevent migration) Commented May 11, 2017 at 0:34
  • 2
    @MTroy mysql_* functions have been depreciated, not mysqli_*. Commented May 11, 2017 at 6:50
  • Why do you assign rrows a string, but then use it as array in foreach? Commented May 11, 2017 at 7:07
  • So I should just pass array to template and then use foreach? Commented May 11, 2017 at 7:13

1 Answer 1

1

You cannot implode because $rrows is multi dimensional array

$rows_string = implode( "," , $rrows);

Option 1 :

You may remove this

$rows_string = implode( "," , $rrows);
$smarty->assign('rrows',$rows_string);

and just pass array to your html

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

and you may try using implode in your html So.. it should be something like this

{foreach $rrows as $row}
    {{ ','|implode:$row }}
{/foreach}

Option 2 :

If you not using $rrows somewhere else you may do implode in your while loop

$rrows     = array();
while($row = mysqli_fetch_row($result))
{
     $rrows[] = $row;
}

$rows_string = implode( "," , $rrows);
$smarty->assign('rrows',$rows_string);

into something like this

$rrows     = array();
while($row = mysqli_fetch_row($result))
{
     $rrows[] = implode( "," , $row);
}

$smarty->assign('rrows', $rrows);
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, got it, and if ii want just to have in template "number 1 number 2 number 3", without commas? i came to this what I just edited above, but that gives me number 906 as well.
Use 'if condition' in foreach

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.