Post Installation Problem - Array Indicies

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jamiejackson
Forum Newbie
Posts: 3
Joined: Thu Aug 21, 2003 11:35 am

Post Installation Problem - Array Indicies

Post by jamiejackson »

Hi all,

had the enclosed script working just fine on my service providers web space (Linux box).

Decided to crate an Apache, PHP, mySQL setup on one of my XP boxes. all seems to be working fine except the script that runs fine on the service providers Linux box doesnt work on my XP box. PHP versions are the same, I've tried to get php.ini as close as possible.

It moans about array indicies.

I'll enclose some error messages and the script.

TIA for any help.

Regards
Jamie

Code: Select all

<?php
include("databaskoppling.php");

$indexnr = 0;
$indexnr2 = 0;
$in_char = 0;
$in_alliance = 0;
$in_crafting = 0;
$indexnr3 = 0;
$indexnr4 = 0;
$indexnr5 = 0;
function startElement($parser, $name, $attrs=''){
    global $current_tag, $thenames, $indexnr, $indexnr2, $in_char, $in_alliance, $thealliance, $in_crafting, $thecraft, $indexnr3, $thecraft2, $indexnr4, $theguild, $indexnr5;
    $current_tag = $name;
    switch($name){
		case 'GUILD':
		$theguild[$indexnr5]['guildname']=$attrs['NAME'];
		$theguild[$indexnr5]['realm']=$attrs['REALM'];
		$theguild[$indexnr5]['activechars']=$attrs['ACTIVECHARS'];
		$theguild[$indexnr5]['activemembers']=$attrs['ACTIVEMEMBERS'];
		$theguild[$indexnr5]['guildrp']=$attrs['GUILDRP'];
		$theguild[$indexnr5]['contacturl']=$attrs['CONTACTURL'];
		$theguild[$indexnr5]['websiteurl']=$attrs['WEBSITEURL'];
		$theguild[$indexnr5]['timestamp']=$attrs['TIMESTAMP'];
		$theguild[$indexnr5]['allianceid']=$attrs['ALLIANCE'];
		break;
    	case 'CHARACTER':
		$in_char = 1;
		$thenames[$indexnr]['name']=$attrs['NAME'];
		$forefternamn = explode(' ',$thenames[$indexnr]['name']);
		$thenames[$indexnr]['firstname'] = $forefternamn[0];
		$thenames[$indexnr]['lastname'] = $forefternamn[1];
		$thenames[$indexnr]['laston']=$attrs['LASTON'];	
    	 break;
		 case 'ALLIANCEMEMBER':
			$in_alliance = 1;
		 break;
		 case 'TOP_CHAR':
			 $in_crafting2 = 1;
			 $thecraft2[$indexnr4]['name']=$attrs['NAME'];
			 $thecraft2[$indexnr4]['point']=$attrs['POINT'];
        default:
        break;
    }
}

function endElement($parser, $name, $attrs=''){
    global $current_tag, $in_char, $indexnr, $in_alliance, $indexnr2, $indexnr3, $in_crafting, $in_crafting2, $indexnr4;
    switch($name){
		case 'GUILD':
		$indexnr5++;
		break;
        case 'CHARACTER':
		  $indexnr++;
		  $in_char = 0;
        break;
		 case 'ALLIANCEMEMBER':
		  $indexnr2++;
		  $in_alliance = 0;
		 break;
		 case 'TOP_CHAR':
		  $indexnr4++;
		  $in_crafting2 = 0;
		 break;
        default:
        break;
    }
}


function characterData($parser, $data){
    global $current_tag, $indexnr, $thenames, $in_char, $in_alliance, $indexnr2, $thealliance, $in_crafting, $indexnr3, $thecraft;
	
	if (($in_char == 1) && ($current_tag == 'RACE')) {
            $thenames[$indexnr]['race'] .= trim($data);
	}
	if (($in_char == 1) && ($current_tag == 'CLASS')) {
            $thenames[$indexnr]['class'] .= trim($data);
	}
	if (($in_char == 1) && ($current_tag == 'LEVEL')) {
            $thenames[$indexnr]['level'] .= trim($data);
	}
	if (($in_char == 1) && ($current_tag == 'GUILDRANK')) {
            $thenames[$indexnr]['guildrank'] .= trim($data);
	}
	if (($in_char == 1) && ($current_tag == 'TOTALRP')) {
            $thenames[$indexnr]['totalrp'] .= trim($data);
	}
	if (($in_char == 1) && ($current_tag == 'LASTWEEKRP')) {
            $thenames[$indexnr]['lastweekrp'] .= trim($data);
	}
	if (($in_char == 1) && ($current_tag == 'ANON')) {
            $thenames[$indexnr]['anon'] .= trim($data);
	}
	if (($in_char == 1) && ($current_tag == 'WEAPONCRAFT')) {
            $thenames[$indexnr]['weaponcraft'] .= trim($data);
	}
	if (($in_char == 1) && ($current_tag == 'FLETCHING')) {
            $thenames[$indexnr]['fletching'] .= trim($data);
	}
	if (($in_char == 1) && ($current_tag == 'TAILORING')) {
            $thenames[$indexnr]['tailoring'] .= trim($data);
	}
	if (($in_char == 1) && ($current_tag == 'SPELLCRAFT')) {
            $thenames[$indexnr]['spellcraft'] .= trim($data);
	}
	if (($in_char == 1) && ($current_tag == 'ARMORCRAFT')) {
            $thenames[$indexnr]['armorcraft'] .= trim($data);
	}
	if (($in_char == 1) && ($current_tag == 'ALCHEMY')) {
            $thenames[$indexnr]['alchemy'] .= trim($data);
	}

	if (($in_alliance == 1) && ($current_tag == 'ALNAME')) {
			$thealliance[$indexnr2]['alname'] .= trim($data);
	}
	if (($in_alliance == 1) && ($current_tag == 'ALGUILDID')) {
			$thealliance[$indexnr2]['alguildid'] .= trim($data);
	}
	if (($in_alliance == 1) && ($current_tag == 'ALTOTALRP')) {
			$thealliance[$indexnr2]['altotalrp'] .= trim($data);
	}
}

