2015-10-19 50 views
1

我的代码问题环路是,当我打印里面的for循环的PHP AJAX格,我得到循环的一个单一的指标当循环指数为2。我的代码PHP与AJAX格自动刷新

例如:

的index.php

<html> 
<head> 
<title>DeportesRulz.com</title> 
<link charset="utf-8" href="./css/tablaodds.css" media="screen" rel="stylesheet" type="text/css" /> 
<script> 
      function AJAX(){ 
      try{ 
      xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari 
      return xmlHttp; 
      } 
      catch (e){ 
      try{ 
      xmlHttp=new ActiveXObject('Msxml2.XMLHTTP'); // Internet Explorer 
      return xmlHttp; 
      } 
      catch (e){ 
      try{ 
      xmlHttp=new ActiveXObject('Microsoft.XMLHTTP'); 
      return xmlHttp; 
      } 
      catch (e){ 
      alert('Your browser does not support AJAX.'); 
      return false; 
      } 
      } 
      } 
      } 


      // Timestamp for preventing IE caching the GET request (common function) 

      function fetch_unix_timestamp() 
      { 
       return parseInt(new Date().getTime().toString().substring(0, 10)) 
      } 
<?php 
//Obtener fecha 
date_default_timezone_set('America/Caracas'); 
$fechaabrev = strftime("Sat, %b 17, %y"); 
//[Variables globales] 
$xml = simplexml_load_file('./MLB.xml'); 
$juegos = count($xml->EventType->Date); 
//Obtener todos los juegos 
for ($i=0;$i<=$juegos-1;$i++) { 
    //Obtener solo juegos del dia 
    if ($xml->EventType->Date[$i]->attributes()->DTEXT == $fechaabrev){ 
     $juegosdeldia = count($xml->EventType->Date[$i]->Event); 
     //Bucle principal juegos del dia 
     for ($k=0;$k<=$juegosdeldia-1;$k++) { 
      $juego_dia = $xml->EventType->Date[$i]->attributes()->DTEXT.' '; 
      $juego_horaf = $xml->EventType->Date[$i]->Event[$k]->Time->attributes()->TTEXT.' '; 
      $juego_horaf = explode(" ", $juego_horaf); 
      $juego_hora = $juego_horaf[0]; 
      $juego_live = $xml->EventType->Date[$i]->Event[$k]->attributes()->LIVE_ENABLED.' '; 
      $juego_ligaf = $xml->EventType->Date[$i]->Event[$k]->NOTE.' '; 
      $juego_ligaf = explode(" ", $juego_ligaf); 
      if ($juego_ligaf[0] == 'AMERICAN'){ 
       $juego_liga = 'American League'; 
      } else if ($juego_ligaf[0] == 'NATIONAL'){ 
       $juego_liga = 'National League'; 
      } 

      $equipos = count($xml->EventType->Date[$i]->Event[$k]->Competitor); 
      //Bucle equipos 
      for ($j=0;$j<=$equipos-1;$j++) { 
       //Obtener equipo A 
       if ($xml->EventType->Date[$i]->Event[$k]->Competitor[$j]->attributes()->NUM == 2){ 
        $equipo_a = $xml->EventType->Date[$i]->Event[$k]->Competitor[$j]->attributes()->NAME; 
       }//--Fin Obtener equipo A-- 
       //Obtener equipo B 
       if ($xml->EventType->Date[$i]->Event[$k]->Competitor[$j]->attributes()->NUM == 1){ 
        $equipo_b = $xml->EventType->Date[$i]->Event[$k]->Competitor[$j]->attributes()->NAME; 
       }//--Fin Obtener equipo B-- 
      }//--Fin Bucle equipos-- 

//--------------------------------------------[Imprimiendo datos]---------------------------------------------------------// 

      $headscript = " 

      function refreshdiv_otherdiv(){ 

      // Customise those settings 

      var seconds = 5; 
      var divid = 'live[$k]'; 
      var url = './file1.php?row=[$k]'; 

      var xmlHttp_three = AJAX(); 

      // No cache 

      var timestamp = fetch_unix_timestamp(); 
      var nocacheurl = url+'?t='+timestamp; 


      // The code... 

      xmlHttp_three.onreadystatechange=function(){ 
      if(xmlHttp_three.readyState==4){ 
      document.getElementById(divid).innerHTML=xmlHttp_three.responseText; 
      setTimeout('refreshdiv_otherdiv()',seconds*1000); 
      } 
      } 
      xmlHttp_three.open('GET',nocacheurl,true); 
      xmlHttp_three.send(null); 
      } 

      // Start the refreshing process 

      var seconds; 
      window.onload = function startrefresh(){ 
      setTimeout('refreshdiv_otherdiv()',seconds*1000); 
      } 
      "; 
      if ($juegosdeldia == 1){ 
       echo "$headscript</script></head><body><script type='text/javascript'>refreshdiv_live[$k]();</script><table cellpadding='3' cellspacing='1' class='tablehead' style='background-color:white;'><tbody><tr class='stathead'><td colspan='5' style='background-color:#0B3861;color:white;'>$equipo_a vs $equipo_b | [MLB] $juego_liga <div name='live[$k]' id='live[$k]'></div></tr><tr class='colhead' style='text-align:center;'><td>$juego_hora</td><td>A GANAR</td><td>ALTA/BAJA (6.5)</td><td>RUN LINE</td><td>ALTA/BAJA (3.5 | 5to inning)</td></tr><div id='odds'></div></tbody></table></body></html>"; 
      } else if ($juegosdeldia > 1){ 
       if ($k == 0){ 
        echo "$headscript"; 
       } else if ($k > 0){ 
        echo "$headscript</script></head><body><script type='text/javascript'>refreshdiv_live[$k]();</script><table cellpadding='3' cellspacing='1' class='tablehead' style='background-color:white;'><tbody><tr class='stathead'><td colspan='5' style='background-color:#0B3861;color:white;'>$equipo_a vs $equipo_b | [MLB] $juego_liga <div name='live[$k]' id='live[$k]'></div></tr><tr class='colhead' style='text-align:center;'><td>$juego_hora</td><td>A GANAR</td><td>ALTA/BAJA (6.5)</td><td>RUN LINE</td><td>ALTA/BAJA (3.5 | 5to inning)</td></tr><div id='odds'></div></tbody></table></body></html>"; 
       } 
      } 
      echo ""; 


//------------------------------------------[Fin Imprimiendo datos]---------------------------------------------------------// 

     }//--Fin Bucle principal juegos del dia-- 
    }//--Fin Obtener solo juegos del dia-- 
}//--Fin Obtener todos los juegos-- 

