Myoga- Posted November 20, 2010 Share Posted November 20, 2010 Alright, this has been puzzling me for a while now and I think it's time I seek help before my brain turns to mush. I've been modifying a PHP text based game called Legend of the Green Dragon to add more functionality, etc. One of the things I want to add is the ability to add levels higher then 15. The original array looks like this: $exparray = array(1=>100,2=>400,3=>1002,4=>1912,5=>3140,6=>4707, 7=>6641,8=>8985, 9=>11795,10=>15143,11=>19121,12=>23840, 13=>29437,14=>36071,15=>43930, ); Now, I could just hardcode each level, but noo, I want to be able to do it from a module type thing from the admin panel (which I've already gotten that working gratefully) so I've tried writing a few different scripts to take the information from the MySQL database and turn it into an array like above but I've gotten completely lost. This is what I currently have: $expsql = "SELECT * FROM " . db_prefix("myolevels"); $numrows = mysql_num_rows($expsql); for ($i = 1; $i <= $numrows; $i++) { $expresult = mysql_query($expsql); while ($rows = mysql_fetch_assoc($expresult)) { $exparray = array($i=>$rows['myoexpreq']); } } Although when I call the array, it always returns null for the myoexpreq causing your master to look for you all the time (forcing level up when it shouldn't be). Is there any way to create an array like the first with SQL? Thought this might help as well: PHP Warning: "Variable passed to each() is not an array or object" in /home/ileetcom/public_html/logd/lib/experience.php at 35. Call Stack: 2: each(NULL) called from /home/ileetcom/public_html/logd/lib/experience.php on line 35 3: exp_for_next_level(16, "0") called from /home/ileetcom/public_html/logd/village.php on line 131 Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted November 20, 2010 Share Posted November 20, 2010 Not 100% this is correct without knowing how you database looks so I can test it. But try this out... $expsql = mysql_query("SELECT * FROM " . db_prefix("myolevels")); $level = 0; while($rows = mysql_fetch_assoc($expsql)) { $level++; $exparray = array($level=>$rows['myoexpreq']); } print_r($exparray); If this doesn't work, tell me how the DB looks then I'll try again Regards, Paul. Quote Link to comment Share on other sites More sharing options...
Myoga- Posted November 20, 2010 Author Share Posted November 20, 2010 Not 100% this is correct without knowing how you database looks so I can test it. But try this out... $expsql = mysql_query("SELECT * FROM " . db_prefix("myolevels")); $level = 0; while($rows = mysql_fetch_assoc($expsql)) { $level++; $exparray = array($level=>$rows['myoexpreq']); } print_r($exparray); If this doesn't work, tell me how the DB looks then I'll try again Regards, Paul. All errors are gone but it's still forcing you to fight your master Entire experience.php script: <?php // translator ready // addnews ready // mail ready // phpDocumentor ready /** * Returns the experience needed to advance to the next level. * * @param int $curlevel The current level of the player. * @param int $label The current number of dragonkills. * @return int The amount of experience needed to advance to the next level. */ function exp_for_next_level($curlevel, $curdk) { //$exparray = array(1=>100,2=>400,3=>1002,4=>1912,5=>3140,6=>4707, // 7=>6641,8=>8985, 9=>11795,10=>15143,11=>19121,12=>23840, // 13=>29437,14=>36071,15=>43930,16=>54788,17=>71932,18=>89978,19=>114832,20=>136542); $expsql = db_query("SELECT * FROM " . db_prefix("myolevels")); $level = 0; while ($rows = db_fetch_assoc($expsql)) { $level++; $exparray = array($level=>$rows['myoexpreq']); } if ($curlevel < 1) return 0; while(list($key,$val) = each($exparray)) { $exparray[$key] = round($val + ($curdk/4) * $key * 100, 0); } if ($curlevel > 20) $curlevel = 20; $exprequired = $exparray[$curlevel]; return $exprequired; } ?> Also, what do you mean tell you how it looks? There are 2 columns, one called "myolevel" and one called "myoexpreq". So it looks like this: Level Exp Required 1 100 2 400 3 1002 4 1912 5 3140 6 4707 7 6641 8 8986 9 11795 10 15143 11 19122 12 23840 13 29437 14 36071 15 43930 16 54788 17 71932 18 89979 19 113948 The array that we are building should, in essence look like this: $exparray = array($myolevel=>$myoexpreq,$myolevel=>$myoexpreq... etc. Thanks for the help btw. Quote Link to comment Share on other sites More sharing options...
Myoga- Posted November 20, 2010 Author Share Posted November 20, 2010 Sorry, I just did the print_r (I didn't see it there). It seems it can only display the last record in the sql table. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 20, 2010 Share Posted November 20, 2010 On each run through the loop, you reinitialize the array, therefore it will only hold the last value it's given. Initialize the array before the loop, then add an element on each iteration. Quote Link to comment Share on other sites More sharing options...
Myoga- Posted November 20, 2010 Author Share Posted November 20, 2010 On each run through the loop, you reinitialize the array, therefore it will only hold the last value it's given. Initialize the array before the loop, then add an element on each iteration. Got it! $expsql = db_query("SELECT * FROM " . db_prefix("myolevels")); $level = 0; $exparray = array(); while ($rows = db_fetch_assoc($expsql)) { $level++; $exparray["$level"] = $rows['myoexpreq']; } Thank you VERY much everyone for the help! Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 20, 2010 Share Posted November 20, 2010 You're welcome. If the problem is resolved, please mark it as such with the green "Mark Solved" button . . . Thanks! Quote Link to comment 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.