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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。