2017-03-08 45 views
-3

我的指令已被註釋掉,我試圖按照概述進行編碼。學習PHP數組和開關

我覺得我把變量傳遞給intval錯誤,錯誤地實現了switch方法,並且檢查是否不是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

您不檢查錯誤。 ''switch(purpose){'是一個錯誤,'case a:'是一個錯誤..'$ _GET ['purpose'];'什麼也不做,'intval($ i);'什麼都不做,'$ _GET [我'];'什麼都不做。您需要分配,並啓用錯誤報告和/或檢查您的日誌。 – chris85

回答

0

讓我們先從簡單的東西:

聲明變量i這樣的:

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

聲明變量purpose這樣的:

$purpose=$_GET["purpose"]; 

這是假設你將有一個類似於?i=1&purpose=a的查詢字符串在末尾o f你網頁的網址

現在聲明的變量可以使用它們的$ -prefixed名稱來訪問。 情況字符串必須是單引號或者雙引號

switch($purpose){ 
    case "a": 

如果NUMBERSLETTERS是數組,則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

通常,在每個case語句之後,您使用break;,以便switch不會繼續查找其他匹配項。如果你想尋找更多的比賽,那沒關係,但我認爲你不會這樣做。

僅供參考,php中的變量不一定是全部大寫。

我會在那裏停下來。 請考慮我的所有建議。 閱讀儘可能多的在線PHP手冊。 總是檢查你的錯誤日誌。 然後隔離你的問題。 然後再次閱讀手冊。 然後嘗試一下自己。 然後檢查你的錯誤日誌。 ...你明白了我的觀點。

+0

@Saterial我不知道我可以解決這個問題,因爲它有太多問題,但是如果您發現我的答案有用,請立即投票。 – mickmackusa