Jump to content

convert from javascript array's to geting values from mysql tables


wilsoc31

Recommended Posts

Can anyone help me know how to convert this from javascript array?  Currently it is running through an array which im not a fan of because since it runs in the array its cached memory that cant be saved as progress and return later.  as you can see, i have some php and can save it off, but it still runs through the array instead.

 

/*
 *	Tournament Administration Sheet
 *
 *  Copyright (C) 2008  Jan Gassen
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
var groupCounter = 0;
var finishCounter = 0;
var allFinished = false;
var modeConnector;
var groupList = new Array();
var members = 0;

function Connector(playerCounter, groupCounter) {
	this.table = document.createElement("table");
    this.table.setAttribute("id", "playerInputWindow");
	this.table.setAttribute("width", "450");
	this.table.style.position = "absolute";
	this.table.style.left = document.body.clientWidth / 2 - 125;
	this.table.style.top = "30";

 	this.groups = groupCounter;

    var instance = this;

	this.table.style.display = "none";

	var tbody = document.createElement("tbody");
    this.table.appendChild(tbody);

    this.dropDown = document.createElement("select");
    for (var x = 0; x < members; x++) {
    	var option = document.createElement("option");
    	//option.text = (x + 1) + " Player";
    	option.text = (x + 1) + " Team";
		option.value = x + 1;
		this.dropDown.options.add(option);
    }

    this.modes = document.createElement("select");
    var opt1 = document.createElement("option");
   	opt1.text = "Single Elimination";
	opt1.value = "ko.html";
	this.modes.options.add(opt1);

	var opt2 = document.createElement("option");
   	opt2.text = "Double Elimination";
	opt2.value = "dko.html";
	this.modes.options.add(opt2);

   this.partner = document.createElement("select");
 /*    var opt1 = document.createElement("option");
   	opt1.text = "als DYP";
	opt1.value = "1";
	this.partner.options.add(opt1);
*/
	var opt2 = document.createElement("option");
   	opt2.text = "als Teams";
	opt2.value = "0";
	this.partner.options.add(opt2);

    var btn = document.createElement("input");
    btn.type = "button";
    btn.value = "Next";
    btn.onclick = function () {
    	Connector.prototype.go(instance);
    };

    var btn2 = document.createElement("input");
    btn2.type = "button";
    btn2.value = "Cancel";
    btn2.onclick = function () {
    	Connector.prototype.stop(instance);
    };

    this.setInput = document.createElement("input");
    this.setInput.type = "text";
    this.setInput.value = "1";
    this.setInput.size = "2";
    this.setInput.setAttribute("MAXLENGTH", "1");

	this.shuffleInput = document.createElement("input");
    this.shuffleInput.type = "checkbox";


    var row = this.table.insertRow(0);
	var cell = row.insertCell(0);
	cell.setAttribute("colspan", "2");
	cell.setAttribute("valign", "middle");
	cell.innerHTML = "<div id='popupTitle'><table><tr><td>Continue...</td></tr></table></div>";

	row = this.table.insertRow(1);
	cell = row.insertCell(0);
	cell.setAttribute("colspan", "2");
	cell.innerHTML = "Time for bracket play:";

    // Row 2
    row = tbody.insertRow(2);

    cell = row.insertCell(0);
    cell.innerHTML = "Continue with: ";

    cell = row.insertCell(1);
	cell.appendChild(this.modes);

	// Row 3
	row = tbody.insertRow(3);

	cell = row.insertCell(0);
    cell.innerHTML = "How many teams: ";

    cell = row.insertCell(1);
	cell.appendChild(this.dropDown);

	// Row 4
	row = tbody.insertRow(4);

	cell = row.insertCell(0);

	cell = row.insertCell(1);
	cell.appendChild(this.partner);

    // Row 5
    row = tbody.insertRow(5);

    cell = row.insertCell(0);
    cell.innerHTML = "Profit rates: ";

    cell = row.insertCell(1);
	cell.appendChild(this.setInput);

    // Row 6
    row = tbody.insertRow(6);

    cell = row.insertCell(0);
    cell.innerHTML = "Losing round shuffle?";

    cell = row.insertCell(1);
    cell.appendChild(this.shuffleInput);

    // Row 7
    row = tbody.insertRow(7);

    cell = row.insertCell(0);
    cell.appendChild(btn2);

    cell = row.insertCell(1);
    cell.appendChild(btn);
}