?> 

MLB.xml

<Schedule PUBLISH_DATE="10/18/2015" PUBLISH_TIME="08:02" TS="1445212923955"><EventType GROUP="E" ID="MLB" NAME="Baseball - MLB" SPORT="B"><Date DTEXT="Fri, Oct 16, 15" TS="1444968000000"><Event DenySameGame="false" GAME_STATUS="" ID="687326" LIVE_ENABLED="true" LineOrder="D" NAME="Toronto Blue Jays @ Kansas City Royals" PXP_GAME_ID="8228712" Publish="true" STATUS="Final"><Competitor ID="687326-1633" NAME="Kansas City Royals" NUM="1" ROT="952"><Line ORDER="2" TYPE="Moneyline"><Choice ID="65735691" NUMBER="" VALUE="+116"><Odds FRACTION="23/20" Line="+116" MULTIPLIER="2.16" RISK="100" WIN="116"/></Choice></Line><Line ORDER="1" TYPE="Runline"><Choice ID="65726357" NUMBER="1½" VALUE="+1½ (-140)"><Odds FRACTION="8/11" Line="-140" MULTIPLIER="1.714" RISK="140" WIN="100"/></Choice></Line></Competitor><Competitor ID="687326-9999" NAME="Toronto Blue Jays" NUM="2" ROT="951"><Line ORDER="2" TYPE="Moneyline"><Choice ID="65735690" NUMBER="" VALUE="-126"><Odds FRACTION="3/4" Line="-126" MULTIPLIER="1.794" RISK="126" WIN="100"/></Choice></Line><Line ORDER="1" TYPE="Runline"><Choice ID="65726356" NUMBER="-1½" VALUE="-1½ (+120)"><Odds FRACTION="6/5" Line="+120" MULTIPLIER="2.20" RISK="100" WIN="120"/></Choice></Line></Competitor><NOTE>AMERICAN LEAGUE - CHAMPIONSHIP SERIES - GAME #1</NOTE><Time TTEXT="08:07PM EDT" TS="1445040420000"/><Line ORDER="3" TYPE="Total"><Choice ID="65734371" NUMBER="8.0" VALUE="+8.0 (-110)"><Odds FRACTION="10/11" Line="-110" MULTIPLIER="1.909" RISK="110" WIN="100"/>Over</Choice><Choice ID="65734372" NUMBER="8.0" VALUE="+8.0 (-110)"><Odds FRACTION="10/11" Line="-110" MULTIPLIER="1.909" RISK="110" WIN="100"/>Under</Choice></Line></Event></Date><Date DTEXT="Sat, Oct 17, 15" TS="1445054400000"><Event DenySameGame="false" GAME_STATUS="" ID="687327" LIVE_ENABLED="true" LineOrder="D" NAME="Toronto Blue Jays @ Kansas City Royals" PXP_GAME_ID="8228714" Publish="true" STATUS="Final"><Competitor ID="687327-1633" NAME="Kansas City Royals" NUM="1" ROT="956"><Line ORDER="2" TYPE="Moneyline"><Choice ID="66123598" NUMBER="" VALUE="+139"><Odds FRACTION="11/8" Line="+139" MULTIPLIER="2.39" RISK="100" WIN="139"/></Choice></Line><Line ORDER="1" TYPE="Runline"><Choice ID="66118320" NUMBER="1½" VALUE="+1½ (-115)"><Odds FRACTION="17/20" Line="-115" MULTIPLIER="1.87" RISK="115" WIN="100"/></Choice></Line></Competitor><Competitor ID="687327-9999" NAME="Toronto Blue Jays" NUM="2" ROT="955"><Line ORDER="2" TYPE="Moneyline"><Choice ID="66123597" NUMBER="" VALUE="-154"><Odds FRACTION="8/13" Line="-154" MULTIPLIER="1.649" RISK="154" WIN="100"/></Choice></Line><Line ORDER="1" TYPE="Runline"><Choice ID="66118319" NUMBER="-1½" VALUE="-1½ (-105)"><Odds FRACTION="20/21" Line="-105" MULTIPLIER="1.952" RISK="105" WIN="100"/></Choice></Line></Competitor><NOTE>AMERICAN LEAGUE CHAMPIONSHIP SERIES GAME #2</NOTE><Time TTEXT="04:07PM EDT" TS="1445112420000"/><Line ORDER="3" TYPE="Total"><Choice ID="66104749" NUMBER="7½" VALUE="+7½ (+115)"><Odds FRACTION="23/20" Line="+115" MULTIPLIER="2.15" RISK="100" WIN="115"/>Over</Choice><Choice ID="66104750" NUMBER="7½" VALUE="+7½ (-135)"><Odds FRACTION="8/11" Line="-135" MULTIPLIER="1.741" RISK="135" WIN="100"/>Under</Choice></Line></Event><Event DenySameGame="false" GAME_STATUS="" ID="688238" LIVE_ENABLED="true" LineOrder="D" NAME="Chicago Cubs @ New York Mets" PXP_GAME_ID="8232308" Publish="true" STATUS="Closed"><Competitor ID="688238-10457" NAME="New York Mets" NUM="1" ROT="954"><Line ORDER="2" TYPE="Moneyline"><Choice ID="66200407" NUMBER="" VALUE="-110"><Odds FRACTION="10/11" Line="-110" MULTIPLIER="1.909" RISK="110" WIN="100"/></Choice></Line><Line ORDER="1" TYPE="Runline"><Choice ID="66167912" NUMBER="-1½" VALUE="-1½ (+190)"><Odds FRACTION="19/10" Line="+190" MULTIPLIER="2.90" RISK="100" WIN="190"/></Choice></Line></Competitor><Competitor ID="688238-10395" NAME="Chicago Cubs" NUM="2" ROT="953"><Line ORDER="2" TYPE="Moneyline"><Choice ID="66200406" NUMBER="" VALUE="EVEN"><Odds FRACTION="1/1" Line="EVEN" MULTIPLIER="2.00" RISK="100" WIN="100"/></Choice></Line><Line ORDER="1" TYPE="Runline"><Choice ID="66167911" NUMBER="1½" VALUE="+1½ (-230)"><Odds FRACTION="2/5" Line="-230" MULTIPLIER="1.435" RISK="230" WIN="100"/></Choice></Line></Competitor><NOTE>NATIONAL LEAGUE CHAMPIONSHIP SERIES GAME #1</NOTE><Time TTEXT="08:07PM EDT" TS="1445126820000"/><WagerStopTime TTEXT="11:44PM EDT" TS="1445139857447"/><Line ORDER="3" TYPE="Total"><Choice ID="66201325" NUMBER="6.0" VALUE="+6.0 (-110)"><Odds FRACTION="10/11" Line="-110" MULTIPLIER="1.909" RISK="110" WIN="100"/>Over</Choice><Choice ID="66201326" NUMBER="6.0" VALUE="+6.0 (-110)"><Odds FRACTION="10/11" Line="-110" MULTIPLIER="1.909" RISK="110" WIN="100"/>Under</Choice></Line></Event></Date><Date DTEXT="Sun, Oct 18, 15" TS="1445140800000"><Event DenySameGame="false" GAME_STATUS="" ID="688239" LIVE_ENABLED="true" LineOrder="D" NAME="Chicago Cubs @ New York Mets" PXP_GAME_ID="8232310" Publish="true" STATUS="Open"><Competitor ID="688239-10457" NAME="New York Mets" NUM="1" ROT="958"><Line ORDER="2" TYPE="Moneyline"><Choice ID="66645325" NUMBER="" VALUE="+138"><Odds FRACTION="11/8" Line="+138" MULTIPLIER="2.38" RISK="100" WIN="138"/></Choice></Line><Line ORDER="1" TYPE="Runline"><Choice ID="66611035" NUMBER="1½" VALUE="+1½ (-135)"><Odds FRACTION="8/11" Line="-135" MULTIPLIER="1.741" RISK="135" WIN="100"/></Choice></Line></Competitor><Competitor ID="688239-10395" NAME="Chicago Cubs" NUM="2" ROT="957"><Line ORDER="2" TYPE="Moneyline"><Choice ID="66645324" NUMBER="" VALUE="-153"><Odds FRACTION="4/6" Line="-153" MULTIPLIER="1.654" RISK="153" WIN="100"/></Choice></Line><Line ORDER="1" TYPE="Runline"><Choice ID="66611034" NUMBER="-1½" VALUE="-1½ (+115)"><Odds FRACTION="23/20" Line="+115" MULTIPLIER="2.15" RISK="100" WIN="115"/></Choice></Line></Competitor><NOTE>NATIONAL LEAGUE CHAMPIONSHIP SERIES New York Mets leads 1-0</NOTE><Time TTEXT="08:07PM EDT" TS="1445213220000"/><Line ORDER="3" TYPE="Total"><Choice ID="66630752" NUMBER="5½" VALUE="+5½ (-110)"><Odds FRACTION="10/11" Line="-110" MULTIPLIER="1.909" RISK="110" WIN="100"/>Over</Choice><Choice ID="66630753" NUMBER="5½" VALUE="+5½ (-110)"><Odds FRACTION="10/11" Line="-110" MULTIPLIER="1.909" RISK="110" WIN="100"/>Under</Choice></Line></Event></Date><Date DTEXT="Mon, Oct 19, 15" TS="1445227200000"><Event DenySameGame="false" GAME_STATUS="" ID="687328" LIVE_ENABLED="false" LineOrder="D" NAME="Kansas City Royals @ Toronto Blue Jays" PXP_GAME_ID="8228716" Publish="true" STATUS="Open"><Competitor ID="687328-9999" NAME="Toronto Blue Jays" NUM="1" ROT="960"><Line ORDER="2" TYPE="Moneyline"><Choice ID="66645502" NUMBER="" VALUE="-170"><Odds FRACTION="4/7" Line="-170" MULTIPLIER="1.588" RISK="170" WIN="100"/></Choice></Line><Line ORDER="1" TYPE="Runline"><Choice ID="66604018" NUMBER="-1½" VALUE="-1½ (+115)"><Odds FRACTION="23/20" Line="+115" MULTIPLIER="2.15" RISK="100" WIN="115"/></Choice></Line></Competitor><Competitor ID="687328-1633" NAME="Kansas City Royals" NUM="2" ROT="959"><Line ORDER="2" TYPE="Moneyline"><Choice ID="66645501" NUMBER="" VALUE="+150"><Odds FRACTION="6/4" Line="+150" MULTIPLIER="2.50" RISK="100" WIN="150"/></Choice></Line><Line ORDER="1" TYPE="Runline"><Choice ID="66604019" NUMBER="1½" VALUE="+1½ (-145)"><Odds FRACTION="4/6" Line="-145" MULTIPLIER="1.69" RISK="145" WIN="100"/></Choice></Line></Competitor><NOTE>AMERICAN LEAGUE CHAMPIONSHIP SERIES - Kansas City lead series 2-0</NOTE><Time TTEXT="08:07PM EDT" TS="1445299620000"/><Line ORDER="3" TYPE="Total"><Choice ID="66604027" NUMBER="8.0" VALUE="+8.0 (-115)"><Odds FRACTION="17/20" Line="-115" MULTIPLIER="1.87" RISK="115" WIN="100"/>Over</Choice><Choice ID="66604026" NUMBER="8.0" VALUE="+8.0 (-115)"><Odds FRACTION="17/20" Line="-115" MULTIPLIER="1.87" RISK="115" WIN="100"/>Under</Choice></Line></Event></Date><Date DTEXT="Tue, Oct 20, 15" TS="1445313600000"><Event DenySameGame="false" GAME_STATUS="" ID="687329" LIVE_ENABLED="false" LineOrder="D" NAME="Kansas City Royals @ Toronto Blue Jays" PXP_GAME_ID="8228718" Publish="true" STATUS="Open"><Competitor ID="687329-9999" NAME="Toronto Blue Jays" NUM="1" ROT=""><Line ORDER="2" TYPE="Moneyline"/><Line ORDER="1" TYPE="Runline"/></Competitor><Competitor ID="687329-1633" NAME="Kansas City Royals" NUM="2" ROT=""><Line ORDER="2" TYPE="Moneyline"/><Line ORDER="1" TYPE="Runline"/></Competitor><Time TTEXT="04:07PM EDT" TS="1445371620000"/><Line ORDER="3" TYPE="Total"/></Event><Event DenySameGame="false" GAME_STATUS="" ID="688237" LIVE_ENABLED="false" LineOrder="D" NAME="New York Mets @ Chicago Cubs" PXP_GAME_ID="8232312" Publish="true" STATUS="Open"><Competitor ID="688237-10395" NAME="Chicago Cubs" NUM="1" ROT=""><Line ORDER="2" TYPE="Moneyline"/><Line ORDER="1" TYPE="Runline"/></Competitor><Competitor ID="688237-10457" NAME="New York Mets" NUM="2" ROT=""><Line ORDER="2" TYPE="Moneyline"/><Line ORDER="1" TYPE="Runline"/></Competitor><Time TTEXT="08:07PM EDT" TS="1445386020000"/><Line ORDER="3" TYPE="Total"/></Event></Date><Date DTEXT="Wed, Oct 21, 15" TS="1445400000000"><Event DenySameGame="false" GAME_STATUS="" ID="688236" LIVE_ENABLED="false" LineOrder="D" NAME="New York Mets @ Chicago Cubs" PXP_GAME_ID="8232314" Publish="true" STATUS="Open"><Competitor ID="688236-10395" NAME="Chicago Cubs" NUM="1" ROT=""><Line ORDER="2" TYPE="Moneyline"/><Line ORDER="1" TYPE="Runline"/></Competitor><Competitor ID="688236-10457" NAME="New York Mets" NUM="2" ROT=""><Line ORDER="2" TYPE="Moneyline"/><Line ORDER="1" TYPE="Runline"/></Competitor><Time TTEXT="08:07PM EDT" TS="1445472420000"/><Line ORDER="3" TYPE="Total"/></Event></Date><SEGMENT CODE="MLBWG" SEQUENCE_NUMBER="1" SHORT_NAME="Game" NAME="Whole Game" EventType="MLB"/></EventType></Schedule> 

