PDA

View Full Version : counting



Rohan72
08-11-2007, 12:43 AM
I'm trying to make a scoreboard (with statistics) for a sportsclub.
At the moment the page is crowded with buttons - for every player a button for every stats-category.
Now I would like to have only the players numbers or names on button and another row of buttons for each stat. To get the input i would have to hit a players button and next a stat button.

let's say the players buttons are "pl01" and "pl02" and the stats "s1" and "s2"
the stats would appear on screen as "pl01_s1", "pl01_s2", "pl02_s1" and "pl02_s2"

so if i would hit button "pl01" and next "s1" then the stat "pl01_s1" would be added with 1.

Is this possible with javascript? if so, can anyone give me an example?

???
08-11-2007, 01:06 AM
<html>
<head>
<script type="text/javascript" lang="Javascript">
var nplayers = 2;
var nstats = 2;
var stats = new Array ();
//Predefine stats
for (var i = 1; i <= nplayers; i = i + 1) {
for (var j = 1; j <= nstats; j = j + 1) {
stats ["p" + i + "s" + j] = 0;
}
}
var pbutton = null;
var sbutton = null;
var check = function () {
if (pbutton != null && sbutton != null) {
stats ["p" + pbutton + "s" + sbutton] = stats ["p" + pbutton + "s" + sbutton] + 1;
alert ("p" + pbutton + "s" + sbutton);
pbutton = null;
sbutton = null;
}
}
</script>
</head>
<body>
<div onclick="pbutton = 1; check ();">Player 1</div><br/>
<div onclick="pbutton = 2; check ();">Player 2</div><br/>
<div onclick="sbutton = 1; check ();">Stat 1</div><br/>
<div onclick="sbutton = 2; check ();">Stat 2</div><br/>
</body>
</html>

That worked for me. Add that script, you can change nplayers to the number of players you have, and nstats to the number of stats you have. Add onclick="pbutton = 1; check ();" to player buttons but 1 is whatever player, and onclick="sbutton = 2; check ();" to stat buttons where 2 is the stat number. I hope it works.

Rohan72
08-11-2007, 01:23 AM
Thanks, i'm going to try this, but it'll take some time to integrate everything, cause the individual stats (16 per player, 18 players) need to be put into team-totals, etc...
I'll let you know if it worked

???
08-11-2007, 02:14 AM
Opse, it doesn't yet display the stats. If you want, I could write that part too.

Rohan72
08-11-2007, 02:17 AM
the script works, but how do i get the stats in a table?
let's say the 2 stats are points and assists and the players names are mike and john.
when i push button "mike" and next button "points" then i would like to see the onscreen table change, adding 1 point to mike's total.

???
08-11-2007, 03:56 AM
var nplayers = new Array ("Player 1","Player 2");
var nstats = new Array ("Stat 1","Stat 2");
var stats = new Array ();
var tableHTML = "";
//Predefine stats
tableHTML += "<tr><td style='background-color: grey;'></td>";
for (var j = 1; j <= nstats.length; j = j + 1) {
tableHTML += "<td>" + nstats [j - 1] + "</td>";
}
tableHTML += "</tr>";
for (var i = 1; i <= nplayers.length; i = i + 1) {
tableHTML += "<tr><td>" + nplayers [i - 1] + "</td>";
for (var j = 1; j <= nstats.length; j = j + 1) {
tableHTML += "<td id ='" + "p" + i + "s" + j + "'>0</td>";
stats ["p" + i + "s" + j] = 0;
}
tableHTML += "</tr>";
}
var pbutton = null;
var sbutton = null;
var check = function () {
if (pbutton != null && sbutton != null) {
stats ["p" + pbutton + "s" + sbutton] = stats ["p" + pbutton + "s" + sbutton] + 1;
document.getElementById ("p" + pbutton + "s" + sbutton).innerHTML = stats ["p" + pbutton + "s" + sbutton];
pbutton = null;
sbutton = null;
}
}
window.onload = function () {
document.getElementById ("stats").innerHTML = tableHTML;
}


That for the js and add:


<table id="stats">
</table>

Where you want the table to be.
Hope it helps!

Rohan72
08-11-2007, 04:05 AM
I gives an error: "document.getElementById ... is null or not an object"

how 'bout if i send u the code i've written, so u can see where i want to go to?

???
08-11-2007, 02:06 PM
Sure, my email is: sub.atomic.fusion@gmail.com
Actually I don't know whats wrong. I never test things in IE anymore and it says "unknown error" in it anyway. It appears to have a problem with my "d" in document, what browser do you use anyway?