有问题使用shuffle与数组和mysql查询

时间:2014-03-04 08:58:49

标签: php arrays

我遇到了阵列无法改组的问题。我希望数组随机化为每个页面加载。我跟着documentation,但这里还有一些错误

错误消息

Warning: Invalid argument supplied for foreach() in E:\xampp\htdocs\projects\snppets\sidebar.php on line 65

这是获取好友列表的代码,将sting转换为数组,对数组进行计数并对其进行洗牌。

$friends = get_friends_IDS($profile_id);
$friends = make_string_to_ARRAY($friends);
$friends_count = count($friends);
$friends = shuffle($friends);

这是第65行的代码

<?php
foreach($friends as $id){
        $sql = "SELECT profile_id, profile_photo, profile_username, profile_name FROM profile WHERE profile_id='$id' LIMIT 1";
            $query = $db->SELECT($sql);
            $rows = $db->FETCH_OBJECT();
                if($db->NUM_ROWS() > 0){
                    foreach($rows as $row){
                        $friends_profile_id = $row->profile_id;
                        $friends_profile_photo = $row->profile_photo;
                        $friends_profile_username = $row->profile_username;
                        $friends_profile_name = $row->profile_name;
                    }
            }
}
?>  

此功能从数据库中获取好友列表

function get_friends_IDS($profile_id){
        global $db;
        $sql = "SELECT profile_id, profile_friends FROM profile WHERE profile_id='$profile_id' LIMIT 1";
        $query = $db->SELECT($sql);
        if($db->NUM_ROWS()){
        foreach($db->FETCH_OBJECT() as $row){
          return $row->profile_friends;
        }
      }
    }

这是函数将字符串转换为数组

function make_string_to_ARRAY($array){
  $array = explode(',', $array);
  return $array;
}

当我删除shuffle然后它工作正常...

1 个答案:

答案 0 :(得分:3)

shuffle()返回布尔值。

所以$friends = shuffle($friends);将$ friends作为布尔变量。

只需使用shuffle($friends);对其进行随机播放