newbtophp Posted May 2, 2010 Share Posted May 2, 2010 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/200495-is-their-a-more-effective-way-of-doing-this/ Share on other sites More sharing options...
ChemicalBliss Posted May 2, 2010 Share Posted May 2, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/200495-is-their-a-more-effective-way-of-doing-this/#findComment-1052132 Share on other sites More sharing options...
bspace Posted May 3, 2010 Share Posted May 3, 2010 this perhaps though maybe i've not understood what your trying to do SELECT DISTINCT user_level FROM site_users Quote Link to comment https://forums.phpfreaks.com/topic/200495-is-their-a-more-effective-way-of-doing-this/#findComment-1052164 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.