-1

因此,我正在尝试创建一个在特定日期读取特定 XML 文件的页面。我有这个问题,所有的帮助将不胜感激。

这是我的 XML 文件的结构:

<?xml version="1.0" encoding="iso-8859-1"?>
<excercises>
  <excercise>
        <name>Test</name>
    <sr></sr>
    <weight>25kg</weight>
    <comment>Testing</comment>
  </excercise>
    <excercise>
    <name>Test II</name>
    <sr></sr>
    <weight>250kg</weight>
    <comment>Testing more</comment>
  </excercise>
</excercises>

这是我的 PHP 代码:

<?php $month = mktime("m"); ?>
<?php $weekday = mktime("weekday"); ?>

<?php 
$doc = new DOMDocument(); 

if ($month == "01","03","05","07","09","11") && ($weekday == "Monday") {
    $doc->loadXML( 'resourceMon.xml' ); }
if ($month == "01","03","05","07","09","11") && ($weekday == "Tuesday") {
    $doc->load( 'resourceTue.xml' ); }
if ($month == "01","03","05","07","09","11") && ($weekday == "Thursday") {
    $doc->load( 'resourceThu.xml' ); }
if ($month == "01","03","05","07","09","11") && ($weekday == "Friday") {
    $doc->load( 'resourceFri.xml' ); }

if ($month == "02","04","06","08","10","12") && ($weekday == "Monday") {
    $doc->load( 'resourceMon.xml' ); }
if ($month == "02","04","06","08","10","12") && ($weekday == "Tuesday") {
    $doc->load( 'resourceTue.xml' ); }
if ($month == "02","04","06","08","10","12") && ($weekday == "Thursday") {
    $doc->load( 'resourceThu.xml' ); }
if ($month == "02","04","06","08","10","12") && ($weekday == "Friday") {
    $doc->load( 'resourceFri.xml' ); }

else { echo "This day is not a right day to workout!"; }


$xpath = new DOMXPath($dom);  


$root=$doc->documentElement;

$excercises = $dom->getElementsByTagName( "excercise" ); 
foreach( $excercises as $excercise ) { 
  $names = $excercise->getElementsByTagName( "name" ); 
  $name = $names->item(0)->nodeValue; 

  $sr = $excercise->getElementsByTagName( "sr" ); 
  $sr = $sr->item(0)->nodeValue; 

  $weights= $excercise->getElementsByTagName( "weight" ); 
  $weight= $weights->item(0)->nodeValue; 

  $comments = $excercise->getElementsByTagName( "comment" ); 
  $comment = $comments->item(0)->nodeValue; 

  echo "<tr><td><b>$name</b></td><td>$sr</td><td>$weight</td><td><i>$comment</i></td></tr>"; 

  } 
?>

4

4 回答 4

0

你可以这样写条件

if (($month == "01" || $month =="03" || $month =="05" || $month =="07" || $month =="09" || $month =="11")) && ($weekday == "Monday") {
    $doc->loadXML( 'resourceMon.xml' ); }
于 2012-09-10T06:19:14.210 回答
0

mktime返回一个 unix 时间戳

您需要使用date()

date('l') // returns Monday, Tuesday, Wednesday etc.
date('m') // returns month in two digits, 01, 02, 03 etc.
于 2012-09-10T06:19:30.680 回答
0

首先,阅读mktime. 它根本不像您尝试使用它的方式那样工作。

其次,$foo == "01", "02", ...是胡说八道。in_array如果您需要这样的比较,请使用。

第三,你把事情复杂化了。执行以下操作:

$file = 'resource' . date('D') . '.xml';

$doc->loadXML($file);

或者

switch (date('N')) {
    case 1 :
        $file = 'resourceMon.xml';
        break;
    case 2 :
        ...
    default :
        return false;
}

$doc->loadXML($file);
于 2012-09-10T06:22:44.703 回答
0

1) 使用 date() 代替 mktime。
2) 使用 load() 而不是 loadXML。
3) 创建一个数组并使用 in_array() 在数组中查找特定月份

<?php $month = date("m"); 
$weekday = date("D"); 

$doc = new DOMDocument(); 

$monthHaystack = array("01","03","05","07","09","11");
if (in_array($month,$monthHaystack) && ($weekday == "Mon")) {
    $doc->load( 'resourceMon.xml' ); }

else { echo "This day is not a right day to workout!"; }

?>

然后其余的保持不变,也不需要 xpath。

于 2012-09-10T06:30:26.473 回答