tring to compare.. need help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kryppienation
Forum Newbie
Posts: 9
Joined: Thu Dec 04, 2008 1:12 am

tring to compare.. need help

Post by kryppienation »

Hey guys i'm trying to do something and i can't seem to get it done correctly. I have a preset amount of levels for a monster to be. These levels are 2-99. What i'm trying to do is go through each level 2-99, do a lookup in my monsters database, if the level is already there i would like nothing to happen, if the level is not there i'd like it to display the level so that i may know that i need to add a monster of this level. All the code is doing is listing the number all 2-99 and i only want it to list the numbers 2-99 that are already not used from the database.


Can anyone help me out here?

Code: Select all

//we need a counter to go from 2 - 99
   
   $count = 2;
   
   
   while ($count <= 99){
   
   $getlevels = 'select level from monsters';
   
   
DB::connect($DB_database);

$getlevelsresult = DB::query($getlevels, "get levels");

DB::close();



while ($row = mysql_fetch_assoc($getlevelsresult)) {




$levels = $row['level'];

}
   
   
   if ($levels == $count){
   
   $count++;
   
   } else {
   
   echo ''.$count.' -';
   
   $count++;
   
   }
   
   }




all fields in the database are not null tho. let me try to explain better what i'm trying to do. I'll include a SS of the database and a SS of the output and better try to explain what i want the output to come out as.

ok so as you can see in the database i have multiply monsters with the level of 2 and 63 as well as all of the other levels. All i'm trying to do is list all the numbers 2-99 that are not taken already so i know which levels i still need to add. So if the output as is, i'm trying to get the output to be like...

6 - 7 - 8 - 9 - 11 - 12 - 14 .. ect

because these are the levels i have not yet created monsters for.. i hope that this makes it easier. Thanks for the reply and i hope someone knows how to do this! :)
Attachments
output.PNG
output.PNG (5.28 KiB) Viewed 80 times
database.PNG
database.PNG (3.06 KiB) Viewed 80 times
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: tring to compare.. need help

Post by liljester »

first i would change my query to something like:

Code: Select all

$getlevels = 'select distinct(levels) from monsters where levels is not null order by levels asc;
that will give you a list of unique levels in order. then read all your results into an array via a loop; once you have an array of levels in your db, do a for loop that goes from 2 to 99, on each iteration check to see if the current value in the for loop is in your level array.. if its not, then print the current for loop value.
Post Reply