2013-10-05 65 views
0

PHP - MySQL - Javascript如何在满足条件时循环while循环和行变量?

我有一个javascript手风琴,我想填充基于MySQL查询的数据。手风琴的一个例子(现在手动更新)可以在这里找到:http://www.vi-athletics.com/schedules/mens-soccer-schedule-2013-14?id=56

我已经写了一些代码(看看它在http://www.vi-athletics.com/schedules/mens-soccer-schedule-2013-14?id=55上做了什么),但是,我不确定如何继续。我需要手风琴始终与本周的日期在一起,并且要么包含那天的游戏行,要么说今天没有游戏。

我还需要显示按他们的运动分组的游戏。举例来说,如果9月10日有两场男子足球比赛和一场排球比赛,那么我需要桌子上有一个表格行,其中有一个表格行(每个游戏),然后是表格行运动(就像我的例子)。如果当天没有安排比赛,那么我需要看看是否有比赛计划在第二天等等...

我在想的是继续写每个手风琴的声明,但我认为我'对于每一行出现$ sport =“value”的运动类型都会有问题。 这里是我到目前为止的代码:

<?php 
/* create the days of the week */ 
date_default_timezone_set('US/Eastern'); 
$today = time(); 
$weekStartDate = date('l, F d, Y',strtotime("last Monday", $today)); 
$weekTuesDate = date('l, F d, Y', strtotime('+1 days', strtotime($weekStartDate))); 
$weekWedDate = date('l, F d, Y', strtotime('+2 days', strtotime($weekStartDate))); 
$weekThursDate = date('l, F d, Y', strtotime('+3 days', strtotime($weekStartDate))); 
$weekFriDate = date('l, F d, Y', strtotime('+4 days', strtotime($weekStartDate))); 
$weekSatDate = date('l, F d, Y', strtotime('+5 days', strtotime($weekStartDate))); 
$weekSunDate = date('l, F d, Y', strtotime('+6 days', strtotime($weekStartDate))); 
/* select only the games for the current week from database */ 
require_once('filename.php'); 
$query = "SELECT gamedate, gametime, homeschool, visitorschool, homelivestatsurl, notes, gamestatus, homescore, visitorscore, score, record, sporttype FROM my_table WHERE WEEKOFYEAR(gamedate)=WEEKOFYEAR(NOW()) ORDER BY gamedate"; 
/* if there are results build the accordion and tables - if not, just say there are no games this week */ 
if ($result = mysqli_query($link, $query)) { ?> 
<div style="vertical-align:top;"><img src="images/cobrasschedule.png" border="0"/></div> 
<div style="width:300px; float: left; margin-left:15px; display:table;"> 
<?php 
    /* variables that will change*/ 
    $weekday = ' '; 
    $day = ' '; 
    $sport = ' '; 
    /* fetch associative array */ 
    while ($row = mysqli_fetch_assoc($result)) { 
    if (date('d', strtotime($row['gamedate'])) !== $weekday) { 
    $weekday=date('d', strtotime($row['gamedate'])); 
    if (date('l, F d, Y', strtotime($row['gamedate'])) !== $day) { 
    $day=date('l, F d, Y', strtotime($row['gamedate'])); 
    if ($row['sporttype'] !== $sport) { 
    $sport=$row['sporttype']; 
?> 
<!--This builds the accordion --> 
<div style="margin-top:13px;"> 
<div id="AC-label"> 
    <div id="ACIntro"><span id="hidden_box_button1" style="color:#000000; cursor:pointer;padding-top:10px; padding-bottom:10px;font-size:14px;font-weight:bold; text-shadow:none;"><?php echo date('l, F d, Y', strtotime($weekStartDate));?><span style="float:right; margin-right: 10px;"><span id="hidden_box_button1" style="color:#000000; cursor:pointer; text-shadow:none;">+</span></span></div></div></div> 
    <div id="hidden_box1" style="display:none;"> 
<div style="float:left; clear:both; display:table; margin-top:5px;"> 
<!-- See if the gamedate matches the accordion date --> 
<?php if (date('d', strtotime($row['gamedate'])) == date('d', strtotime($weekStartDate))) { ?> 
<table style="margin-left:-15px;" cellpadding="3px"> 
<tbody> 
<tr> 
<!--Check the value of $sport and echo the appropriate table row --> 
    <?php if ($sport == 'Baseball'): ?> 
    <td colspan="2" style="background-color:#D3BC9A; color:white;text-transform:uppercase; padding-left:25px;"><strong>BASEBALL</strong></td> 
    <?php elseif ($sport == 'MBasketball'): ?> 
     <td colspan="2" style="background-color:#D3BC9A; color:white;text-transform:uppercase; padding-left:25px;"><strong>MEN'S BASKETBALL</strong></td> 
    <?php elseif ($sport == 'Golf'): ?> 

     <td colspan="2" style="background-color:#D3BC9A; color:white;text-transform:uppercase; padding-left:25px;"><strong>GOLF</strong></td> 
    <?php elseif ($sport == 'MSoccer'): ?> 
     <td colspan="2" style="background-color:#D3BC9A; color:white;text-transform:uppercase; padding-left:25px;"><strong>MEN'S SOCCER</strong></td> 
    <?php elseif ($sport == 'WBasketball'): ?> 
     <td colspan="2" style="background-color:#D3BC9A; color:white;text-transform:uppercase; padding-left:25px;"><strong>WOMEN'S BASKETBALL</strong></td> 
    <?php elseif ($sport == 'WSoccer'): ?> 
     <td colspan="2" style="background-color:#D3BC9A; color:white;text-transform:uppercase; padding-left:25px;"><strong>WOMEN'S SOCCER</strong></td> 
    <?php elseif ($sport == 'Softball'): ?> 
     <td colspan="2" style="background-color:#D3BC9A; color:white;text-transform:uppercase; padding-left:25px;"><strong>SOFTBALL</strong></td> 
    <?php elseif ($sport == 'Volleyball'): ?> 
     <td colspan="2" style="background-color:#D3BC9A; color:white;text-transform:uppercase; padding-left:25px;"><strong>VOLLEYBALL</strong></td> 
    <?php elseif ($sport == 'Cycling'): ?> 
     <td colspan="2" style="background-color:#D3BC9A; color:white;text-transform:uppercase; padding-left:25px;"><strong>CYCLING</strong></td> 
    <?php elseif ($sport == 'Equine'): ?> 
     <td colspan="2" style="background-color:#D3BC9A; color:white;text-transform:uppercase; padding-left:25px;"><strong>EQUINE</strong></td> 
    <?php endif; ?> 
</tr> 
<!--Build the table rows for the games based on the sport --> 
<?php if ($row['homescore'] != ' ' and $row['homeschool'] == 'Virginia Intermont College - VA'): ?> 
<tr> 
<td style="margin-bottom:3px;"><span style="text-shadow: none;"><strong><?php echo $row['homeschool'];?>&nbsp;vs.</strong><br/><?php echo $row['visitorschool'];?></span></td> 
<td style="margin-bottom:3px;"><span style="text-shadow: none;"><?php echo $row['homescore'];?><br/><?php echo $row['visitorscore'];?></span></td> 
</tr> 
<?php elseif ($row['homescore'] != ' ' and $row['visitorschool'] == 'Virginia Intermont College - VA'): ?> 
<tr> 
<td style="margin-bottom:3px;"><span style="text-shadow: none;"><?php echo $row['visitorschool'];?>&nbsp;@<br/><?php echo $row['homeschool'];?></span></td> 
<td style="margin-bottom:3px;"><span style="text-shadow: none;"><?php echo $row['visitorscore'];?><br/><?php echo $row['homescore'];?></span></td> 
</tr> 
<?php elseif (empty($row['homescore']) and empty($row['score']) and $row['homeschool'] == 'Virginia Intermont College - VA'): ?> 
<tr> 
<td style="margin-bottom:3px;"><span style="text-shadow: none;"><strong><?php echo $row['homeschool'];?>&nbsp;vs.</strong><br/><?php echo $row['visitorschool'];?></span></td> 
<td style="margin-bottom:3px;"><span style="text-shadow: none;"><?php echo $row['gametime'];?><br/>&nbsp;</span></td> 
</tr> 
<?php elseif (empty($row['homescore']) and empty($row['score']) and $row['visitorschool'] == 'Virginia Intermont College - VA'): ?> 
<tr> 
<td style="margin-bottom:3px;"><span style="text-shadow: none;"><?php echo $row['visitorschool'];?>&nbsp;@<br/><?php echo $row['homeschool'];?></span></td> 
<td style="margin-bottom:3px;"><span style="text-shadow: none;"><?php echo $row['gametime'];?><br/>&nbsp;</span></td> 
</tr> 
<?php elseif ($row['score'] != ' ' and $row['homeschool'] == 'Virginia Intermont College - VA'): ?> 
<tr> 
<td style="margin-bottom:3px;"><span style="text-shadow: none;"><strong><?php echo $row['homeschool'];?>&nbsp;vs.</strong><br/><?php echo $row['visitorschool'];?></span></td> 
<td style="margin-bottom:3px;"><span style="text-shadow: none;"><?php echo $row['score'];?><br/>&nbsp;</span></td> 
</tr> 
<?php elseif ($row['score'] != ' ' and $row['visitorschool'] == 'Virginia Intermont College - VA'): ?> 
<tr> 
<td style="margin-bottom:3px;"><span style="text-shadow: none;"><?php echo $row['visitorschool'];?>&nbsp;@<br/><?php echo $row['homeschool'];?></span></td> 
<td style="margin-bottom:3px;"><span style="text-shadow: none;"><?php echo $row['score'];?><br/>&nbsp;</span></td> 
</tr> 
<?php endif; ?> 
</tr> 
</tbody> 
</table> 
</div> 
<?php } else { ?> 
<div style="float:left; clear:both; display:table; margin-top:5px;"> 
<table style="margin-left:-15px;" cellpadding="3px"> 
<tbody> 
<tr> 
<td style="margin-bottom:3px;"><span style="text-shadow: none;">No games today.</span></td> 
</tr> 
</tbody> 
</table> 
</div> 
<?php } 
    } 
    } 
} 
    /* close while and reset weekday date and sport */ 
    $weekday=date('d', strtotime($row['gamedate'])); 
    $day=date('l, F d, Y', strtotime($row['gamedate'])); 
    $sport=$row['sporttype']; 
    } 
?>  
</div> 
     <script type="text/javascript"> 
     (function($) { 
      $("#hidden_box_button1").click(function() { 
       if ($("#hidden_box1").is(":hidden")) { 
        $("#hidden_box1").slideDown("normal"); 
       } else { 
        $("#hidden_box1").slideUp("normal"); 
       } 
      }); 
     })(jQuery); 
     </script> 
</div> 
<?php 
/* close ifresult and give no results statement*/ 
} else {?> 
<span style="vertical-align:top;margin-left:-16px;padding:0;"><img src="images/cobrasschedule.png" border="0"/></span> 
<?php 
echo "Schedule is not available."; 
} 
/* free result set*/ 
mysqli_free_result($result); 

/* close connection */ 
mysqli_close($link); 
?> 
+0

你'if'声明有矫枉过正。您应该使用数组作为字典来回显您的数据。 – vikingmaster

回答

2

我会回答这个问题在标题

$sql=mysql_query(SELECT field1 from table1 where condition1) // condition1 =field1=value1 for example 

然后

while($r=mysql_fetch_assoc($sql)){ 
    $var=$r['field1']; 
    if($var){ 
    //Something 
} 
    } 
+0

所以,我可以为gamedate和sporttype这么写,然后写下这样的内容: – user2833089

+0

对不起 - 意思是问: 所以,我可以为gamedate和sporttype这么写,然后写一个像这样的东西: 'code' while($ r = mysql_fetch_assoc($ sql)){ $ gameday = date('d'(strtotime($ r ['gamedate'])); $ sportt = $ r ['sporttype' ]; if($ gameday == date('d',strtotime($ weekStartDate))){ //创建表并检查sporttype if($ sport == $ sportt){ //获得该运动并创建运动行 '代码' 会这样吗? – user2833089

+0

是的,但我有点怀疑l关于你在日期间进行的比较.. –

相关问题