-1

I want to create a title description for a chess eco code:

This is the lookup array in php named eco_codes:

 <?php
 $eco_tmp = array(
 "A00" => "Uncommon Opening 1.g4, a3, h3, etc.",
 "A01" => "Nimzovich-Larsen Attack 1.b3",
 "A02" => "Bird's Opening 1.f4",
 "A03" => "Bird's Opening 1.f4 d5")
 ?>

I have the lookup code (ex. GameECO = "A02") like this:

 <span id="GameECO"></span> 

What I want is this:

 <?php $key = array_search($val['GameECO'], $eco_codes);?>

 <span id="GameECO" title = "<?php echo $key.$val['ECO']></span> 

How can I proceed? Is php even required for this? Maybe js alone would do it?

my page is http://communitychessclub.com/basic.php?game=5312

verlager
  • 794
  • 5
  • 25
  • 43

1 Answers1

0

Assuming you have the code in some variable, have a js file (ecocodes.js) containing

  var ecoCodes = { 
      "A00":"Uncommon Opening 1.g4, a3, h3, etc.",
      "A01":"Nimzovich-Larsen Attack 1.b3",
      "A02":"Bird's Opening 1.f4",
      "A03":"Bird's Opening 1.f4 d5"
    } // no comma after the last one

then you can use

<!DOCTYPE html>
<html>
  <head>
    <title>Chess</title>
    <script src="ecocodes.js"></script>
    <script>
      window.onload=function() {
        var eco = "A02";
        document.getElementById("GameECO").innerHTML=eco + ":"+ecoCodes[eco];
      }  
    </script>
  </head>
  <body>  
  ECO: <span id="GameECO"></span>
  </body>
</html>

It works like this:

  var ecoCodes = { 
      "A00":"Uncommon Opening 1.g4, a3, h3, etc.",
      "A01":"Nimzovich-Larsen Attack 1.b3",
      "A02":"Bird's Opening 1.f4",
      "A03":"Bird's Opening 1.f4 d5"
    } // no comma after the last one

window.onload=function() {
    var eco = "A02";
    document.getElementById("GameECO").innerHTML=eco + ":"+ecoCodes[eco];
}  
ECO: <span id="GameECO"></span>
mplungjan
  • 169,008
  • 28
  • 173
  • 236