Jump to content

Is their a more effective way of doing this?


newbtophp

Recommended Posts

Is their a more effective way of doing this?, as something is telling me it may cause load issues in the future (as the db gets larger?). What im trying to do is extract all the unique user_level's from the db so they return an array. (im current querying the entire db and then extracting all the user_level's and doing the unique php side, as im sure on how to do that all in 1 query?)

 

My code:

 

<?php
$user_group_levels = array();
$user_group_result = mysql_query("SELECT * FROM site_users");
while($user_group_data = mysql_fetch_array($user_group_result)){
$user_group_levels[] = $user_group_data['user_level'];
}

$user_group_levels = array_unique($user_group_levels);

foreach($user_group_levels as $user_group){

?>
[<span class="<?php echo $user_group; ?>"><?php echo ucfirst($user_group); ?></span>]

<?php
}
?>

You should never need to pull every record at once from a user table.

 

I suggest you read heavily on 'database association design', You should have a seperate table containing all the different available user_levels.

Each user would have a 'user_level' int field - it would contain the ID of the user_level from the user_level table.

 

This way you keep separate data separate and dynamic, you can add/remove levels more easily etc etc.

 

-cb-

 

NOTE: You can select from multiple tables etc, to get the user_level as you pull the username your after from the db.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.