【问题标题】:Warning Invalid Argument for foreach()警告 foreach() 的参数无效
【发布时间】:2015-06-09 17:22:01
【问题描述】:

好的,我最近购买了一个名为 WPArcade 的 wordpress 插件,但是我遇到了一些问题。特别是我不能让我的游戏像他们想象的那样喂食。当我尝试从 Kongregate 提供游戏时,它显示 0 个游戏已找到,但也给了我这个错误:

警告:在 C:\Program Files (x86)\Ampps\www\wp\wp-content\plugins\wparcade-plugin\feeds\kongregate\kongregate.php 第 113 行中为 foreach() 提供的参数无效

我正在本地服务器上测试该站点,这就是它位于 C 驱动器中的原因。

Konggregates 提要:http://www.kongregate.com/games_for_your_site.xml

插件中的每个发布者都会发生这种情况,但不会向我发出警告的发布者除外。

第 113 到 155 行:Kongregate

$xml = simplexml_load_string($get_kongregate_feed);

foreach($xml->game as $game) {  
    $id = intval($game->id);
    $name = mysql_real_escape_string($game->title);
    $check_id = $wpdb->get_var("SELECT id FROM ".$wpdb->prefix."wparcade_kongregate_games WHERE id = '$id'");
    $count = mysql_result(mysql_query("SELECT COUNT(*) as newgames FROM ".$wpdb->prefix."wparcade_kongregate_games WHERE id = '$check_id'"),0);

    if($count !== '1') {
        $name = mysql_real_escape_string($game->title);
        $slug = mysql_real_escape_string($game->title);
        $slug = wparcade_stringforslug($slug);
        $category = mysql_real_escape_string($game->category);
        $category = wparcade_strstr_before($category, " & ");
        if(!$category) $category = mysql_real_escape_string($game->category);
        $categories = mysql_real_escape_string($game->category);
        $categories = str_replace(' & ', ', ', $categories);
        if(!$categories) $categories = mysql_real_escape_string($game->category);
        $description = mysql_real_escape_string($game->description);
        $instructions = mysql_real_escape_string($game->instructions);
        $thumbnail_url = mysql_real_escape_string($game->thumbnail);
        $swf_url = mysql_real_escape_string($game->flash_file);
        $width = $game->width;
        $height = $game->height;
        $tags = strtolower($categories);
        $author = mysql_real_escape_string($game->developer_name);
        $wparcade_kongregate_feed_game_status = $options['kongregate_feed_game_status'];
        $status = $wparcade_kongregate_feed_game_status;
        $feedsinglecat_id = $options['kongregate_feed_single_cat'];
        $feedcategory = get_cat_name($feedsinglecat_id);
        if($feedsinglecat_id !== 'none') { 
            $categories = $feedcategory;
            $category = $feedcategory;
        }

        $sql = "INSERT INTO ".$wpdb->prefix."wparcade_kongregate_games (id, name, slug, description, thumbnail_url, swf_url, width, height, author, categories, category, tags, instructions, status) VALUES ('$id', '$name', '$slug', '$description', '$thumbnail_url', '$swf_url', '$width', '$height', '$author', '$categories', '$category', '$tags', '$instructions', '$status')";

        $list .= '<li class="wparcade_new_games""><strong>'.$name.'</strong></li>';
        $wpdb->query($sql);
        $inserted++;

    } else {
        $list .= '<li class="wparcade_old_games"><strong>'.$name.'</strong></li>';
    }
}

以防万一,这是唯一有效的提要中的代码:

$xml = simplexml_load_string($get_scirra_feed);

