我的代码的问题是当我在for循环中打印php ajax div时,当循环有2个索引时,我得到一个循环索引。
我的代码示例:
的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>
我尝试将代码放在循环ajax中但我无法使其工作。
file1.php
echo htmlspecialchars($_GET["row"]);
抱歉我的错误解释...在ajax代码更改中:
var url = "./file1.php?row=[$k]";
并在循环中添加。
我的代码不好:/
答案 0 :(得分:0)
请认真对待这个建议:阅读有关编程的更多信息。特别是基本知识,如:变量,函数和范围。如果您要继续使用javascript,请了解闭包(在学习其他内容之后)。
我认为你试图让PHP代码为每个“live”div打印一个单独的函数。您可以将div作为函数的参数,然后为每个div调用该函数。
我在代码中添加了一些关于代码的注释。请务必在阅读后将其删除(除非您发现它们对记忆有用:)
$('#<%= dom_id(@post) %>').replaceWith("<%= j render partial 'calendars/post_partial', object: @post %>");