我试图把代码中的循环阿贾克斯内,但我不能做这项工作。

file1.php

echo htmlspecialchars($_GET["row"]); 

对不起,我不好解释...在Ajax代码变化:

var url = "./file1.php?row=[$k]"; 

并添加循环中。

我的代码是坏的:/

+1

哪里是'file1.php',而你是不是这个代码示例中使用'jquery',虽然你的标题和标签这样说的。 – bonesbrigade

+0

你在说什么php ajax?是在file1.php?另外,'refreshdiv_live()'定义在哪里? – Terminus

+0

@bonesbrigade抱歉我的英文不好,file1.php通过GET参数获取循环的行数。 – jtox

回答

0

请认真对待这个建议:阅读关于编程的更多信息。特别是基础知识如:变量,函数和范围。如果您要继续使用JavaScript,请了解关闭(了解其他内容后)。

我认为你试图有PHP代码打印一个“活”的div一个单独的函数。您可以将div创建为函数的参数,然后调用每个div的函数。

大约有,我已经把尽可能代码中的注释代码中的一些注意事项。请务必阅读它们之后将其删除(除非你发现它们非常有用记住:)

// don't forget to add a doctype. since you didn't i'm picking HTML5 
<!DOCTYPE html> 
<html> 
<head> 
// also add a character set 
<meta charset='utf-8'> 
<title>DeportesRulz.com</title> 
<script> 
    function AJAX() { 
     try{ 
      xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari 
      return xmlHttp; 
     } 
     catch (e) { 
      try{ 
      xmlHttp=new ActiveXObject('Msxml2.XMLHTTP'); // Internet Explorer 
      return xmlHttp; 
      } 
      catch (e) { 
       try{ 
        xmlHttp=new ActiveXObject('Microsoft.XMLHTTP'); 
        return xmlHttp; 
       } 
       catch (e) { 
        alert('Your browser does not support AJAX.'); 
        return false; 
       } 
      } 
     } 
    } 

    // Timestamp for preventing IE caching the GET request (common function) 
    function fetch_unix_timestamp() { 
     return parseInt(new Date().getTime().toString().substring(0, 10)) 
    } 

    //takes a div element as a parameter. the data-rownum on that div will be used as the rownumber passed to file1.php 
    function refreshLiveStatus(targetDiv) { 
     // access the data-rownum attribute of the targetDiv 
     var rowNum = targetDiv.dataset['rownum']; 
     // if you don't need the square brackets, remove them 
     var url = './file1.php?row=[' + rowNum + ']'; 

     var xmlHttp_three = AJAX(); 

     // No cache 
     var timestamp = fetch_unix_timestamp(); 
     // you had `url+'?t='+timestamp` here before. you can't have multiple '?' in a url like that. you need to use `&` as below 
     var nocacheurl = url+'&t='+timestamp; 

     // The code... 
     xmlHttp_three.onreadystatechange = function() { 
      if(xmlHttp_three.readyState==4) { 
       targetDiv.innerHTML=xmlHttp_three.responseText; 
       // start the next refresh 
       setTimeout(function() { 
        refreshLiveStatus(targetDiv); 
       },seconds); 
      } 
     } 
     xmlHttp_three.open('GET',nocacheurl,true); 
     xmlHttp_three.send(null); 
    } 

    var seconds = 5; // just set it here so you don't have to keep setting the variable (unless you want different refresh rates??) 
    seconds *= 1000; // if they're seconds make them seconds and save yourself the trouble of having to remember to do `* 1000` everywhere 
    // Start the refreshing process 
    // function to replace window.onload below. It's not necessary to replace window.onload but this will let you easily add more than one function to the window.onload 
    // console logs left in for science 
    function ready(fn) { 
     console.log('readying'); 
     if (document.readyState != 'loading') { 
      console.log('loading...'); 
      fn(); 
     } else { 
      console.log('content not loaded'); 
      document.addEventListener('DOMContentLoaded', fn); 
     } 
    } 

    ready(function() { 
     // querySelectorAll lets you use CSS selectors to select HTML elements. Note that the `liveStatus` class is added in the PHP code below. 
     var divs = document.querySelectorAll('div.liveStatus'); 
     console.log(divs); // left here for you to what's selected 
     for(var i = 0, imax = divs.length; i < imax; i++) { 
      // the below is a closure. definitely read about these if you don't know what they are already 
      (function(liveDiv) { 
       refreshLiveStatus(liveDiv); 
      })(divs[i]); 
     } 
    }); 

    // style the .liveStatus div. leave it out and see what happens 
    </script> 
    <style type='text/css'> 
     div.liveStatus { 
      display: inline-block; 
     } 
    </style> 