$xml_file = 'http://camelot-europe.goa.com/herald/servers/Excalibur/guilds/60.xml';
$type = 'UTF-8';
$xml_parser = xml_parser_create($type);
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
xml_set_element_handler($xml_parser, 'startElement','endElement');
xml_set_character_data_handler($xml_parser, 'characterData');

if (!($fp = fopen($xml_file, 'r'))) {
    die("Could not open $xml_file for parsing!\n");
}

$xmldata="";
while ($data = fread($fp, 4096)) {
    if (!($data = utf8_encode($data))) {
        echo 'ERROR'."\n";
    }
	$xmldata.=$data;
}
if (!xml_parse($xml_parser, $xmldata, true)) {
  die("XML error".xml_error_string(xml_get_error_code($xml_parser)));
}
xml_parser_free($xml_parser);
?>
	
<?php
mysql_query("DELETE FROM members");
mysql_query("DELETE FROM alliance");

	for($j=0;$j<$indexnr;$j++)
	{
	
		mysql_query("INSERT INTO members (name, firstname, lastname, laston, race, class, level, guildrank, totalrp, lastweekrp, anon, weaponcraft, fletching, tailoring, spellcraft, armorcraft, alchemy) VALUES('".$thenames[$j]['name']."','".$thenames[$j]['firstname']."','".$thenames[$j]['lastname']."','".$thenames[$j]['laston']."','".$thenames[$j]['race']."','".$thenames[$j]['class']."','".$thenames[$j]['level']."','".$thenames[$j]['guildrank']."','".$thenames[$j]['totalrp']."','".$thenames[$j]['lastweekrp']."','".$thenames[$j]['anon']."','".$thenames[$j]['weaponcraft']."','".$thenames[$j]['fletching']."','".$thenames[$j]['tailoring']."','".$thenames[$j]['spellcraft']."','".$thenames[$j]['armorcraft']."','".$thenames[$j]['alchemy']."')");

	}

	for($k=0;$k<$indexnr2;$k++)
	{
		$thealliance[$k]['alname'] = str_replace("ö","ö",$thealliance[$k]['alname']);
		$thealliance[$k]['alname'] = str_replace("´","´",$thealliance[$k]['alname']);
		$thealliance[$k]['alname'] = str_replace("'", "´", $thealliance[$k]['alname']);

		mysql_query("INSERT INTO alliance (alname, guildid, totalrp) VALUES('".$thealliance[$k]['alname']."','".$thealliance[$k]['alguildid']."','".$thealliance[$k]['altotalrp']."')") or die(mysql_error());
	}

$ourdate = date("Y-m-d H:i:s", time());
$ourdatetime = explode(' ',$ourdate);

	for($o=0;$o<=$indexnr5;$o++)
	{
		$datetime = explode(' ',$theguild[$o]['timestamp']);
		mysql_query("INSERT INTO info (guildname, realm, activechars, activemembers, guildrp, contacturl, website, timestamp, alliance, goadate, goatime, ourdate, ourtime) VALUES('".$theguild[$o]['guildname']."','".$theguild[$o]['realm']."','".$theguild[$o]['activechars']."','".$theguild[$o]['activemembers']."','".$theguild[$o]['guildrp']."','".$theguild[$o]['contacturl']."','".$theguild[$o]['websiteurl']."','".$theguild[$o]['timestamp']."','".$theguild[$o]['allianceid']."','".$datetime[0]."','".$datetime[1]."','".$ourdatetime[0]."','".$ourdatetime[1]."')");
	}

		for($m=0;$m<$indexnr4;$m++)
		{
			if($c >= 0 && $c < 5)
			{
				$crafttyp = "fletching";
			}
			elseif($c >= 5 && $c < 10)
			{
				$crafttyp = "weaponcraft";
			}
			elseif($c >= 10 && $c < 15)
			{
				$crafttyp = "armorcraft";
			}
			elseif($c >= 15 && $c < 20)
			{
				$crafttyp = "tailoring";
			}
			elseif($c >= 20 && $c < 25)
			{
				$crafttyp = "alchemy";
			}
			elseif($c >= 25 && $c < 30)
			{
				$crafttyp = "spellcraft";
			}			
			if($thecraft2[$m]['name'] != "anonymous")
			{
				mysql_query("UPDATE fom_xml_members set $crafttyp = '".$thecraft2[$m]['point']."' WHERE name = '".$thecraft2[$m]['name']."'");
			}
			$c++;
		}

