Jump to content

So frustrated I could scream...


Tundra

Recommended Posts

When I set off to help the World in Conflict gaming community come up with a way to display their clan's player's stats in a table I found W3schools. I quickly learned how to display the data using JS by reading the XML to HTML lesson in the XML Tutorial. I was amazed how fast I was able to come up with a table to display each player's stats as no one before was able to. Then I learned that if a player did not have a certain stat, particulary being ranked in a certain area like Air combat, the script would crash and nothing would print. So I started posting here in the forums for help. At first all I got was how sloppy my work was and a few other choice words. So with the exeption of Synook showing me how to make a node into an image (awesome btw) and Jesh with more great help on syntax, I have not been able to perfect my code or "clean it up" as everyone suggests. My sloppy code is the only one I have been able to make work.So basically all I am asking is to be pointed in the right direction as to what tutorial I should be reading. I want to start from scratch and produce a clean, efficient, and working project. I really appreciate everyone's help so far but the frustration and stress is really getting me down.My self-taught sloppy code that seems to work but crashes:

<html><head><style type="text/css">table.roster {	border: 0; cellpadding: 5; cellspacing: 1; background-color: #000000; text-align: center; font-size:11px; font-family:verdana;}table.roster td.01 {	width: 40px; }table.roster td.02 {	width: 80px;}table.roster td.03 {	width: 80px;}table.roster tr.d1 {	background-color: #999999; color: black;}table.roster tr.d2 {	background-color: #cccccc; color: black;}</style></head><body><!--**********Start Column Header Table**********--><table class="roster">	<tr class="d2">		<td class="02">Callsign</td>		<td class="01">Rank</td>		<td class="01">Global</td>		<td class="01">Air</td>		<td class="01">Arm</td>		<td class="01">Inf</td>		<td class="01">Sup</td>		<td class="03">Last Played</td>		<td class="02">Total Score</td>		<td class="01">Kills</td>		<td class="01">Deaths</td>	</tr></table><!--**********End Column Header Table**********--><!--XML Script NotesScript is Copyright 2008 Corey Dobbs (saturn_command@hotmail.com)You may use this code for free, just please keep these notes intactI am self taught and I know this code can be improved upon, please email me with any improvements!End XML Notes--><!--********************--><script type="text/javascript">var xmlDoc=null;if (window.ActiveXObject){// code for IExmlDoc=new ActiveXObject("Microsoft.XMLDOM");}else if (document.implementation.createDocument){// code for Mozilla, Firefox, Opera, etc.xmlDoc=document.implementation.createDocument("","",null);}else{alert('Your browser cannot handle this script');}<!--********************--><!--Start Tundra--><!--********************-->if (xmlDoc!=null){xmlDoc.async=false;xmlDoc.load("http://www.massgate.net/xml-player.php?id=336482");document.write("<table class='roster'>");var x=xmlDoc.getElementsByTagName("player");for (i=0;i<x.length;i++){ document.write("<tr class='d1'>");document.write("<td class='02'>");document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write("<img src=\"http://www.massgate.net/img/ranks/20px-" + x[i].getElementsByTagName("rank")[0].childNodes[0].nodeValue + ".png\" />");document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("playerLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("airLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("armorLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("infantryLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("supportLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='03'>");document.write(x[i].getElementsByTagName("lastPlayed")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='02'>");document.write(x[i].getElementsByTagName("totalScore")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("numUnitsKilled")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("numUnitsLost")[0].childNodes[0].nodeValue);document.write("</td>");document.write("</tr>");}document.write("</table>");}<!--********************--><!--Drako85--><!--********************-->if (xmlDoc!=null){xmlDoc.async=false;xmlDoc.load("http://www.massgate.net/xml-player.php?id=352899");document.write("<table class='roster'>");var x=xmlDoc.getElementsByTagName("player");for (i=0;i<x.length;i++){ document.write("<tr class='d2'>");document.write("<td class='02'>");document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write("<img src=\"http://www.massgate.net/img/ranks/20px-" + x[i].getElementsByTagName("rank")[0].childNodes[0].nodeValue + ".png\" />");document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("playerLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("airLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("armorLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("infantryLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("supportLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='03'>");document.write(x[i].getElementsByTagName("lastPlayed")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='02'>");document.write(x[i].getElementsByTagName("totalScore")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("numUnitsKilled")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("numUnitsLost")[0].childNodes[0].nodeValue);document.write("</td>");document.write("</tr>");}document.write("</table>");}<!--********************--><!--Nattis--><!--********************-->if (xmlDoc!=null){xmlDoc.async=false;xmlDoc.load("http://www.massgate.net/xml-player.php?id=347396");document.write("<table class='roster'>");var x=xmlDoc.getElementsByTagName("player");for (i=0;i<x.length;i++){ document.write("<tr class='d1'>");document.write("<td class='02'>");document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write("<img src=\"http://www.massgate.net/img/ranks/20px-" + x[i].getElementsByTagName("rank")[0].childNodes[0].nodeValue + ".png\" />");document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("playerLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("airLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("armorLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("infantryLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("supportLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='03'>");document.write(x[i].getElementsByTagName("lastPlayed")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='02'>");document.write(x[i].getElementsByTagName("totalScore")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("numUnitsKilled")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("numUnitsLost")[0].childNodes[0].nodeValue);document.write("</td>");document.write("</tr>");}document.write("</table>");}<!--********************--><!--ardisalis--><!--********************-->if (xmlDoc!=null){xmlDoc.async=false;xmlDoc.load("http://www.massgate.net/xml-player.php?id=355309");document.write("<table class='roster'>");var x=xmlDoc.getElementsByTagName("player");for (i=0;i<x.length;i++){ document.write("<tr class='d2'>");document.write("<td class='02'>");document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write("<img src=\"http://www.massgate.net/img/ranks/20px-" + x[i].getElementsByTagName("rank")[0].childNodes[0].nodeValue + ".png\" />");document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("playerLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("airLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("armorLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("infantryLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("supportLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='03'>");document.write(x[i].getElementsByTagName("lastPlayed")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='02'>");document.write(x[i].getElementsByTagName("totalScore")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("numUnitsKilled")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("numUnitsLost")[0].childNodes[0].nodeValue);document.write("</td>");document.write("</tr>");}document.write("</table>");}<!--********************--><!--ChurlishBrute--><!--********************-->if (xmlDoc!=null){xmlDoc.async=false;xmlDoc.load("http://www.massgate.net/xml-player.php?id=213069");document.write("<table class='roster'>");var x=xmlDoc.getElementsByTagName("player");for (i=0;i<x.length;i++){ document.write("<tr class='d1'>");document.write("<td class='02'>");document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write("<img src=\"http://www.massgate.net/img/ranks/20px-" + x[i].getElementsByTagName("rank")[0].childNodes[0].nodeValue + ".png\" />");document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("playerLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("airLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("armorLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("infantryLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("supportLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='03'>");document.write(x[i].getElementsByTagName("lastPlayed")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='02'>");document.write(x[i].getElementsByTagName("totalScore")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("numUnitsKilled")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("numUnitsLost")[0].childNodes[0].nodeValue);document.write("</td>");document.write("</tr>");}document.write("</table>");}<!--********************--><!--w1sp3r--><!--********************-->if (xmlDoc!=null){xmlDoc.async=false;xmlDoc.load("http://www.massgate.net/xml-player.php?id=226372");document.write("<table class='roster'>");var x=xmlDoc.getElementsByTagName("player");for (i=0;i<x.length;i++){ document.write("<tr class='d2'>");document.write("<td class='02'>");document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write("<img src=\"http://www.massgate.net/img/ranks/20px-" + x[i].getElementsByTagName("rank")[0].childNodes[0].nodeValue + ".png\" />");document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("playerLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("airLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("armorLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("infantryLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("supportLeaderboard")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='03'>");document.write(x[i].getElementsByTagName("lastPlayed")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='02'>");document.write(x[i].getElementsByTagName("totalScore")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("numUnitsKilled")[0].childNodes[0].nodeValue);document.write("</td>");document.write("<td class='01'>");document.write(x[i].getElementsByTagName("numUnitsLost")[0].childNodes[0].nodeValue);document.write("</td>");document.write("</tr>");}document.write("</table>");}</script></body></html>

Attempt to clean it up (provided by Jesh):

<html><head><style type="text/css">table.roster {	border: 0; cellpadding: 5; cellspacing: 1; background-color: #000000; text-align: center; font-size:11px; font-family:verdana;}table.roster td.01 {	width: 40px; }table.roster td.02 {	width: 80px;}table.roster td.03 {	width: 80px;}table.roster tr.d1 {	background-color: #999999; color: black;}table.roster tr.d2 {	background-color: #cccccc; color: black;}</style></head><body><script type="text/javascript">var xmlDoc=null;if (window.ActiveXObject){// code for IExmlDoc=new ActiveXObject("Microsoft.XMLDOM");}else if (document.implementation.createDocument){// code for Mozilla, Firefox, Opera, etc.xmlDoc=document.implementation.createDocument("","",null);}else{alert('Your browser cannot handle this script');}if (xmlDoc!=null){xmlDoc.async=false;xmlDoc.load("http://www.massgate.net/xml-player.php?id=336482");function GetPlayerObject(playerNode){	// Add error handling here to deal with empty nodes...	var name = playerNode.getElementsByTagName("name")[0].childNodes[0].nodeValue;	var rank = playerNode.getElementsByTagName("rank")[0].childNodes[0].nodeValue;	var country = playerNode.getElementsByTagName("country")[0].childNodes[0].nodeValue;	var favoriteRole = playerNode.getElementsByTagName("favoriteRole")[0].childNodes[0].nodeValue;	var lastPlayed = playerNode.getElementsByTagName("lastPlayed")[0].childNodes[0].nodeValue;	var playerLeaderboard = playerNode.getElementsByTagName("playerLeaderboard")[0].childNodes[0].nodeValue;	var airLeaderboard = playerNode.getElementsByTagName("airLeaderboard")[0].childNodes[0].nodeValue;	var armorLeaderboard = playerNode.getElementsByTagName("armorLeaderboard")[0].childNodes[0].nodeValue;	var infantryLeaderboard = playerNode.getElementsByTagName("infantryLeaderboard")[0].childNodes[0].nodeValue;	var supportLeaderboard = playerNode.getElementsByTagName("supportLeaderboard")[0].childNodes[0].nodeValue;	var totalScore = playerNode.getElementsByTagName("totalScore")[0].childNodes[0].nodeValue;	var numUnitsKilled = playerNode.getElementsByTagName("numUnitsKilled")[0].childNodes[0].nodeValue;	var numUnitsLost = playerNode.getElementsByTagName("numUnitsLost")[0].childNodes[0].nodeValue;		// Build a player object using JSON.			var player = {						"name" : name,			"rank" : rank,			"country" : country,			"favoriteRole" : favoriteRole,			"lastPlayed" : lastPlayed,			"playerLeaderboard" : playerLeaderboard,			"airLeaderboard" : airLeaderboard,			"armorLeaderboard" : armorLeaderboard,			"infantryLeaderboard" : infantryLeaderboard,			"supportLeaderboard" : supportLeaderboard,			"totalScore" : totalScore,			"numUnitsKilled" : numUnitsKilled,			"numUnitsLost" : numUnitsLost,		}		return player;}function GetPlayerHtml(player)}	// Using the JSON object passed, build out the HTML string	var html = "<tr class='d1'>";	html += "<td class='02'>";	html += player.name;	html += "</td>";	html += "<td class='01'>";	html += player.rank;	html += "</td>";	html += "<td class='02'>";	html += player.country;	html += "</td>";	html += "<td class='01'>";	html += player.favoriteRole;	html += "</td>";		html += "<td class='02'>";	html += player.lastPlayed;	html += "</td>";	html += "<td class='01'>";	html += player.playerLeaderboard;	html += "</td>";	html += "<td class='01'>";	html += player.airLeaderboard;	html += "</td>";	html += "<td class='01'>";	html += player.armorLeaderboard;	html += "</td>";	html += "<td class='01'>";	html += player.infantryLeaderboard;	html += "</td>";	html += "<td class='01'>";	html += player.supportLeaderboard;	html += "</td>";	html += "<td class='01'>";	html += player.totalScore;	html += "</td>";		html += "<td class='01'>";	html += player.numUnitsKilled;	html += "</td>";	html += "<td class='01'>";	html += player.numUnitsLost;	html += "</td>";		html += "</tr>";	// Return it to the caller	return html;}var playerNodes = xmlDoc.getElementsByTagName("player");</script></body></html>

Thanks again...

Link to comment
Share on other sites

Well, once I told you to clean your code up, I mentioned two alternatives to java script: XSLT and PHP.With XSLT, you just need to put a little pointer in the XML file to the XSLT file, and then when you open the XML file, you'll see the table, or whatever else you use XSLT for.With PHP, you do pretty much what you do already, but do it on the server, which makes it easier to solve problems, as JavaScript problems can vary from browser to browser. PHP problems don't. They either affect everyone or no one.A combination of the three (PHP, XSLT and JavaScript) is the best way, but that may be too advanced for you right now, so I'd suggest XSLT. XSLT requires that you also learn XPath, which should teach you well how XML really works. That knowledge will later will help you with JavaScript and/or PHP (it sure helped me learn PHP quicker), that is, if you want to use them. XSLT & XPath alone can do all that you want with least effors.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...