Connector.prototype.show = function () {
	this.table.style.display = "block";

	document.getElementById("dimmer").style.display = "block";
	document.getElementById("dimmer").style.width = document.body.clientWidth + "px";
	document.getElementById("dimmer").style.height = (document.body.scrollHeight || document.body.offsetHeight) + "px";
}

Connector.prototype.get = function () {
	return this.table;
}

Connector.prototype.stop = function (instance) {
	instance.table.style.display = "none";
	document.getElementById("dimmer").style.display = "none";
}

Connector.prototype.go = function (instance) {
	pageUrl = instance.modes.options[instance.modes.selectedIndex].value;
	pageTxt = instance.modes.options[instance.modes.selectedIndex].text;

	player = instance.dropDown.options[instance.dropDown.selectedIndex].value;

	top.GLOBALS["teams"] = instance.groups * parseInt(player);
	top.GLOBALS["sets"] = parseInt(instance.setInput.value);
	top.GLOBALS["groups"] = 1;
	top.GLOBALS["modeVal"] = pageUrl;
	top.GLOBALS["modeTxt"] = pageTxt;
	top.GLOBALS["shuffleLooser"] = instance.shuffleInput.checked;

	var list = new Array();

	var len = groupList.length;
	for (var i = 0; i < len; i++) {
		var grp = groupList[i];

		var teams = grp.getTeams();

		for (var j = 0; (j < teams.length && j < parseInt(player)); j++) {
			list.push(teams[j][0]);
		}
	}

	top.GLOBALS["playerList"] = list;
	top.GLOBALS["dyp"] = instance.partner.options[instance.partner.selectedIndex].value;

	top.FILL_FROM_GRP = true;

	window.location = pageUrl;
}

function Score(size) {
	this.rows = new Array();
	this.size = size;
	this.data;

	this.table = document.createElement("table");
    this.table.setAttribute("border", "0");
    this.table.setAttribute("width", "100%");
    this.table.setAttribute("cellspacing", "0");
    this.table.setAttribute("cellpadding", "2");

    var tbody = document.createElement("tbody");
    this.table.appendChild(tbody);

    this.table.style.display = "none";
    this.table.style.cursor = "pointer";

    var row = tbody.insertRow(0);
    this.detailCell = row.insertCell(0);
    this.detailCell.setAttribute("width", "100%");
    this.detailCell.setAttribute("class", "groupDetail");
    this.detailCell.colSpan = "4";
    this.detailCell.innerHTML="&nbsp;";
    this.detailCell.style.display = "none";
    this.detailCell.onclick = function () {
    	this.style.display = "none";
    }

    var instance = this;
    var x = 1;

    for (x = 1; x <= size; x++) {
    	var row = tbody.insertRow(x);
    	row.setAttribute("id", x-1);

    	row.onclick = function () {
    		Score.prototype.detail(this.id, instance);
    	};

    	if (x % 2) {
    		row.setAttribute("class", "odd");
    	} else {
    		row.setAttribute("class", "even");
    	}

		var cellName = row.insertCell(0);
		var cellPoints = row.insertCell(1);
		cellPoints.setAttribute("align", "right");
		var cellGames = row.insertCell(2);
		cellGames.setAttribute("align", "right");
		var cellDiff = row.insertCell(3);
		cellDiff.setAttribute("align", "right");

		this.rows.push(new Array(cellName, cellPoints, cellGames, cellDiff));
    }
}

