Jump to content

[SOLVED] array_rand saying its not an array when it is?


cooldude832

Recommended Posts

I have this code that is saying my array_rand is not an array being provided

error:

Warning: array_rand() [function.array-rand]: First argument has to be an array in functions.php on line 118

 

Warning: array_rand() [function.array-rand]: First argument has to be an array in functions.php on line 118

 

<?php
require_once("battle.php");
$i = 0;
$j = $i;
$tempfields = "";
$tempvalues = "";
$num_ops = $num_ops-1;
if($num_ops<1){$num_ops = 1;}
while($i <= $num_ops){
	$j++;
	$npc_key = array_rand($npc_name);
	$npc_insert_name[$i] = $npc_name[$npc_key];
	$npc_insert_HP[$i] = $npc_HP[$npc_key];
	$npc_insert_attack[$i] = $npc_attack[$npc_key];
	$npc_insert_defense[$i] = $npc_defense[$npc_key];
	$npc_insert_weapon[$i] = $npc_weapon[$npc_key];
	//Adds a comma on afterwards 
	if($tempfields != ""){$tempfields .= ",";}
	$tempfields .= "Name".$j.", HP".$j.", Attack".$j.", Defense".$j;
	//Adds a comma on afterwards 
	if($tempvalues != ""){$tempvalues .= ",";}
	$tempvalues .= "'".$npc_insert_name[$i]."',"."'".$npc_insert_HP[$i]."',"."'".$npc_insert_attack[$i]."',"."'".$npc_insert_defense[$i]."',"."'".$npc_insert_weapon[$i];
	$i++;
}
?>

This is battle.php

<?php
$x = $row['X'];
$y = $row['Y'];
$num_ops = array();
$npc_name = array();
$npc_HP = array();
$npc_attack = array();
$npc_defense = array();
$npc_weapon = array();
//Reigion One (from (5,5) to (40,40)
if($x>5 && $x <40 && $y<40 && $y>5){

$num_ops = rand(1,1);
$npc_name[] = "Hod Goblin";
$npc_HP[] = rand(25,40);
$npc_attack[] = rand(5,10);
$npc_defense[] = rand(5,10);
$npc_weapon[] = "Slap Attack";
}
?>

Link to comment
Share on other sites

check the manual for the function.  it triggers an error because the count of the array is less than the optional $num_req parameter, which defaults to 1.  this makes sense - how are you supposed to pick a random entry from an empty array?

Link to comment
Share on other sites

still no good tried doing this

functions.php

<?php
function attackers() {
require_once("battle.php");
connectSQL();
$tempq = "INSERT INTO `Battle` (UserID,".$tempfields.") VALUES('".$_SESSION['UserID']."',".$tempvalues.")";
mysql_query($tempq) or die("tempvalues = ".$tempvalues."<br/>tempfields = ".$tempfields."<br/>Done");
echo "Battle Has been intialzed";
}//end of attackres
?>

battle.php

<?php
$num_ops = 1;
$i = 1;
while ($i<=$num_ops){
	$npc_name[] = "Hod Goblin";
	$npc_HP[] = rand(25,40);
	$npc_attack[] = rand(5,10);
	$npc_defense[] = rand(5,10);
	$npc_weapon[] = "Slap Attack";
	$tempfields = "";
	$tempvalues = "";
	//Adds a comma on afterwards 
	if($tempfields != ""){$tempfields .= ",";}
	$tempfields .= "Name".$i.", HP".$i.", Attack".$i.", Defense".$i;
	//Adds a comma on afterwards 
	if($tempvalues != ""){$tempvalues .= ",";}
	$tempvalues .= "'".$npc_name[$i]."',"."'".$npc_HP[$i]."',"."'".$npc_attack[$i]."',"."'".$npc_defense[$i]."',"."'".$npc_weapon[$i];
	$i++;
}
?>

 

 

Now i'm really baffeled because the while loop isnt even starting

Link to comment
Share on other sites

as i said, add a dummy echo line to the start of the while() loop to ensure that it's the while() loop not executing.  the next step would be to echo all the variables declared in the battle.php file from within the function, to ensure that it's executing correctly in scope.

Link to comment
Share on other sites

scope is sounding like the issue here, I can get it to echo out tempvalues/fields fine in the top level document, but when i try and get it to echo then out in the function inside functions.php it can't and thsu my query is an empty query.  The values are generated from battle.php and then used in functions.php inside a function, any ideas how to fix this?

 

Even though the query inserts nothing the query still executes and a new row is added

functions.php

<?php
function attackers() {
require_once("battle.php");
connectSQL();
$tempq = "INSERT INTO `Battle` (".$tempfields.") VALUES(".$tempvalues.")";
mysql_query($tempq) or die(mysql_error());
echo "Battle Has been intialzed<br/>Temp Fields:" .$tempfields."<br/>Temp Values: ".$tempvalues;
}//end of attackres
?>

Main document  (this part echos out tempfields/tempvalues perfectly fine

<?php
if($view == "battle"){
echo "<tr><td>Battle Time<br/>Tempfields: ".$tempfields."<br/>Temp Values: ".$tempvalues."</td></tr></table>";
}
?>

battle.php (source of variables)

<?php
$num_ops = 1;
$i = 0;
$j = 0;
$tempfields = "UserID";
$tempvalues = "'".$_SESSION['UserID']."'";
while ($i<$num_ops){
	$j++;
	$npc_name[] = "Hod Goblin";
	$npc_HP[] = rand(25,40);
	$npc_attack[] = rand(5,10);
	$npc_defense[] = rand(5,10);
	$npc_weapon[] = "Slap Attack";
	//Adds a comma on afterwards 
	$tempfields .= ", Name".$j.", HP".$j.", Attack".$j.", Defense".$j;
	//Adds a comma on afterwards 
	$tempvalues .= ",'".$npc_name[$i]."',"."'".$npc_HP[$i]."',"."'".$npc_attack[$i]."',"."'".$npc_defense[$i]."',"."'".$npc_weapon[$i]."'";
	$i++;
	$j++;
}
?>

Link to comment
Share on other sites

if you're trying to pass them from the main scope to the function, you'll either need to declare them global within the function or pass them manually.  if the variables are already defined in the main scope, no need to include battle.php from within attackers():

 

<?php
function attackers($tempfields, $tempvalues) {
connectSQL();
$tempq = "INSERT INTO `Battle` (".$tempfields.") VALUES(".$tempvalues.")";
mysql_query($tempq) or die(mysql_error());
echo "Battle Has been intialzed<br/>Temp Fields:" .$tempfields."<br/>Temp Values: ".$tempvalues;
}//end of attackres
?>

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.