Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.