Score.prototype.diff = function (id) {
	var val = this.data[id][6] - this.data[id][7];

	if (val > 0) {
		val = "+" + val;
	}

	return val;
}

Score.prototype.detail = function (id, instance) {
	instance.detailCell.style.display = "table-cell";

	instance.detailCell.innerHTML = "<div><b>" + instance.data[id][0] + "</b><br>";
	instance.detailCell.innerHTML += "Points: " + instance.data[id][1] + "<br>";
	instance.detailCell.innerHTML += "Games: " + instance.data[id][2] + "<br>";
	instance.detailCell.innerHTML += "S/U/N: " + instance.data[id][3] + "/" + instance.data[id][4] + "/" + instance.data[id][5] + "<br>";
	instance.detailCell.innerHTML += "Goals: " + instance.data[id][6] + ":" + instance.data[id][7]
									+ " Diff: " + (instance.diff(id)) + "</div>";

}

Score.prototype.set = function (scores) {
	this.data = scores;


	for (var x = 0; x < this.rows.length; x++) {
		var row = this.rows[x];

		row[0].innerHTML = scores[x][0];
		row[1].innerHTML = scores[x][1];
		row[2].innerHTML = scores[x][2];
		row[3].innerHTML = this.diff(x);
	}
}

Score.prototype.show = function () {
	this.table.style.display = "table";
}

Score.prototype.hide = function () {
	this.table.style.display = "none";
}

Score.prototype.get = function () {
	return this.table;
}


var groupMap = new Array();

function Group(size, id, matches) {
	this.teams = new Array();
	this.games = new Array();
	this.scores = new Array();

	this.size = size;

	this.scoreTable = new Score(size);

	var instance = this;

	this.table = this.create(size, id, instance);
	this.innerTable;
	this.historyCell;

	this.player1name;
	this.player1points;

	this.player2name;
	this.player2points;

	this.id = id;

	this.gameCounter = 0;

	this.isFinish = false;

	this.showHistory = false;
	this.history = "";

	groupMap["Group_"+id] = new Array();
	groupCounter++;

	for (var i = 0; i < matches; i++) {
		this.createGames(size);
	}
}

Group.prototype.showAllGames = function()
{
	var allGames = "";
    let text;

	for (var x = this.gameCounter; x < this.games.length; x++)
	{
		var id1 = this.games[x][0];
		var id2 = this.games[x][1];

		var name1 = this.teams[id1][0].value;
		var name2 = this.teams[id2][0].value;

		//allGames += name1 + " - " + name2 + "<br>";
          //  allGames += "all games - all games2 <br>";

		//insert records into the database for all the games
		var xmlhttp;
		if (window.XMLHttpRequest)
		    xmlhttp = new XMLHttpRequest();
		else
		    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
       // alert('inserting data..' );
		xmlhttp.open('GET',"save_pool_history.php?action=insert&history=" + name1 + "|||" + name2, true);
        xmlhttp.send();
	}

    fetch('save_pool_history.php?action=select_future').then((response)=>{
				     if (!response.ok)
				     {
				         throw new Error('Invalid Response');
				     }

				     return response.text();
				 }).then((text)=>{
					 // alert('2nd alert' );

	          allGames = text;
	          alert('allGames1: ' + allGames);
				   //  alert('1st alert' + text);
				   //  alert('1-2 alert' + this.history);
				 }).catch((error)=>{
				     alert('Error fetching response.');
            });
       alert('allGames2: ' + allGames);
	return allGames;
}



Group.prototype.shuffle = function () {
	this.games.sort(function() {
			return 0.5 - Math.random();
	});
}

Group.prototype.createGames = function (size) {
	for (var x = 0; x < size; x++) {
		for (var y = x+1; y < size; y++) {
			this.games.push(new Array(x, y));
		}
	}

	this.shuffle();
}

