-1
<?php
$files = glob( 'docs/*.xml' );
if ( isset( $_GET['doctype'] ) == "all" ) {
    foreach ( $files as $file ) {
        $xml = new SimpleXMLElement( $file, 0, true );
        echo'
                <tr>
                <td id="'. $xml->doctype .'" name="'. $xml->doctype .'" class="mainTable">' . $xml->doctype . '</td>
                <td><a href="viewdoc.php?docname=' . basename( $file, '.xml' ) . '&username='. $xml->startedby .'&myname='. $_SESSION['username'] .'">' . basename( $file, '.xml' ) . '</a></td>
                <td><a href="viewprofile.php?name='. $xml->startedby .'">'. $xml->startedby .'</a></td>
                <td>'. $xml->date .'</td>
                <td>* * * * *</td>
                <td>'. filesize( $file ) .'kb</td>
                </tr>
                ';
    }
}
else if ( isset( $_GET['doctype'] ) == "doc" ) {
        foreach ( $files as $file ) {
            $xml = new SimpleXMLElement( $file, 0, true );
            // code filter the $xml->doctype by equal it to currect value - which i'm not sure how to do.
            echo'
                     <tr>
                     <td id="'. $xml->doctype .'" name="'. $xml->doctype .'" class="mainTable">' . $xml->doctype . '</td>
                     <td><a href="viewdoc.php?docname=' . basename( $file, '.xml' ) . '&username='. $xml->startedby .'&myname='. $_SESSION['username'] .'">' . basename( $file, '.xml' ) . '</a></td>
                     <td><a href="viewprofile.php?name='. $xml->startedby .'">'. $xml->startedby .'</a></td>
                     <td>'. $xml->date .'</td>
                     <td>* * * * *</td>
                     <td>'. filesize( $file ) .'kb</td>
                    </tr>
               ';
        }
    }
?>

私はいくつかの<a>タグ (同じリンク、home.php) を取得し、それぞれに異なる $_GET リンク ( home.php?doctype=doc、home.php?doctype=all など) を取得しました。ここで、if ステートメントを使用して各 doctype をフィルタリング$_GET['doctype']し、値が自分の値と等しいかどうかを確認します (値がワード、エクセル、パワーポイントなどであると仮定します)。

私の質問は: $xml->doctype が自分の値の 1 つに等しいと仮定して、doctype をフィルタリングするにはどうすればよいですか?

4

2 に答える 2

3

これにはビットマスクを使用します: http://php.net/manual/en/language.operators.bitwise.php

<?php

// define your types and groups:
$listedTypes["TYPE_MSWORD"]             = 1;
$listedTypes["TYPE_MSEXCEL"]            = 2;
$listedTypes["TYPE_MSPOWERPOINT"]       = 4;
$listedTypes["TYPE_OFFICE"]             = $listedTypes["TYPE_MSWORD"] + $listedTypes["TYPE_MSEXCEL"] + $listedTypes["TYPE_MSPOWERPOINT"];

$listedTypes["TYPE_HTML"]               = 8;
$listedTypes["TYPE_SVG"]                = 16;
$listedTypes["TYPE_W3C"]                = $listedTypes["TYPE_HTML"] + $listedTypes["TYPE_SVG"];

$listedTypes["TYPE_ALL"]                = $listedTypes["TYPE_OFFICE"] + $listedTypes["TYPE_W3C"];


// try to open the page.php?doctype=TYPE_MSWORD
// or page.php?doctype=TYPE_ALL
if(!isset($_GET['doctype']))                        $_GET['doctype'] = "TYPE_ALL";
if(!isset($listedTypes[$_GET['doctype']]))          $_GET['doctype'] = "TYPE_ALL";
$requestedType = $listedTypes[$_GET['doctype']];



foreach ( $files as $file )
{
    $xml = new SimpleXMLElement( $file, 0, true );

    if($xml->doctype == "......")               $fileType = $listedTypes["TYPE_MSWORD"];
    elseif($xml->doctype == ".........")        $fileType = $listedTypes["TYPE_MSEXCEL"];
    elseif($xml->doctype == ".........")        $fileType = $listedTypes["TYPE_MSPOWERPOINT"];
    //... continue

    // then check if the $type matches the $selection of $_GET['doctype']
    // if the filetype is in the requested file type group, it will be shown
    if($fileType & $requestedType)
    {
        echo'
        <tr>
        <td id="'. $xml->doctype .'" name="'. $xml->doctype .'" class="mainTable">' . $xml->doctype . '</td>
        <td><a href="viewdoc.php?docname=' . basename( $file, '.xml' ) . '&username='. $xml->startedby .'&myname='. $_SESSION['username'] .'">' . basename( $file, '.xml' ) . '</a></td>
        <td><a href="viewprofile.php?name='. $xml->startedby .'">'. $xml->startedby .'</a></td>
        <td>'. $xml->date .'</td>
        <td>* * * * *</td>
        <td>'. filesize( $file ) .'kb</td>
        </tr>
        ';
    }
}
于 2013-07-06T03:33:20.030 に答える
2

あなたの問題はIFにあります:

if ( isset( $_GET['doctype'] ) == "all" )

このステートメントはisset()'sboolean resulttrueまたはfalsewithを評価string "all"しますが、おそらくそれを意味するものではありませんでした

これを修正し、冗長性を取り除くには、次のコードを使用します。

$acceptedDocTypes = ['all', 'doc', 'excel'];

if ((isset($_GET['doctype'])) && (in_array($_GET['doctype'], $acceptedDocTypes))) {
    foreach ( $files as $file ) {
        $xml = new SimpleXMLElement( $file, 0, true );
        echo'
                <tr>
                <td id="'. $xml->doctype .'" name="'. $xml->doctype .'" class="mainTable">' . $xml->doctype . '</td>
                <td><a href="viewdoc.php?docname=' . basename( $file, '.xml' ) . '&username='. $xml->startedby .'&myname='. $_SESSION['username'] .'">' . basename( $file, '.xml' ) . '</a></td>
                <td><a href="viewprofile.php?name='. $xml->startedby .'">'. $xml->startedby .'</a></td>
                <td>'. $xml->date .'</td>
                <td>* * * * *</td>
                <td>'. filesize( $file ) .'kb</td>
                </tr>
                ';
    }
}

'doctype'が設定されていて、かつ、許可されているタイプである限り、このコードはそれによってフィルタリングされます。

于 2013-07-06T03:36:20.657 に答える