Jump to content

Creating array with MySQL and PHP


Myoga-

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.