foreach($vip_users as $vip_user){
    $extra_info = fetch_profile_pic($vip_user); /* in this function it connect to mysql db */
    $profile_picture[$vip_user] = $extra_info['ProfilePic'];

}

its seems like a bad idea to me , what do u guys think? and how can do this in a proper way
Thanks

Are you worried about the number of elements that could be in the $vip_users array? Or about storing the return value of a function?
A little more information would help...

In general practise, arrays with mass insertation is a bad idea like you have done in line 3 where as the better idea is to fetch from the database as you need them.

Let's take for example your deploying a website like facebook and your using the above code for the contacts list. You will need to not only loop though but also to store in memory millions possibly even billions of users in memory causing the server to use terrabytes of memory just on the one little section of script. Not a good thing.

My recommendation is fetching directly from the database upon use and only store it inan array if your going to use every piece of information in that array later on. By saying every piece I don't mean just for filters but I mean for things like displaying on pages and such.

Are you worried about the number of elements that could be in the $vip_users array? Or about storing the return value of a function?
A little more information would help...

No im not worried about that cuz in my actual code is like this

$i = 1;
foreach($vip_users as $vip_user){
    $extra_info = fetch_profile_pic($vip_user); /* in this function it connect to mysql db */
    $profile_picture[$vip_user] = $extra_info['ProfilePic'];
    if ($i > 10){
        BREAK;
    }
    $i++;
}`

what concerns me is that it has to enter the db 10 times ... is that bad?

My recommendation is fetching directly from the database upon use and only store it inan array if your going to use every piece of information in that array later on. By saying every piece I don't mean just for filters but I mean for things like displaying on pages and such.

how would u recommend me to do this...

function fetch_profile_pic($username){
    $sql = mysql_query("SELECT * FROM `users` WHERE `usuario`  = '{$username}'");
    $userInfo = array ();
    while($rows= mysql_fetch_array($sql)) {
        $userInfo = array(
            'ProfilePic' => $rows['ProfilePic']
        );

    }
    return $userInfo;
}


$i = 1;
foreach($vip_users as $vip_user){
    $extra_info = fetch_profile_pic($vip_user); /* in this function it connect to mysql db */
    $profile_picture[$vip_user] = $extra_info['ProfilePic'];
    echo '<img src=users/'.$vip_user.'/photos/'.$profile_picture[$vip_user].'/>';
    if ($i > 10){
        BREAK;
    }
    $i++;
}

oh and the $vip_users is

function get_vip_users () {
    $sql= "SELECT * FROM `users` WHERE vip=1 AND level != 5 AND id != {$_SESSION['id']} ORDER BY `ProfilePic` DESC LIMIT 10";
    $users = array();
    $result = mysql_query($sql);
    while ( ( $row = mysql_fetch_assoc($result) ) !== false ){
        $users[] =  $row['usuario'];
    }
    return $users;
}


$vip_users = get_vip_users ();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.