Group.prototype.sort = function () {
	this.scores.sort(function(a, b) {
			return (b[6] - b[7]) - (a[6] - a[7]);
	});

	this.scores.sort(function(a, b) {
			return (a[5] - b[5]);
	});

	this.scores.sort(function(a, b) {
			return (b[3] - a[3]);
	});

	this.scores.sort(function(a, b) {
			return (b[1] - a[1]);
	});
}

let teama;
let teamb;
let myarr;
Group.prototype.refresh = function () {
	var player1id = this.games[this.gameCounter][0];
	var player2id = this.games[this.gameCounter][1];

fetch('save_pool_history.php?action=select_current').then((response)=>{
			     if (!response.ok)
			     {
			         throw new Error('Invalid Response');
			     }

			     return response.text();
			 }).then((text)=>{
				//  alert('text before split: ' + text );
             myarr = text.split("|||");
             teama=myarr[0];
             teamb=myarr[1];
            //  this.player1name.setAttribute("value",teama);
           //  this.player2name.setAttribute("value",teamb);

             //    alert('team1: '+ teama);
              //   alert('team2: '+ teamb);
			    // alert('1st alert' + text);

			//     alert('1-2 alert' + this.history);
			 }).catch((error)=>{
			     alert('Error fetching games history.');
            });

                   alert('team1-2: '+ teama);
                //  alert('team2-2: '+ teamb);
                   this.player1name.setAttribute("value", teama );
                  this.player2name.setAttribute("value", teamb );

	if ("" !== this.player1points.value && "" !== this.player2points.value)
	{
		var score1 = parseInt(this.player1points.value);
		var score2 = parseInt(this.player2points.value);

		if (score1 == score2)
		{
			this.teams[player1id][1].value = parseInt(this.teams[player1id][1].value) + 1;
			this.teams[player2id][1].value = parseInt(this.teams[player2id][1].value) + 1;

			this.teams[player1id][4].value = parseInt(this.teams[player1id][4].value) + 1;
			this.teams[player2id][4].value = parseInt(this.teams[player2id][4].value) + 1;
		}
		else if (score1 > score2)
		{
			this.teams[player1id][1].value = parseInt(this.teams[player1id][1].value) + 3;

			this.teams[player1id][3].value = parseInt(this.teams[player1id][3].value) + 1;
			this.teams[player2id][5].value = parseInt(this.teams[player2id][5].value) + 1;
		}
		else
		{
			this.teams[player2id][1].value = parseInt(this.teams[player2id][1].value) + 3;

			this.teams[player1id][5].value = parseInt(this.teams[player1id][5].value) + 1;
			this.teams[player2id][3].value = parseInt(this.teams[player2id][3].value) + 1;
		}

		this.teams[player1id][2].value = parseInt(this.teams[player1id][2].value) + 1;
		this.teams[player2id][2].value = parseInt(this.teams[player2id][2].value) + 1;

		this.teams[player1id][6].value = parseInt(this.teams[player1id][6].value) + score1;
		this.teams[player2id][6].value = parseInt(this.teams[player2id][6].value) + score2;

		this.teams[player1id][7].value = parseInt(this.teams[player1id][7].value) + score2;
		this.teams[player2id][7].value = parseInt(this.teams[player2id][7].value) + score1;

		this.player1points.value = "";
		this.player2points.value = "";

     //setup the new object
		var xmlhttp;
		if (window.XMLHttpRequest)
		    xmlhttp = new XMLHttpRequest();
		else
		    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

		//this.history += this.teams[player1id][0].value + " - " 	+ this.teams[player2id][0].value + " " + score1 + ":" + score2 + "<br>";
        // this.history += "teamab - teambb " + score1 + ":" + score2 + "<br>";

             fetch('save_pool_history.php?action=select_past').then((response)=>{
			     if (!response.ok)
			     {
			         throw new Error('Invalid Response');
			     }

			     return response.text();
			 }).then((text)=>{
			//	  alert('2nd alert' );
            this.history = text;
          let tourny_past = text;
			//     alert('1st alert' + text);
			//     alert('1-2 alert' + this.history);
			 }).catch((error)=>{
			     alert('Error fetching games history.');
            });

           // alert('2nd alert' );
           // this.history = text;

           // alert('tournypast' + tourny_past);
		//insert records into the database for all the games
		xmlhttp.open('GET',"save_pool_history.php?action=update&history=" + this.teams[player1id][0].value + "|||" 	+ this.teams[player2id][0].value + "|||" + score1 + "|||" + score2, true);
        xmlhttp.send();

	    // window.location.href("save_pool_history.php?history=" + this.teams[player1id][0].value + " - " 	+ this.teams[player2id][0].value + " " + score1 + ":" + score2);

		this.calculate();
		this.next();
		this.refresh();
	}

	this.sort();
	this.scoreTable.set(this.scores);
	if (this.showHistory) {
		this.historyCell.innerHTML ="<div><table><tr><td align='right'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button type='button'>OK</button></td></tr></table></div>"+
		                            "<div id=\"history_title\">History:</div>" +
									"<div id=\"history_past\">" + this.history + "</div>"  + // 441,489
									"<div id=\"history_future\">"+ this.showAllGames() + "</div>";  // 373

	} else {
		this.historyCell.innerHTML = "<div><table><tr><td align='right'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button type='button'>OK</button></td></tr></table></div>"+
		                              "Show History";
	}
}

