-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

まず、のドキュメントをお読みください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

このように条件を書くことができます

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 に答える