kryppienation Posted July 13, 2010 Share Posted July 13, 2010 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? //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++; } } Link to comment https://forums.phpfreaks.com/topic/207558-need-some-help/ Share on other sites More sharing options...
xcandiottix Posted July 13, 2010 Share Posted July 13, 2010 I think you would need to check if the field in your DB is set with the isset command. If your table is as so: Name level stat Monster 1 | level 2 | stats Monster 2 | level 3 | stats Monster 3 | level 4 | The you can put in your array: if(!isset('stat')){ echo this row } so if the stat row has no data it will echo it... if it does have data it will skip it. Link to comment https://forums.phpfreaks.com/topic/207558-need-some-help/#findComment-1085173 Share on other sites More sharing options...
kryppienation Posted July 13, 2010 Author Share Posted July 13, 2010 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! [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/207558-need-some-help/#findComment-1085174 Share on other sites More sharing options...
Pikachu2000 Posted July 13, 2010 Share Posted July 13, 2010 I haven't tested this, but I think it's gonna do what you want . . . <?php $getlevels = 'select level from monsters'; DB::connect($DB_database); $getlevelsresult = DB::query($getlevels, "get levels"); DB::close(); $levels = array(); while ($row = mysql_fetch_assoc($getlevelsresult)) { $levels[] = $row['level']; } $comp = range(2, 99); $need = array_diff($comp, $levels); foreach( $need as $v ) { echo $v . '<br />'; } ?> Link to comment https://forums.phpfreaks.com/topic/207558-need-some-help/#findComment-1085175 Share on other sites More sharing options...
kryppienation Posted July 13, 2010 Author Share Posted July 13, 2010 totally ftw!! Thanks a lot Pikachu2000~~ Link to comment https://forums.phpfreaks.com/topic/207558-need-some-help/#findComment-1085176 Share on other sites More sharing options...
kryppienation Posted July 13, 2010 Author Share Posted July 13, 2010 well that worked totally awesome and i was able to fill the gap very well including at least 1 monster for each level... Now let me ask you this, because you can see from the way i try to do things i'm not really good with this and still learning. Is there an easy was (just like the poster was able to help me easily solve this) to say now if i want to echo the numbers 2-99 and do a count of each level to see how many monsters i have for each level? If i wanted the output to look like this: Level - monstercount 2 - 3 3 - 2 4 - 2 5 - 5 6 - 1 7 - 1 8 - 1 9 - 2 ect ect all the way until 99. I really don't know how to use the array functions well and i know that there must be a simple way to display this information. Link to comment https://forums.phpfreaks.com/topic/207558-need-some-help/#findComment-1085578 Share on other sites More sharing options...
Pikachu2000 Posted July 13, 2010 Share Posted July 13, 2010 Yes. I just last week wrote a function that does precisely that to check for duplicates in a DB field. I posted it in a thread here. Let me see if I can find it. Link to comment https://forums.phpfreaks.com/topic/207558-need-some-help/#findComment-1085582 Share on other sites More sharing options...
Pikachu2000 Posted July 13, 2010 Share Posted July 13, 2010 Found it. Might need a tweak or two depending on how you're set up. $field = 'your_fieldname'; $table = 'your_tablename'; require_once("your_db_conn_script"); $output = ''; $query = "SELECT `{$field}`AS field, COUNT(`{$field}`) AS c FROM `{$table}` GROUP BY `{$field}` ORDER BY c DESC"; $result = mysql_query( $query ); $output = "<table>"; while( $array = mysql_fetch_assoc($result) ) { if( $array['c'] > 0 ) { // To check a field only for duplicated values, $array['c'] > 1 $bg != "CCCCCC" ? $bg = "CCCCCC" : $bg = "FFFFFF"; $count = $array['c']; $output .= "<tr bgcolor=\"$bg\"><td>Value:</td><td>" . $array['field'] . "</td><td>Entries:</td><td>" . $count . "</td></tr>"; } } $output .= "</table>"; echo $output; } Link to comment https://forums.phpfreaks.com/topic/207558-need-some-help/#findComment-1085584 Share on other sites More sharing options...
kryppienation Posted July 13, 2010 Author Share Posted July 13, 2010 Real decent! This worked great only had to change a few things. Thanks again so much!! Link to comment https://forums.phpfreaks.com/topic/207558-need-some-help/#findComment-1085592 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.