Jump to content

Notice: Undefined variable: array


mrt003003

Recommended Posts

Hi there i keep getting a Notice: Undefined variable on my page. i gave 3 arrays and it points to all 3 of them.

 

Heres the lines of code:

<?php 
$total2 = count($array);
$random_ship2 = $array[$random2];

$total3 = count($array);
$random_ship3 = $array[$random3];

$total4 = count($array);
$random_ship4 = $array[$random4];

 

Am i missing something??

 

 

 

 

Heres full code:

<?php require_once('Connections/swb.php'); ?>
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

session_start();
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_Ships = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_Ships = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_swb, $swb);
$query_Ships = sprintf("SELECT * FROM ship WHERE PlayerName = %s", GetSQLValueString($colname_Ships, "text"));
$Ships = mysql_query($query_Ships, $swb) or die(mysql_error());
$row_Ships = mysql_fetch_assoc($Ships);
$totalRows_Ships = mysql_num_rows($Ships);


$colname_results = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_results = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_swb, $swb);
$query_results = sprintf("SELECT SUM(Class2) As Class2_totlas, SUM(Class3) As Class3_totlas, SUM(Class4) As Class4_totlas FROM ship WHERE PlayerName = %s AND `Template` =0 AND `Unique` = 0", GetSQLValueString($colname_results, "text"));
$results = mysql_query($query_results, $swb) or die(mysql_error());
$row_results = mysql_fetch_assoc($results);
$totalRows_results = mysql_num_rows($results);

$colname_resultp = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_resultp = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_swb, $swb);
$query_resultp = sprintf("SELECT SUM(Class2) As Class2_totla, SUM(Class3) As Class3_totla, SUM(Class4) As Class4_totla FROM planet WHERE PlayerName = %s", GetSQLValueString($colname_resultp, "text"));
$resultp = mysql_query($query_resultp, $swb) or die(mysql_error());
$row_resultp = mysql_fetch_assoc($resultp);
$totalRows_resultp = mysql_num_rows($resultp);

$colname_ShipYards = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_ShipYards = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_swb, $swb);
$query_ShipYards = sprintf("SELECT SUM(ShipYard1) As Shipyard_total1, SUM(ShipYard2) As Shipyard_total2, SUM(ShipYard3) As Shipyard_total3 FROM player WHERE PlayerName = %s", GetSQLValueString($colname_ShipYards, "text"));
$ShipYards = mysql_query($query_ShipYards, $swb) or die(mysql_error());
$row_ShipYards = mysql_fetch_assoc($ShipYards);
$totalRows_ShipYards = mysql_num_rows($ShipYards);

$colname_ShipYards1 = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_ShipYards1 = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_swb, $swb);
$query_ShipYards1 = sprintf("SELECT SUM(ShipYard1) As Shipyard1, SUM(ShipYard2) As Shipyard2, SUM(ShipYard3) As Shipyard3 FROM player WHERE PlayerName = %s", GetSQLValueString($colname_ShipYards1, "text"));
$ShipYards1 = mysql_query($query_ShipYards1, $swb) or die(mysql_error());
$row_ShipYards1 = mysql_fetch_assoc($ShipYards1);
$totalRows_ShipYards1 = mysql_num_rows($ShipYards1);


$colname_Class2 = "2";
$colname_Ships2 = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_Ships2 = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_swb, $swb);
$query_Ships2 = sprintf("SELECT * FROM ship WHERE PlayerName = %s AND Class=%s AND Template='1' AND `Unique`='0'", GetSQLValueString($colname_Ships2, "text"),
GetSQLValueString($colname_Class2, "int"));
$Ships2 = mysql_query($query_Ships2, $swb) or die(mysql_error());
$totalRows_Ships2 = mysql_num_rows($Ships2);


$colname_Class3 = "3";
$colname_Ships3 = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_Ships3 = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_swb, $swb);
$query_Ships3 = sprintf("SELECT * FROM ship WHERE PlayerName = %s AND Class=%s AND Template='1' AND `Unique`='0'", GetSQLValueString($colname_Ships3, "text"),
GetSQLValueString($colname_Class3, "int"));
$Ships3 = mysql_query($query_Ships3, $swb) or die(mysql_error());
$totalRows_Ships3 = mysql_num_rows($Ships3);

$colname_Class4 = "4";
$colname_Ships4 = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_Ships4 = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_swb, $swb);
$query_Ships4 = sprintf("SELECT * FROM ship WHERE PlayerName = %s AND Class=%s AND Template='1' AND `Unique`='0'", GetSQLValueString($colname_Ships4, "text"),
GetSQLValueString($colname_Class4, "int"));
$Ships4 = mysql_query($query_Ships4, $swb) or die(mysql_error());
$totalRows_Ships4 = mysql_num_rows($Ships4);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<link href="layout.css" rel="stylesheet" type="text/css" />
</head>

<body>
<table width="250" frame="box" align="center">
  <tr>
    <td colspan="3"><div align="center"><span class="style3">Avaliable Starship Class For Construction</span> </div></td>
  </tr>
  <tr>
    <td> </td>
    <td width="20"></td>
    <td width="80"><?php echo $row_ShipYards['Shipyard_total1']; echo $row_ShipYards['Shipyard_total2']; echo $row_ShipYards['Shipyard_total3'];?>
      
     </tr><tr><td></td>
    <td>
      
      <?php

