W H Help
W H Help

Reputation: 105

PHP value comming from $_GET does not work on echoing an array

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

Upvotes: 1

Views: 202

Answers (4)

Giacomo1968
Giacomo1968

Reputation: 26066

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.

Upvotes: 4

M K Garwa
M K Garwa

Reputation: 495

try this

  $page = "$_GET['p']";

Upvotes: 0

Norton
Norton

Reputation: 340

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
....

Upvotes: 1

thaJeztah
thaJeztah

Reputation: 29017

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);

Upvotes: 0

Related Questions