PHP Smarty数组到字符串

时间:2017-05-10 21:33:56

标签: php html smarty

我知道在html中使用smarty显示数据的常见问题。我今天早上刚刚开始使用smarty,并且正在检查网/这里/解决方案,但是3小时后我放弃了。

我有错误数组到字符串转换,没有其他想法做什么。我知道那是在论坛上,但我检查了它们。

const Form = ({ SubmitButtonComponent, children }) =>
  <form>
    {childrem}
    {SubmitButtonComponent ? <SubmitButtonComponent /> : <OtherComponent />}
  </form>

HTML

<?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);

?>

和$ rrows

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

我来到这里:

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"
  }
}

但我不需要号码906

1 个答案:

答案 0 :(得分:0)

你不能内爆,因为$ rrows是多维数组

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

选项1:

您可以删除此

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

并将数组传递给你的html

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

你可以尝试在你的HTML中使用implode 所以..它应该是这样的

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

选项2:

如果你不在其他地方使用$ rrows,你可能会在你的while循环中内爆

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

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

这样的事情

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

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