if($row_ShipYards['Shipyard_total1'] > '0'){
if ($row_results['Class2_totlas'] < $row_resultp['Class2_totla']){
$class2 = ($row_resultp['Class2_totla'] - $row_results['Class2_totlas']);
echo '<span style="color: #009900">';
echo $class2;
?>
      </span> </td>
    <td> <span class="style3">
      <?php
echo' x ';
$count = 0;
while ($row = mysql_fetch_assoc($Ships2)){
$array[$count]['ShipID'] = $row['ShipID'];
$array[$count]['ShipName'] = $row['ShipName'];
$array[$count]['Class'] = $row['Class'];
$count++;
}
$total2 = count($array);
$random2 = rand(0,$total2 - 1);
$random_ship2 = $array[$random2];
$ShipID2 = $random_ship2['ShipID'];
$url2 = '<a href="add_ship.php?recordID='.$ShipID2.'">Class 2</a>';
echo '<span style="color: #009900">';
echo $url2;
echo'<br>';}}
?>
    </span></tr><tr><td></td>
    <td>
      <span class="style3">
      <?php

if($row_ShipYards['Shipyard_total2'] > '0'){
if ($row_results['Class3_totlas'] < $row_resultp['Class3_totla']){
$class3 = ($row_resultp['Class3_totla'] - $row_results['Class3_totlas']);
echo '<span style="color: #009900">';
echo $class3;?>
      </span></td>
    <td>
      <span class="style3">
      <?php
echo' x ';
$count = 0;
while ($row = mysql_fetch_assoc($Ships3)){
$array[$count]['ShipID'] = $row['ShipID'];
$array[$count]['ShipName'] = $row['ShipName'];
$array[$count]['Class'] = $row['Class'];
$count++;
}
$total3 = count($array);
$random3 = rand(0,$total3 - 1);
$random_ship3 = $array[$random3];
$ShipID3 = $random_ship3['ShipID'];
$url3 = '<a href="add_ship.php?recordID='.$ShipID3.'">Class 3</a>';
echo $url3;
echo'<br>';}} ?>
      </span></tr><tr><td></td>
      <td>
        <span class="style3">
        <?php
if($row_ShipYards['Shipyard_total3'] > '0'){
if ($row_results['Class4_totlas'] < $row_resultp['Class4_totla']){

$class4 = ($row_resultp['Class4_totla'] - $row_results['Class4_totlas']);
echo '<span style="color: #009900">';
echo $class4; ?>
        </span></td>
      <td> <span class="style3">
      <?php
echo' x ';
$count = 0;
while ($row = mysql_fetch_assoc($Ships4)){
$array[$count]['ShipID'] = $row['ShipID'];
$array[$count]['ShipName'] = $row['ShipName'];
$array[$count]['Class'] = $row['Class'];
$count++;
}
$total4 = count($array);
$random4 = rand(0,$total4 - 1);
$random_ship4 = $array[$random4];
$ShipID4 = $random_ship4['ShipID'];
$url4 = '<a href="add_ship.php?recordID='.$ShipID4.'">Class 4</a>';
echo $url4;}}
?>
      </span>	</td>
  </tr>
  <tr>
    <td colspan="3" align="center"><span class="style3">
      <?php if($row_ShipYards['Shipyard_total1'] == '0' && $row_ShipYards['Shipyard_total2'] == '0' && $row_ShipYards['Shipyard_total3'] == '0'){
echo 'No avaliable Shipyards'; } ?>
    </span></td>
  </tr>
  <tr>
    <td colspan="2"> </td>
    <td><div align="right"><a href="events.php" class="style4">Back</a></div></td>
  </tr>
</table>


</body>
<?php
mysql_free_result($Ships);
mysql_free_result($Ships2);
mysql_free_result($ShipYards);
mysql_free_result($ShipYards1);
mysql_free_result($Ships3);
mysql_free_result($Ships4);


?>
</html>

 

Thanks :|

Link to comment
https://forums.phpfreaks.com/topic/238585-notice-undefined-variable-array/
Share on other sites

I dont quite understand, this working previously. I thought it was created already.?

 

while ($row = mysql_fetch_assoc($Ships2)){
$array[$count]['ShipID'] = $row['ShipID'];
$array[$count]['ShipName'] = $row['ShipName'];
$array[$count]['Class'] = $row['Class'];
$count++;
}
$total2 = count($array);
$random2 = rand(0,$total2 - 1);
$random_ship2 = $array[$random2];

while ($row = mysql_fetch_assoc($Ships3)){
$array[$count]['ShipID'] = $row['ShipID'];
$array[$count]['ShipName'] = $row['ShipName'];
$array[$count]['Class'] = $row['Class'];
$count++;
}
$total3 = count($array);
$random3 = rand(0,$total3 - 1);
$random_ship3 = $array[$random3];

while ($row = mysql_fetch_assoc($Ships4)){
$array[$count]['ShipID'] = $row['ShipID'];
$array[$count]['ShipName'] = $row['ShipName'];
$array[$count]['Class'] = $row['Class'];
$count++;
}
$total4 = count($array);
$random4 = rand(0,$total4 - 1);
$random_ship4 = $array[$random4]; 

 

What should i do? :)

 

Thanks[/code]

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.