echo "Done!";
?>
Errors:

Code: Select all

Notice: Undefined offset: 1 in C:\Program Files\Apache Group\Apache2\htdocs\guilduppd_runweb.php on line 32

Notice: Undefined index: race in C:\Program Files\Apache Group\Apache2\htdocs\guilduppd_runweb.php on line 75

Notice: Undefined index: class in C:\Program Files\Apache Group\Apache2\htdocs\guilduppd_runweb.php on line 78

Notice: Undefined index: level in C:\Program Files\Apache Group\Apache2\htdocs\guilduppd_runweb.php on line 81

Notice: Undefined index: guildrank in C:\Program Files\Apache Group\Apache2\htdocs\guilduppd_runweb.php on line 84

Notice: Undefined index: totalrp in C:\Program Files\Apache Group\Apache2\htdocs\guilduppd_runweb.php on line 87

Notice: Undefined index: lastweekrp in C:\Program Files\Apache Group\Apache2\htdocs\guilduppd_runweb.php on line 90

Notice: Undefined index: anon in C:\Program Files\Apache Group\Apache2\htdocs\guilduppd_runweb.php on line 93

Notice: Undefined index: race in C:\Program Files\Apache Group\Apache2\htdocs\guilduppd_runweb.php on line 75

Notice: Undefined index: class in C:\Program Files\Apache Group\Apache2\htdocs\guilduppd_runweb.php on line 78

Notice: Undefined index: level in C:\Program Files\Apache Group\Apache2\htdocs\guilduppd_runweb.php on line 81

Notice: Undefined index: guildrank in C:\Program Files\Apache Group\Apache2\htdocs\guilduppd_runweb.php on line 84

Notice: Undefined index: totalrp in C:\Program Files\Apache Group\Apache2\htdocs\guilduppd_runweb.php on line 87

Notice: Undefined index: lastweekrp in C:\Program Files\Apache Group\Apache2\htdocs\guilduppd_runweb.php on line 90

Notice: Undefined index: anon in C:\Program Files\Apache Group\Apache2\htdocs\guilduppd_runweb.php on line 93

..

Repeats over and over ...
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

my first thought is "xp is microsoft tand THAT is the probblem."

reading makes me wonder "do they have warnings turned off on the web host but not on your xp box?"

if the latter is true the issue isn't that it works fine, but that it doesn't show you the warnings on their server



remember XP IS NT andtherefore any nt considerations need to be thought of
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

I think the php.ini settings on your local server are E_ALL - that's good: you want maximum error reporting when you are developing. On the live site, the error reporting level is turned down from that - again as it should be since you don't want to provide any information which a hacker might try to exploit.

There are a lot of .= assignment operators in the characterData() fn, but php doesn't like being told to concatenate extra data to a var which hasn't already been declared.

Adding lines like:

Code: Select all

<?php
$thenames['key1']['key2'] = null;
?>
.. before you .= to the key would fix it.

Or, a better solution might be something like:

Code: Select all

<?php
if(!isset($thenames))
{
    $thenames = array(.. build a "blank" array structure with all the keys set to null ..);
}

?>
Last edited by McGruff on Wed Aug 10, 2005 9:19 pm, edited 1 time in total.
jamiejackson
Forum Newbie
Posts: 3
Joined: Thu Aug 21, 2003 11:35 am

Post by jamiejackson »

Mr McGruff you are a star sir!

Your advice has helped a lot. I have some work to do on my friends (yes, I 'borrowed' this script, but I guess they all say that ;)) script.

You were right about the levels of error reporting on the 2 servers.

The other problem was that on my tired old spare dev box, the script was taking more than 30 seconds to run. I've extended this limit and the script now completes.

This is good because, apart from debugging this script, the primary aim was to prove the installation off my new dev box. I'm happy this is all working now :)

Many thx.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

This may or may not help but I've found that a firewall on a local server can slow down scripts up to ten times - at least Zone Alarm does that on my own machine. Switching it off makes a dramatic improvement.
Post Reply