Check if a key exists in an array that contains sub-arrays using the function array_key_exists

I am encountering an issue with a function.

My goal is to include all pages, but the problem is that not all pages are named like the parameter $_GET['page']. For example, if I call index.php?p=accueil, it should redirect to php/home.php.

Another example is if I call index.php?p=message, it should transparently redirect to message.php.

To handle exceptions, I have created an array as follows:

<?php    
$paramListepages = array(       
     'corbeille' => array(
         'libelle'     => 'corbeille',
         'page'        => 'php/trash.php'
     ),   
     'nouveaumessage' => array(
         'libelle'     => 'nouveaumessage',
         'page'        => 'php/envoyer.php'
     )  
);
?>

This array contains many sub-arrays, such as the first two shown above.

To call pages, I have created a function like this:

function getPage($var) {
    if (!isset($var)){
        // No specific page specified => default page
        include('php/accueil.php');
    }
    elseif (array_key_exists($var,$paramListepages)){
        // Page found => include it!
        include ('php/accueil.php');
    }
    else{
        // Page not found in the array => include directly
        include('php/'.$var.'.php');    
    }
}

Currently, it seems to recognize the if condition and the else statement.

However, when I request a page in the array, I get a blank page. It appears to be unable to read the expected correct value.

I am seeing white empty pages with no errors or messages displayed.

In my Ubuntu environment, I have enabled error_reporting(E_ALL);

Any assistance on this matter would be greatly appreciated.

Answer №1

Is this the correct approach?

if (array_key_exists($variable,$parameterList)){
    require_once ($parameterList[$variable]['page']);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Inquiry regarding the encryption methods I am using

After implementing a login system, I took steps to enhance its security. First, I generated a salt for the passwords: $salt = openssl_random_pseudo_bytes(1024); file_put_contents("salt.txt", $salt); Then, I hashed the passwords using the whirlpool algori ...

What is the process for displaying a PHP array in HTML5 audio and video players?

I am currently working with two PHP arrays. The first array, array "a," contains strings that represent paths for MP3 files on the server. The second array, array "b," contains strings representing paths for MP4 files. For example: $test = 'a.mp3&ap ...

"Receiving an 'undefined index' error when attempting to post in Ajax with

Need help with sending data from client to server using AJAX in PHP. I am facing an issue when trying the following code: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script type="text/javascrip ...

The Magento MySQL database is experiencing an issue where there are too many keys specified, with a maximum allowance

Encountering an error message while working on a Magento PHP file and executing a CREATE TABLE query: SQLSTATE[42000]: Syntax error or access violation: 1069 Too many keys specified; max 64 keys allowed The section of code causing the issue is as follows ...

Having trouble redirecting PHP errors to a separate log file instead of the Apache log file

Trying to configure a Ubuntu web server [LAMP] to write PHP errors into a php_error file instead of the Apache log, but encountering difficulties. Here's the troubleshooting steps taken: Edited the php.ini file: error_reporting = E_ALL | E_STRI ...

Unable to retrieve PHP object using AJAX

I am having trouble fetching the PHP file from Ajax. In the PHP file, an object is created and then converted into JSON using the json_encode() function. However, when I try to request that PHP file from Ajax, nothing is displayed as output ('Smith&ap ...

Upon inspecting the Logcat, a Parse error was discovered: a syntax error, specifically an unexpected ':' in the file path C:wampwwwsignup.php on line 2. This file pertains to a login

Looking for guidance in the world of PHP, I am currently tackling a basic login and registration form for testing purposes. However, as I encounter some errors, I humbly seek assistance from all of you...thank you. Below is a snippet of my code.. any help ...

Tips for ensuring the protection of scorelist in a flash game

For my current project, I am tasked with monitoring the highscores of users who play a game. The challenge lies in ensuring that this data is stored securely in a database to prevent any unauthorized access or modification by users trying to manipulate the ...

Show individual row information within a bootstrap modal upon clicking the edit button

As a beginner in programming, I am attempting to use bootstrap modal to showcase row data from a mysql table within the modal window. Following the advice found on stackoverflow regarding "Pull information from mysql table to bootstrap modal to edit" didn ...

Finding an encrypted value using Laravel Eloquent ORM: A step-by-step guide

In my Laravel project, I am working with encrypted records using the php-encryption library before storing them in a Mysql database. Currently, when searching for a record based on an encrypted value, I loop through all the records to find a match, but thi ...

Filtering date ranges in two columns of Datatables

I am working on a table that contains two date columns. Currently, I have implemented a date range filter for one column (aData[3]) using the following code: $.fn.dataTableExt.afnFiltering.push( function(oSettings, aData, iDataIndex) { var dat ...

Storing data in a many-to-many relationship using a ListBoxField

My application features a custom DataObject named Service, which has a many_many relationship with RelatedServices set up as follows: class Service extends DataObject { private static $db = array ( 'Name' => 'Varchar', ...

Simulating object instantiation with PhpSpec in Laravel

In my codebase, I have a Campaign class that handles the booking of a single campaign in an external API. Additionally, there is an EntryBooking class responsible for preparing the entry and utilizing the Campaign class for booking. In cases where multiple ...

Leveraging PHP, AJAX, and MySQL within the CodeIgniter framework

I am currently venturing into the world of web development using the codeigniter PHP framework. My current hurdle involves a select dropdown on a page, populated with unique IDs such as 1, 2, 3 from the database. What I aim to achieve is, when a value is s ...

What could be the reason I received a warning in my PHP code?

Seeking assistance with a PHP script I have created. It is throwing an error and I am unable to pinpoint the mistake. Here are the warning messages I am encountering: Warning: mysql_query(): Access denied for user ''@'localhost' (usin ...

Check whether the username variable is blank; if it is, then refrain from navigating to page2.php

Introducing meekochat, a live chat website where users can connect with others. To get started, simply input your name on page1.php and you'll be directed to page2.php for chatting. However, I have implemented a feature in my code to ensure that if th ...

Numerous Options for Woocommerce Product Templates

I am working on a WordPress website that incorporates WooCommerce for its e-commerce features. Within the website, there are three distinct categories, each with its own template assigned to the products within those categories. So far, I have successfull ...

Convert the value of a Javascript variable into a PHP variable

I am feeling confused about how to pass JavaScript variables to PHP variables. Currently, I have a PHP session named example_survey and three buttons with jQuery attributes. When a button is clicked, it triggers a jQuery click event and passes the attribut ...

PHP MySQL system for booking events

I am interested in creating a compact event registration tool that allows users to sign up for multiple events at various times. For instance Event1 Time1 09:00 - 10:00 | Max Participants 12 Time2 11:00 - 12:00 | Max Participants 9 Event2 Tim ...

The sum function in MySQL only seems to work accurately for the first row, but can often give

Here is the SQL query I am using: SELECT `cuenta`.`reservaId`, sum(`cuenta`.`tarifaTotal`) FROM `check`,`cuenta`,`comanda`,`reserva` WHERE `comanda`.`id`=`cuenta`.`idComanda` AND `comanda`.`tipo`=0 GROUP BY `cuenta`.`reservaId` ORDER BY `check`.`id` Howe ...