Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9234535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:48:47+00:00 2026-06-18T06:48:47+00:00

I have a php page that gets a value from $_GET and according to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:48:49+00:00Added an answer on June 18, 2026 at 6:48 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page in php that gets from the url such as this
I have created a page called profile.php that gets a value from mainpage.php, by
So I have a php page that gets data from database and displays a
So I have an array that gets created from a php loop that looks
I have very simple form that gets values in JSON format from searchAlbum.php .
I have a PHP page that does a couple of different things depending on
I have a simple php page that displays data from a mysql database. I
I have a working script that passes a value from a popup page to
I have a page called connect.php This page connects with twitter and gets the
I have a PHP page that loads external content using other PHP files. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.