duandazhen7306 2013-01-26 18:53
浏览 88
已采纳

来自$ _GET的PHP值在回显数组时不起作用

I have a php page that gets a value from $_GET and according to this value selects different values in a multidimensional array

$data =
array(
     "index" => array(
         "name" => "title",
         "title" => "<img src = 'logo.png' alt=''>",
         "fallback_html" => "main.php",
         "gallery" => array("1" => "nothing")),
     "gallery1" => array(
         "name" => "gallery1",
         "title" => "Gallery 1",
         "fallback_html" => "",
         "gallery" => array("1" => "jpg1.jpg","2" => "jpg2.jpg")
     )
);

here is the code

if(isset($_GET['p'])){
  $page = $_GET['p']; 
} 
else { 
  $page = "index"; 
} 

echo $data[$page]['title'];

and i am getting a output like that

Notice: Undefined index: 'index' in D:\xampp\htdocs\egliphp\index.php on line 66

if i change the $page value to 'index' or 'gallery1' it works just fine

  • 写回答

4条回答 默认 最新

  • duanci8209 2013-01-26 19:00
    关注

    The logic seems sound, so it could be a whitespace & non-alphanumeric character issue. So I recommend doing this:

    $raw_page = preg_replace('/[^a-zA-Z0-9]+/', '', trim($_GET['p']));
    if(!empty($raw_page) && array_key_exists($raw_page, $data)){
      $page = $raw_page; 
    } 
    else { 
      $page = "index"; 
    }
    

    I also added an array_key_exists so the logic can handle a request that is being made to a non-existant page or array index.

    EDIT: Also, your array data is inconsistent. Dong an echo $data['index']['title']; will result in:

    <img src = 'logo.png' alt=''>
    

    But doing an echo $data['gallery1']['title']; will result in:

    Gallery 1
    

    So I think you need to iron that out as well.

    EDIT FOR ADDITIONAL INFO ON THE MIXED ARRAY VALUE STUFF: Okay, from a programming standpoint there is still an issue with the way the array has mixed value types for the value $data['title']. I see that as a problem waiting to happen. So I would recommend reworking the $data array structure as so:

    $data =
    array(
         "index" => array(
             "name" => "title",
             "title" => array("type" => "image", "value" => "<img src = 'logo.png' alt=''>"),
             "fallback_html" => "main.php",
             "gallery" => array("1" => "nothing")),
         "gallery1" => array(
             "name" => "gallery1",
             "title" => array("type" => "text", "value" => "Gallery 1"),
             "fallback_html" => "",
             "gallery" => array("1" => "jpg1.jpg","2" => "jpg2.jpg")
         )
    );
    

    Specifically I did this to the $data["index"]["title"] that has an image tag:

    "title" => array("type" => "image", "value" => "<img src = 'logo.png' alt=''>"),
    

    And this to the $data["gallery1"]["title"] that has a text string:

    "title" => array("type" => "text", "value" => "Gallery 1"),
    

    That will help you on the rendering side of things better handle each case. So you could have code as follows:

    if ($data["gallery1"]["title"]["type"] == "image") {
      echo $data["gallery1"]["title"]["value"] . "<br clear="all" />";
    }
    else if ($data["gallery1"]["title"]["type"] == "text") {
      echo "<h1>" . $data["gallery1"]["title"]["value"] . "</h1>";
    }
    

    The example above shows that if you tag the type at the datasource, you can do different things to each type when it comes time to render. If you feel it’s too much work, that’s fair. But mixing datatypes in values is never a good idea without someway to clearly differentiate them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。