Group.prototype.finish = function () {
	this.player1points.disabled = true;
	this.player1name.disabled = true;
	this.player2points.disabled = true;
	this.player2name.disabled = true;

	this.isFinish = true;
	finishCounter++;
}

Group.prototype.next = function () {
	if (this.gameCounter >= this.games.length - 1) {
		this.finish();
	} else {
		this.gameCounter++;
	}

	if (finishCounter == groupCounter) {
		modeConnector.show();
	}
}

Group.prototype.onchange = function (instance) {
	instance.refresh();
}

Group.prototype.calculate = function () {
	this.scores = new Array();

	for (var x = 0; x < this.size; x++) {
		var name = this.teams[x][0].value
		var score = this.teams[x][1].value
		var nog = this.teams[x][2].value;
		var wins = this.teams[x][3].value;
		var ties = this.teams[x][4].value;
		var loose = this.teams[x][5].value;
		var goals = this.teams[x][6].value;
		var caughts = this.teams[x][7].value;

		this.scores.push(new Array(name, score, nog, wins, ties, loose, goals, caughts));
	}
}

Group.prototype.button = function (box, instance) {
	if(box.checked) {

		instance.calculate();
		instance.scoreTable.show();
		instance.hide();
	} else {
		instance.scoreTable.hide();
		instance.show();
	}
	instance.refresh();
}

Group.prototype.show = function () {
	this.innerTable.style.display = "block";
}

Group.prototype.hide = function () {
	this.innerTable.style.display = "none";
}