foreach($xml->game as $game) {  
    $id = intval($game->gameid);
    $name = mysql_real_escape_string($game->name);
    $count = mysql_result(mysql_query("SELECT COUNT(*) as newgames FROM ".$wpdb->prefix."wparcade_scirra_games WHERE id = '$id'"),0);
    $category = mysql_real_escape_string($game->category);
    $slug = mysql_real_escape_string($game->name);
    $slug = wparcade_stringforslug($slug);
    $check_for = $wpdb->get_var("SELECT name FROM ".$wpdb->prefix."wparcade_scirra_games WHERE slug = '$slug'");

    if($count !== '1' && $category !== 'Example' && !$check_for) {
        $name = mysql_real_escape_string($game->name);
        $slug = mysql_real_escape_string($game->name);
        $slug = wparcade_stringforslug($slug);
        $category = mysql_real_escape_string($game->category);
        $category = str_replace('Shooter', 'Shooting', $category);
        $category = str_replace('Defence', 'Defense', $category);
        $category = str_replace('Puzzle', 'Puzzles', $category);
        $category = str_replace('Rotary', 'Arcade, Rotary', $category);
        $categories = $category;
        $description = strip_tags(mysql_real_escape_string($game->description));
        $instructions = strip_tags(mysql_real_escape_string($game->instructions));
        $thumbnail_url = $game->images->small;
        $thumbnail_large_url = $game->images->medium;
        $screen1_url = $game->images->big;
        $embed_url = mysql_real_escape_string($game->embedurl);
        $embed_url = str_replace('http://', '//', $embed_url);
        $embed_url = str_replace('//', 'http://', $embed_url);
        $width = $game->width;
        $height = $game->height;
        $tags = strtolower($categories);
        $author = mysql_real_escape_string($game->author->username);
        $wparcade_scirra_feed_game_status = $options['scirra_feed_game_status'];
        $status = $wparcade_scirra_feed_game_status;
        $feedsinglecat_id = $options['scirra_feed_single_cat'];
        $feedcategory = get_cat_name($feedsinglecat_id);
        if($feedsinglecat_id !== 'none') { 
            $categories = $feedcategory;
            $category = $feedcategory;
        }

        $sql = "INSERT INTO ".$wpdb->prefix."wparcade_scirra_games (id, name, slug, description, thumbnail_url, thumbnail_large_url, screen1_url, embed_url, width, height, author, categories, category, tags, instructions, status) VALUES ('$id', '$name', '$slug', '$description','$thumbnail_url', '$thumbnail_large_url', '$screen1_url', '$embed_url', '$width', '$height', '$author', '$categories', '$category', '$tags', '$instructions', '$status')";

        $list .= '<li class="wparcade_new_games""><strong>'.$name.'</strong></li>';
        $wpdb->query($sql);
        $inserted++;

    } else {
        $list .= '<li class="wparcade_old_games"><strong>'.$name.'</strong></li>';
    }
}

提前感谢您的帮助。

附:我在插件的支持网站上问过,但已经快一周没有答案了。

【问题讨论】:

    标签: php wordpress plugins foreach


    【解决方案1】:

    这里的问题是,对于 Kongregate,$xml->game 不是可迭代对象(如数组)。最简单的调试方法是在第 113 行尝试插入如下内容:

    print_r($xml->game); 
    //or
    print_r($xml);
    

    让数据了解什么是有效的和无效的可能会帮助您更好地理解这无效的原因。祝你好运!

    【讨论】:

    • 不知道如何查看。当我运行提要时,将其插入程序不会改变任何内容。现在我在下一行得到一个没有打印信息的错误。
    • 只需通过打印行继续向后工作,直到找到导致问题的行。如果其余部分没有响应以确保您传递了正确的数据,我会尝试打印 $get_kongregate_feed。您需要更好地了解实际值是什么。如果你拉这个,继续发布你试图加载的网址
    • 我想你误解了我的问题。我不确定如何实际打印这些行以找到它们的值,因为当我更改代码时,当我运行插件的特定部分时它不会显示打印的文本。
    • 不确定如何帮助您使用该特定插件。我建议在你的 print 语句之后放一个 break 来让脚本死掉。否则,您通常需要跟踪脚本的输出。祝你好运!
    猜你喜欢
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多