</head> 
<body> 
<?php 
//Obtener fecha 
date_default_timezone_set('America/Caracas'); 
$fechaabrev = strftime("Sat, %b 17, %y"); 
//[Variables globales] 
$xml = simplexml_load_file('./MLB.xml'); 
$juegos = count($xml->EventType->Date); 
//Obtener todos los juegos 
// you can use `$i<$juegos` instead of `$i<=$juegos-1` and save having to do that subtraction 
for ($i=0;$i<$juegos;$i++) { 
    //Obtener solo juegos del dia 
    if ($xml->EventType->Date[$i]->attributes()->DTEXT == $fechaabrev) { 
     $juegosdeldia = count($xml->EventType->Date[$i]->Event); 
     //Bucle principal juegos del dia 
     for ($k=0;$k<$juegosdeldia;$k++) { 
      $juego_dia = $xml->EventType->Date[$i]->attributes()->DTEXT.' '; 
      $juego_horaf = $xml->EventType->Date[$i]->Event[$k]->Time->attributes()->TTEXT.' '; 
      $juego_horaf = explode(" ", $juego_horaf); 
      $juego_hora = $juego_horaf[0]; 
      $juego_live = $xml->EventType->Date[$i]->Event[$k]->attributes()->LIVE_ENABLED.' '; 
      $juego_ligaf = $xml->EventType->Date[$i]->Event[$k]->NOTE.' '; 
      $juego_ligaf = explode(" ", $juego_ligaf); 
      if ($juego_ligaf[0] == 'AMERICAN') { 
       $juego_liga = 'American League'; 
      } else if ($juego_ligaf[0] == 'NATIONAL') { 
       $juego_liga = 'National League'; 
      } 

      $equipos = count($xml->EventType->Date[$i]->Event[$k]->Competitor); 
      //Bucle equipos 
      for ($j=0;$j<=$equipos-1;$j++) { 
       //Obtener equipo A 
       if ($xml->EventType->Date[$i]->Event[$k]->Competitor[$j]->attributes()->NUM == 2) { 
        $equipo_a = $xml->EventType->Date[$i]->Event[$k]->Competitor[$j]->attributes()->NAME; 
       }//--Fin Obtener equipo A-- 
       //Obtener equipo B 
       if ($xml->EventType->Date[$i]->Event[$k]->Competitor[$j]->attributes()->NUM == 1) { 
        $equipo_b = $xml->EventType->Date[$i]->Event[$k]->Competitor[$j]->attributes()->NAME; 
       }//--Fin Obtener equipo B-- 
      }//--Fin Bucle equipos-- 

//--------------------------------------------[Imprimiendo datos]---------------------------------------------------------// 
      // note how we add the rownumber with `...[" . ($k+1) . "]...` 
      // also see how we added the `liveStatus` class AND the data-rownum attribute to the "live" div 
      echo " 
    <table cellpadding='3' cellspacing='1' class='tablehead' style='background-color:white;'> 
     <tbody> 
      <tr class='stathead'> 
       <td colspan='5' style='background-color:#0B3861;color:white;'> 
        [" . ($k+1) . "] $equipo_a vs $equipo_b | [MLB] $juego_liga 
        <div class='liveStatus' name='live[$k]' data-rownum='$k' id='live[$k]'></div> 
       </td> 
      </tr> 
      <tr class='colhead' style='text-align:center;'> 
       <td>$juego_hora</td> 
       <td>A GANAR</td> 
       <td>ALTA/BAJA (6.5)</td> 
       <td>RUN LINE</td> 
       <td> 
        ALTA/BAJA (3.5 | 5to inning) 
        <div id='odds'></div> 
       </td> 
      </tr> 
     </tbody> 
    </table>"; 


//------------------------------------------[Fin Imprimiendo datos]---------------------------------------------------------// 

     }//--Fin Bucle principal juegos del dia-- 
    }//--Fin Obtener solo juegos del dia-- 
}//--Fin Obtener todos los juegos-- 

?> 

</body> 
</html> 
+0

哦,非常感谢!如果我可以给你许多观点,它已经很完美了,再次感谢花时间再次解释和制作代码。 – jtox