2017-03-08 44 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