Group.prototype.create = function (size, id, instance) {
	var table = document.createElement("table");
    table.setAttribute("border", "0");
    table.setAttribute("id", "machbox");

    var tbody = document.createElement("tbody");
    table.appendChild(tbody);

    var row = tbody.insertRow(0);
    var cell = row.insertCell(0);
    cell.setAttribute("id", "marker");
    cell.setAttribute("colspan", "2");
	cell.innerHTML = "Pool " + id;

	var row = tbody.insertRow(1);
    var cell = row.insertCell(0);
    cell.setAttribute("colspan", "2");
    cell.appendChild(this.scoreTable.get());

    var row = tbody.insertRow(2);
    var cell = row.insertCell(0);
    cell.setAttribute("colspan", "2");

    var offset = 3;

    this.innerTable = document.createElement("table");
    var innerBody = document.createElement("tbody");
    this.innerTable.appendChild(innerBody);
    cell.appendChild(this.innerTable);


    for (var x = 0; x < size; x++) {
    	var row = innerBody.insertRow(x);
		row.setAttribute("valign", "middle");

		var cellName = row.insertCell(0);

		var teamInput = document.createElement("input");
    	teamInput.setAttribute("type", "text");
		teamInput.setAttribute("size", "64");
		teamInput.setAttribute("id", "team_" + id + "_input_" + x);
    	teamInput.onchange = function() {
    		Group.prototype.onchange (instance);
    	}

    	cellName.appendChild(teamInput);

		var cellPoints = row.insertCell(1);

		var pointInput = document.createElement("input");
    	pointInput.setAttribute("type", "text");
    	pointInput.setAttribute("size", "2");
	    pointInput.setAttribute("maxlength", "2");
	    pointInput.setAttribute("value", "0");
	    pointInput.setAttribute("id", "result_" + id + "_input_" + x);
	    pointInput.onchange = function() {
    		Group.prototype.onchange (instance);
    	}
    	cellPoints.appendChild(pointInput);

    	var gamesInput = document.createElement("input");
    	gamesInput.setAttribute("value", "0");
    	gamesInput.setAttribute("id", "games_" + id + "_input_" + x);

    	var winInput = document.createElement("input");
    	winInput.setAttribute("value", "0");
    	winInput.setAttribute("id", "win_" + id + "_input_" + x);

    	var tieInput = document.createElement("input");
    	tieInput.setAttribute("value", "0");
    	tieInput.setAttribute("id", "tie_" + id + "_input_" + x);

    	var looseInput = document.createElement("input");
    	looseInput.setAttribute("value", "0");
    	looseInput.setAttribute("id", "loose_" + id + "_input_" + x);

    	var goalsInput = document.createElement("input");
    	goalsInput.setAttribute("value", "0");
    	goalsInput.setAttribute("id", "goals_" + id + "_input_" + x);

    	var caughtsInput = document.createElement("input");
    	caughtsInput.setAttribute("value", "0");
    	caughtsInput.setAttribute("id", "caughts_" + id + "_input_" + x);

    	this.teams.push(new Array(teamInput, pointInput, gamesInput, winInput, tieInput, looseInput, goalsInput, caughtsInput));
    }

    this.calculate();

    var row = tbody.insertRow(offset);
    var cell = row.insertCell(0);
    cell.setAttribute("colspan", "2");

	var chkbox = document.createElement('input');
	chkbox.setAttribute("type", "checkbox");
	chkbox.onchange = function () {
		Group.prototype.button (this, instance);
	}
    cell.appendChild(chkbox);

    var row = tbody.insertRow(1 + offset);
    var cell = row.insertCell(0);
    cell.setAttribute("id", "marker");
    cell.setAttribute("colspan", "2");
	cell.innerHTML = "Current Game:";

	var row = tbody.insertRow(2 + offset);

    var cell = row.insertCell(0);
   	this.player1name = document.createElement("input");
    this.player1name.setAttribute("type", "text");
    this.player1name.setAttribute("size", "60");
    cell.appendChild(this.player1name);

    var cell = row.insertCell(1);
    this.player1points = document.createElement("input");
    this.player1points.setAttribute("type", "text");
    this.player1points.setAttribute("size", "2");
	this.player1points.setAttribute("maxlength", "2");
	this.player1points.onchange = function() {
    	Group.prototype.onchange (instance);
    }
   	cell.appendChild(this.player1points);

	var row = tbody.insertRow(3 + offset);

	var cell = row.insertCell(0);
   	this.player2name = document.createElement("input");
    this.player2name.setAttribute("type", "text");
    this.player2name.setAttribute("size", "60");
    cell.appendChild(this.player2name);

    var cell = row.insertCell(1);
    this.player2points = document.createElement("input");
    this.player2points.setAttribute("type", "text");
    this.player2points.setAttribute("size", "2");
	this.player2points.setAttribute("maxlength", "2");
	this.player2points.onchange = function() {
    	Group.prototype.onchange (instance);
    }
   	cell.appendChild(this.player2points);


   	var row = tbody.insertRow(4 + offset);
    this.historyCell = row.insertCell(0);
    this.historyCell.setAttribute("colspan", "2");
	this.historyCell.setAttribute("class", "history");
	this.historyCell.innerHTML = "Start / View History";
	this.historyCell.onclick = function() {
    	Group.prototype.toggleHistory (instance);
    }

	return table;
}

