2017-03-08 10 views
-3

私の説明はコメントアウトされており、私は説明したようにコードを作成しようとしています。PHPの配列とスイッチの学習

変数をintvalに渡して間違っていると感じ、スイッチメソッドを間違って実装し、nullでないかどうかチェックします。

<?php 
    // $IMAGE_DIR_PREFIX refers to the provided images subdirectory... 
$IMAGE_DIR_PREFIX = 'images/'; 

    // images/globals.php defines two arrays representative of the images 
    // that can be loaded... 
require_once($IMAGE_DIR_PREFIX.'globals.php'); 

    // By default assume no image was selected so: 
    // a) the image file ($IMAGE) is set to null 
    // b) the image file's MIME type is set to null... 
$IMAGE = null; 
$IMAGE_MIME = null; 

    // pic.php will be accessed using URL parameters, e.g., pic.php?i=2. 
    // Per what was demonstrated in class obtain the GET parameter's 
    // key-value pages using the global array $_GET. The parameters are 
    // as follows: 
    // a) 'i' is an image number (e.g., 0, 1, 2, 3, etc.) 
    // b) 'purpose' is either 'g' or 'a' 
    // 
    // So: 
    // * if purpose=g then look up the image file to use in the $LETTERS 
    //  array where i=<INTEGER> is the index into $LETTTERS 
    // * if purpose=a then look up the image file to use in the $NUMBERS 
    //  array where i=<INTEGER> is the index into $NUMBERS 
    // 
    // Sample (partial) URLs: 
    // * pic.php?i=3&purpose=g 
    // * pic.php?purpose=a&i=2 
    // 
    // Because an invalid index can be passed, before accessing the array 
    // do the following: 
    // 1) Obtain the $_GET for 'i', e.g., $_GET['i']. 
$_GET['i']; 
    // 2) Pass (1) into PHP's intval(). This will ensure it is an int. 
    //  NOTE: http://php.net/manual/en/function.intval.php 
intval ($i); 
    // 3) Obtain the $_GET for 'purpose'. 
$_GET['purpose']; 
    // 4) Use a switch statement or a suitable equivalent on (3). 
    //  4a) If (4) matches 'a' then call array_key_exists() on $NUMBERS 
    //   with the value obtained in (2) as the index. If the key exists 
    //   set $IMAGE to the concatenation of $IMAGE_DIR_PREFIX, '/', and 
    //   the string at index in $NUMBERS. 
    //  4b) If (4) matches 'g' then call array_key_exists() on $LETTERS 
    //   with the value obtained in (2) as the index. If the key exists 
    //   set $IMAGE to the concatenation of $IMAGE_DIR_PREFIX, '/', and 
    //   the string at index in $LETTERS. 
    //  NOTE: In PHP the '.' operator is string concatenation. 
switch (purpose){ 
    case a: 
    if (array_key_exists('i', $NUMBERS)) { 
    $IMAGE .= $IMAGE_DIR_PREFIX . '/'. $NUMBERS; 
    } 
    case g: 
    if (array_key_exists('i', $LETTERS)) { 
    $IMAGE .= $IMAGE_DIR_PREFIX . '/'. $LETTERS; 
    } 
} 
    // 5) Now that a possible $IMAGE has been set (NOTE: it still might be null), 
    //  determine the image's MIME type. Instead of hard-coding it, you will 
    //  use PHP's finfo object to determine the MIME type of the file. 
    //  Internally the finfo object determines the MIME type using magic strings 
    //  (i.e., in the way the 'file' command does this from the command line). 
    //  Determine the MIME type as follows: 
    //  5a) If $IMAGE is NOT null: 
if (!empty($IMAGE)) { 
    $fi = new finfo(FILEINFO_MIME); 
    $result = $fi->file($IMAGE); 
     if (!empty($result)) { 
     $IMAGE_MIME=$result; 
     } 
} 
    //  5b) $fi = new finfo(FILEINFO_MIME); 
    //   $result = $fi->file($IMAGE); // Returns MIME type unless an error occurred 
    //  5c) If $result is not an error, set $IMAGE_MIME to $result. 
    // 6) If $IMAGE and $IMAGE_MIME are both NOT null, then output the image with: 
    //   header('Content-Type: '.$IMAGE_MIME); 
    //   @readfile($IMAGE); 
    //   exit; 
if (!empty($IMAGE) && !empty($IMAGE_MIME)) { 
    header('Content-Type: '.$IMAGE_MIME); 
    @readfile($IMAGE); 
    exit; 
    } 
    // 7) Otherwise, generate an HTTP 404 File Not Found response and page with: 
    //   http_response_code(404); 
    //   echo <<<ZZEOF 
    //   <!DOCTYPE html> 
    //   <html> 
    //   <head><title>2017W 03-60-334 Assignment 2: 404 File Not Found</title></head> 
    //   <body><h1>404 File Not Found</h1><p>The requested resource could not be found.</p></body> 
    //   </html> 
    //   ZZEOF; 
    //   exit(0); 
?> 
+1

あなたの質問は何ですか?あなたは何を達成しようとしていますか?現在エラーが表示されていますか?もしそうなら、エラーは何ですか? –

+0

私はどんなエラーも受けません。しかし、私のコードが動作していない、画像が読み込まれていません。私は自分の配列の部分を想定しています。 – Saterial

+1

エラーをチェックしていません。 $ _GET ['目的']; 'は何もしません、' intval($ i); 'は何もしません、' $ _GET ['私は]; '何もしません。割り当てを行い、エラー報告を有効にしたり、ログをチェックしたりする必要があります。 – chris85

答えて

0

簡単なもので始まるのをしてみましょう:

は、このような変数iを宣言:

$i=intval($_GET["i"]); 

は、このような変数purposeを宣言:

$purpose=$_GET["purpose"]; 

これはあなたが持っているだろうと仮定し最後に?i=1&purpose=aのようなクエリー・ストーンあなたのページのURL

ここで、宣言された変数は、その$接頭辞付きの名前を使ってアクセスできます。 NUMBERSLETTERSが配列されている場合 場合文字列が

switch($purpose){ 
    case "a": 

引用された単一または二重にする必要があり、その後、array_key_existsは使い結構です。 あなたは変数iの値を持つキーを検索する場合、このよう$iを使用:今すぐ

if(array_key_exists($i,$NUMBERS)){ 

、あなたはすでにあなたのコードの先頭に$IMAGE_DIR_PREFIX = 'images/';を宣言しました。

したがって、$IMAGE .= $IMAGE_DIR_PREFIX . '/'は接頭辞の値となり、//となりますが問題になります。

$IMAGE$NUMBERSに連結しようとしますが、NUMBERSが配列の場合、この方法では値にアクセスできません。

$NUMBERS[$i]; 

同じアドバイスが$LETTERSについても同様:むしろ、のようなものを使用。

通常、それぞれのケースステートメントの後にbreak;を使用するので、switchは他のマッチを探し続けません。より多くの試合を見たいなら、それは大丈夫ですが、今回はあなたとは思えません。

ちょうどあなたの情報のために、PHPの変数はすべて大文字である必要はありません。

私はそこで止まります。 私のすべてのアドバイスを考慮してください。 オンラインPHPマニュアルからできるだけお読みください。 エラーログを常に確認してください。 次に、問題を特定します。 マニュアルをもう一度お読みください。 それから自分で試してみてください。 次に、エラーログを確認します。 ...あなたは私の得点を得る。

+0

@資料私は質問が多すぎるので問題を解決できるかどうかはわかりませんが、私の答えが有用であることがわかったら、それをアップしてください。 – mickmackusa

関連する問題