来自$ _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
我有一个从$ _GET获取值的php页面,根据此值选择多维数组中的不同值 p>
$ data =
array(
“index”=&gt; array(
“name”=&gt;“title”,
“title”=&gt; “&lt; img src ='logo.png'alt =''&gt;”,
“fallback_html”=&gt;“main.php”,
“gallery”=&gt;数组(“1”=&gt;“ 没有“)),
”gallery1“=&gt;数组(
”name“=&gt;”gallery1“,
”title“=&gt;”图库1“,
”fallback_html“=&gt;”“ ,
“gallery”=&gt;数组(“1”=&gt;“jpg1.jpg”,“2”=&gt;“jpg2.jpg”)
)
);
code> pre>
这里是代码 p>
if(isset($ _ GET ['p'])){
$ page = $ _GET [' p'];
}
else {
$ page =“index”;
echo $ data [$ page] ['title'];
code> pre>
我得到的输出 p>
注意:未定义的索引:第66行的D:\ xampp \ htdocs \ egliphp \ index.php中的'index'
code> pre>
如果 我将$ page值更改为'index'或'gallery1'它可以正常工作 p>
div>
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.
check what var_dump gives You
var_dump(isset($_GET['p']));
but i recomend you to create allowed values array
$allowed_vals = array_keys($data);
and then use in_array() function
if(in_array($_GET['p'], $allowed_vals )){
...
else
....
Is the $data array defined in the same document as the "$page" handling code? (not completely clear from your question). Just to be sure, check if the $data array exists and contains the requested 'index' key, by debugging it using this:
// Just to check if the $data array actually exists and contains the 'index' key:
print_r($data);exit();
if(isset($_GET['p']) && array_key_exists(trim($_GET['p']), $data)){
$page = trim($_GET['p']);
}
else {
$page = "index";
}
[update] according to your last line, the $data array should be present, so debugging the $_GET array may be a good option to check if it contains a 'p' entry and a proper value;
print_r($_GET);
try this
$page = "$_GET['p']";