Group.prototype.getTeams = function () {
	return this.scores;
}

Group.prototype.toggleHistory = function (instance) {
	instance.showHistory = !instance.showHistory;

	instance.refresh();
}

Group.prototype.get = function () {
	return this.table;
}

function createTable(player, groups, matches) {

	var table = document.createElement("table");
	var tbody = document.createElement("tbody");

	table.appendChild(tbody);
	table.setAttribute("border", "0");
	table.setAttribute("width", "100%");
	table.setAttribute("id", "sheet");

	var size = Math.ceil(Math.sqrt(groups));
	members = Math.ceil(player / groups);
	var counter = 1;

	for(var line = 0; line < size; line++) {
		var row = tbody.insertRow(line);
		row.setAttribute("valign", "top");

		for (var x = 0; x < size; x++) {
			var cell = row.insertCell(x);
			cell.setAttribute("align", "center");

			if (counter <= groups) {
				var groupInstance = new Group(members, counter, matches);

				groupList.push(groupInstance);
				cell.appendChild(groupInstance.get());
			}

			counter++;
		}
	}

	document.body.appendChild(table);

	modeConnector = new Connector(player, groups);
	document.body.appendChild(modeConnector.get());
}

 

 

The php it is calling is saving (inserting into tables) and also getting data by doing a select but its still using the array and can be out of sync.  any help would be appriciated.

 

 

Link to comment
Share on other sites

You've dumped a whole bunch of Javascript code and asked to "convert this from Javascript array".

You need to be a LOT more specific about what you want help with because nobody here is going to read through that whole thing and try to guess what you want.

Link to comment
Share on other sites

I appreciate you asking.  I'm having a hard time with the JavaScript part.  I know what it's doing is looping through an array based on info provided, see image.  The issue is the entire thing is looping through an array.  Also in the array I'm inserting records to save them.  Instead of the long array I want to pull from the database so it's no longer a cached issue

Screenshot_20230604-124356~2.png

Link to comment
Share on other sites

That's still not making a whole ton of sense.

The original code you've posted is using fetch to call to a php script, which I assume is pulling data from a database. It's presenting that data as an array - there's really no getting around that. One way or another you'll need an iterable interface in order to loop through the dataset to present the data. An array is the most convenient way; you could json-encode the data before php outputs it so that it comes across as an object in javascript, but really the way it's being used here that's mostly semantic. It sounds like there's something else going on that we don't know; is there caching involved? Like Redis or Memcached?

Explain what goal you're trying to achieve, not how you want to achieve it.

Link to comment
Share on other sites

let me step back and try again..

 

1. this entire thing has been javascript from the start.  I can tell its using some sort of array but im not super great at javascript.

2.  I added the php parts as it was not storing the results other than cache in the array.  it is storing the data just fine and also able to pull the data from the database as well.

3.  if i left it as is from the original (javascript only)  and if someone only did Team1 and Team2.  walked away and came back to it later, it timed out and the cache is gone.  this is why i wanted to store it on the database (which is why you now see php mixed in)

4.  the code even though it saves and pulls from php/mysql, it still is looping through the cached array as the main driver but it starts over as it doesnt know where it left off.  what i would like is to at minimum load the array each time from the database instead.  currently it keeps scores of the teams, so i can do a query with something like:

 

select * from table where score1 =null and score2 =null

 

but im having trouble figuring out the array load part in javascript.

 

Does this make more sense?   and thank you for your help so far by the way

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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