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";
}
?>

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?

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

how do you know the while() loop isn't starting?  what evidence is there?  it looks like it should run just once.  trying adding a dummy echo line on the first line of the while() loop to see if it's running.

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.

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++;
}
?>

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
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.