dongzhou4727
2014-12-22 05:41
浏览 53
已采纳

试图在Smarty中获取数组表和数据库

I Have an array of Databases and Tables from MySQL. I Can't Seem to Get a List of Tables under each database in Smarty? I Have tried multiple foreach statements and none seem to work. The Database Name shows correct but the tables do not. I appreciate all of the help.

My PHP:

$conn = connect();

$db_sql = $conn->query("SHOW DATABASES;");
$dbs = array();
while ($db = $db_sql->fetch_array(MYSQLI_NUM))
{
    $db_name = $db[0];
    $dbs[] = $db_name;
}

$tables = array();
foreach ($dbs as $db => $value) {
    $table_sql = $conn->query("SHOW TABLES FROM $value");
    while ($table_ = $table_sql->fetch_array(MYSQLI_NUM))
    {
        $tb_name = $table_[0];
        $tables[$value]['Tables'][] = $tb_name;
    }
}

$smarty->assign("dbs", $tables);

My Smarty:

 {foreach from=$dbs key=db item=value}
                    <li>
                        <a class="tree-toggler nav-header" href="#" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-th-list"></span> {$db}</a>
                        <ul class="nav nav-list tree">
                        {foreach from=$value->Tables  item=$table}
                            <li><a href="#">{$table}</a></li>
                        {/foreach}
                        </ul>
                    </li>
                {/foreach}
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanjian5059 2014-12-22 09:35
    已采纳

    The problem is with your smarty code in the second foreach loop

    Replace

    This line of your code - {foreach from=$value->Tables item=$table} WITH This line - {foreach from=$value.Tables item=table}

    And it will be working

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题