Alright. I simplified the code in the previous example because I’m not sure I can make it clear what the code is aiming to do without pasting pages of code. Here is an abridged version.
It starts with PHP getting the mySQL table contents, and showing that code is probably the best way to explain what keys the array has:
function load_terrain_contents()
{
global $db;
$map_list = $db->prepare('SELECT * FROM terrain_contents');
$map_list->execute();
$listcounter=1;
foreach($map_list as $terrain)
{
$list['Terrain'][$listcounter] = (int) $terrain['Terrain'];
$list['Nothing'][$listcounter] = (int) $terrain['Nothing'];
$list['Settlement'][$listcounter] = (int) $terrain['Settlement'];
$list['Cave'][$listcounter] = (int) $terrain['Cave'];
$list['Graveyard'][$listcounter] = (int) $terrain['Graveyard'];
$list['Fortress'][$listcounter] = (int) $terrain['Fortress'];
$list['Tower'][$listcounter] = (int) $terrain['Tower'];
$list['Temple'][$listcounter] = (int) $terrain['Temple'];
$list['Mine'][$listcounter] = (int) $terrain['Mine'];
$list['Ruin'][$listcounter] = (int) $terrain['Ruin'];
$listcounter++;
}
$map_list->closeCursor();
}
Then in the file for the webpage near the top it has a couple lines (omitting non-relevant lines here) that convert this to a javascript array (except that I think it becomes an Object due to the source array being mulltidimensional):
$terrain_contents = load_terrain_contents();
$js_array = json_encode($terrain_contents);
echo ("var terrain_contents = ".$js_array.";\n";
Later on in the webpage file’s long SCRIPT block in the HEAD… As I mentioned in the previous code, I found a workaround so there is no use of findIndex anymore.
var i;
var j;
var contents = [];
contents["key"] = [];
contents["weight"] = [];
for(i=0;i<Object.keys(terrain_contents).length-1;i++) // -1 because skipping the ['Terrain'] key itself
{
contents["key"][i] = i+1;
for(j=0;j<Object.keys(terrain_contents["Terrain"]).length;j++)
{
if(terrain_contents["Terrain"][j+1] == document.getElementById("test_tile_value").value)
{
contents["weight"][i] = terrain_contents[getContentsfromIndex(i+1)][j+1];
}
}
}
If I had been able to use fileIndex the way I wanted, I would have been able to avoid nested for loops there.