【问题标题】:Can't load a values from $wpdb无法从 $wpdb 加载值
【发布时间】:2019-01-26 22:51:47
【问题描述】:

我在 wordpress db 中有这些值:

                    global $wpdb;

                    $sql = "SELECT `post_excerpt` FROM `wp_posts` WHERE (`wp_posts`.`post_excerpt` LIKE 'fasad_%') ORDER BY `wp_posts`.`post_excerpt` ASC";
                    $materials = $wpdb->get_results($sql) or die(mysql_error());
                    foreach ($materials as $material) {

                        $fasads_kitchen = $material->post_excerpt;
                        echo $fasads_kitchen;                              
                    }    

没关系。显示值。但在那之后我需要像这样执行一个 foreach 函数:

                     foreach ($fasads_kitchen as $fasad_arg) {                            
                        // an error is here
                        if( have_rows($fasad_arg) ): ?>
                               while ( have_rows($fasad_arg) ) : the_row();
                               //something
                    }

但是有一个错误:为 foreach() 提供的参数无效。 我尝试使用

foreach ((array)$fasads_kitchen as $fasad_arg) {

但调试器说它不是数组。

它是一个字符串变量还是什么?如何解决?

如果我正在使用

$fasads_kitchen = array('fasad_plastic','fasad_mdf') {}

它正在工作,但我需要从 wpdb 加载这些值。

【问题讨论】:

    标签: php arrays wordpress foreach


    【解决方案1】:

    你在使用时正在创建一个字符串

    $fasads_kitchen = $material->post_excerpt;
    

    $fasads_kitchen 需要是一个数组:

    $fasads_kitchen = array();    
    foreach ($materials as $material) {
        array_push($fasads_kitchen, $material->post_excerpt); 
        // or use one of the many ways to add to an array                         
    } 
    

    然后就可以循环遍历$fasads_kitchen

    【讨论】:

    • 是的,我不知道如何将字符串转换为数组。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多