Seker Posted May 21, 2012 Posted May 21, 2012 I'm attempting to create a mod which sets up daily missions. I want to have different requirements for different levels. My question is, could I add something like this to global_func.php: function mission_level($level) { if ($level <= 10) { $missionlevel = 1; } etc., etc. } And then, at the top of the mission file, add something like this: $missionlevel = mission_level($level); $mpointsql = "SELECT pointsneeded FROM missions WHERE missionlevel=$missionlevel"; $mpoints = mysql_query($mpointsql); I feel like the function is right, but the check in the file is wrong. Any opinions on this? Quote
Nickson Posted May 21, 2012 Posted May 21, 2012 Well, the 'etc., etc.' is somewhat important too. The check itself is fine, however the function must have a return statement! If it does not, your missionlevel might not hold the value that you expect. Also to avoid developer mistakes, or if you really want to be sure, you could add a check on the data type of $level as well. PHP allows different data types to be bound to the same variables, thus strings as well. This way, you can be sure that the value is what you expect it to be. If you are 100% sure that $level will always be a numeric value, you don't really have to do this check. As 3rd remark, the $mpoint var will only hold a false (if the query failed) or a #resource id and not the 'pointsneeded' out of your database table. You still need to fetch the data. Quote
Seker Posted May 21, 2012 Author Posted May 21, 2012 Well, the 'etc., etc.' is somewhat important too. The check itself is fine, however the function must have a return statement! If it does not, your missionlevel might not hold the value that you expect. Also to avoid developer mistakes, or if you really want to be sure, you could add a check on the data type of $level as well. PHP allows different data types to be bound to the same variables, thus strings as well. This way, you can be sure that the value is what you expect it to be. If you are 100% sure that $level will always be a numeric value, you don't really have to do this check. As 3rd remark, the $mpoint var will only hold a false (if the query failed) or a #resource id and not the 'pointsneeded' out of your database table. You still need to fetch the data. So, with the check inside the file, perhaps something like...? $missionlevel = mission_level($level); $mpointsql = "SELECT pointsneeded FROM missions WHERE missionlevel=$missionlevel"; $mpoints = mysql_query($mpointsql); while ($r = mysql_fetch_array($mpoints) { blah...blah..blah... } Or, am I completely off base here? As for checking the level in the function itself, would it work to use something like...? $level = $userid['level'] Quote
Nickson Posted May 21, 2012 Posted May 21, 2012 Yes something like that should work. I'm not convinced on this though. Could just pass the variable straight away to the function. Just make sure that the variable you're passing on is always of the range you expect it to be. You don't want to pass a non-numeric string to that function if it isn't capable of processing it correctly. $level = $userid['level'] did you mean ? $level = $ir['level'] or have you defined the $userid array somewhere else? As this is not a standard array in mcc as far as I can remember. Quote
Seker Posted May 21, 2012 Author Posted May 21, 2012 Yes something like that should work. I'm not convinced on this though. Could just pass the variable straight away to the function. Just make sure that the variable you're passing on is always of the range you expect it to be. You don't want to pass a non-numeric string to that function if it isn't capable of processing it correctly. $level = $userid['level'] did you mean ? $level = $ir['level'] or have you defined the $userid array somewhere else? As this is not a standard array in mcc as far as I can remember. Yes, that is definitely what I meant. Sorry, it's about 2 in the morning, so I'm a little groggy. Thanks for the help. I'm just in the beginning stages of figuring out functions, so I definitely appreciate any input. I'll play around with it and see how it comes out. Again, thanks for the help! Quote
Nickson Posted May 21, 2012 Posted May 21, 2012 Not a problem! Let us know how your code-conquest works out ;) Quote
Seker Posted May 21, 2012 Author Posted May 21, 2012 Okay, I haven't tested this yet. I believe I have the rewards page, the SQL's, and the daillymission page itself where I need it. I'm just wondering, since the function defining the mission levels is pretty long, as I've gone up to level 30 and each requires a different if statement, could I just make the function its own file and include it on the mission page? As in: mission_func.php: global $h,$ir,$c; $level = $ir['level']; if ($level <= 10) { $missionlevel = 1; } if (($level > 10) && ($level <= 20)) { $missionlevel = 2; } etc., etc. And then, in dailymission.php, just add this at the top: include "mission_func.php"; In theory, it seems like this should work. Am I wrong on this? Quote
Nickson Posted May 22, 2012 Posted May 22, 2012 Well try it out and see what happens, writing theories is good, but it doesn't solve your issues. Trial and error is fun! Including your newly made function into a new file is, somewhat overdone, yet ease to reuse. Like so many things it has pros and cons. If it's never to be reused, I don't see the point. However, you could write your function more efficiently by using returns inside a function. The function will stop processing as soon as it reaches a return statement. Surely, it won't shave off seconds of processing times. But why not? Take a look at this logical. function getMissionLevel($level = false) { if(!$level || !is_numeric($level)) return -1; if($level <= 10) return 1; if($level <= 20) return 2; if($level <= 30) return 3; return 0; } This snippet offers quite some flexibility and robustness while using a logical way to solve the problem, but it can still get pretty long depending on how many mission levels you want to get. The difference with your script is that this one first checks whether the value that is passed is in fact, a numeric value. If not it returns a -1 and it stops. When the level is a numeric value it will continue and start continue to do checks until it meets an expression which is true. If it doesn't, it will meet an return 0; at the end of the function. Using this function by $missionlevel = getMissionLevel($ir['level']); We know that the range of $mission level can be -1 to MAX, simple checks can be done in the actual code where a -1 equals an incorrect value, a 0 would mean a 'not found' and anything above specifies a mission level to which you can let the script act accordingly. However, you can solve the problem more mathematical ;) Look at this.. function getMissionLevel($level = false) { if(!$level || !is_numeric($level)) return -1; return ( ceil($level / 10) ); } This function is much shorter, but will require a mathematical calculation. You can make it as easy (as shown) or as hard as you want, but it could offer a great flexibility. The "dummy check" is here as well, booleans, non-numeric strings, and a 0 ;) return -1, the rest is returned by the simple formula. Whatever level you pass on to the function will divide it by ten, and then round it up to the nearest integer. 9 / 10 = 0.9, rounded up by ciel() => 1 10 / 10 = 1, rounded up by ciel() => 1 (as it's an integer value already) 11 / 10 = 1.1, rounded up by ciel() => 2 So in short, if you're not reusing this function anywhere, you add the function to the page itself. But try it out and play around a bit ;) I'm quite sure you'll get there. Quote
Seker Posted May 22, 2012 Author Posted May 22, 2012 Well try it out and see what happens, writing theories is good, but it doesn't solve your issues. Trial and error is fun! Including your newly made function into a new file is, somewhat overdone, yet ease to reuse. Like so many things it has pros and cons. If it's never to be reused, I don't see the point. However, you could write your function more efficiently by using returns inside a function. The function will stop processing as soon as it reaches a return statement. Surely, it won't shave off seconds of processing times. But why not? Take a look at this logical. function getMissionLevel($level = false) { if(!$level || !is_numeric($level)) return -1; if($level <= 10) return 1; if($level <= 20) return 2; if($level <= 30) return 3; return 0; } This snippet offers quite some flexibility and robustness while using a logical way to solve the problem, but it can still get pretty long depending on how many mission levels you want to get. The difference with your script is that this one first checks whether the value that is passed is in fact, a numeric value. If not it returns a -1 and it stops. When the level is a numeric value it will continue and start continue to do checks until it meets an expression which is true. If it doesn't, it will meet an return 0; at the end of the function. Using this function by $missionlevel = getMissionLevel($ir['level']); We know that the range of $mission level can be -1 to MAX, simple checks can be done in the actual code where a -1 equals an incorrect value, a 0 would mean a 'not found' and anything above specifies a mission level to which you can let the script act accordingly. However, you can solve the problem more mathematical ;) Look at this.. function getMissionLevel($level = false) { if(!$level || !is_numeric($level)) return -1; return ( ceil($level / 10) ); } This function is much shorter, but will require a mathematical calculation. You can make it as easy (as shown) or as hard as you want, but it could offer a great flexibility. The "dummy check" is here as well, booleans, non-numeric strings, and a 0 ;) return -1, the rest is returned by the simple formula. Whatever level you pass on to the function will divide it by then, and then round it up to the nearest integer. 9 / 10 = 0.9, rounded up by ciel() => 1 10 / 10 = 1, rounded up by ciel() => 1 (as it's an integer value already) 11 / 10 = 1.1, rounded up by ciel() => 2 So in short, if you're not reusing this function anywhere, you add the function to the page itself. But try it out and play around a bit ;) I'm quite sure you'll get there. I finally got everything working right at around midnight last night. I ended up going with the include idea, as it had to be on both the mission, and the missionattack page. Thanks so much for your help! I'll definitely look into your method as far as optimization goes. Quote
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.