Page 1 of 1

Need help with mutile arrays and if statements

Posted: Wed Aug 04, 2010 6:14 pm
by Lostnode
Ok, not sure if the topic is as descriptive as it should be, let alone how to describe my situation.

Lets take a look at my code snipet first.

Code: Select all

$pages = array('employees' => '/admin/content/employees.php',
                'emp-add' => '/admin/content/emp-add.php',
                'emp-added' => '/admin/content/emp-added.php',
		'emp-update' => '/admin/content/emp-update.php',
		'emp-updated' => '/admin/content/emp-updated.php',
		'emp-delete' => '/admin/content/emp-delete.php',
		'emp-deleted' => '/admin/content/emp-deleted.php',
		'emp-view' => '/admin/content/emp-view.php',
		'punch-add' => '/admin/content/punch-add.php',
		'punch-added' => '/admin/content/punch-added.php',
		'punch-update' => '/admin/content/punch-update.php',
		'punch-updated' => '/admin/content/punch-updated.php',
		'punch-delete' => '/admin/content/punch-delete.php',
		'punch-deleted' => '/admin/content/punch-deleted.php',
		'user-add' => '/admin/content/user-add.php',
		'user-added' => '/admin/content/user-added.php',
		'login' => '/admin/content/login.php',
		'logout' => '/admin/content/logout.php',
		'login-failed' => '/admin/content/login-failed.php',
		'user-add' => '/admin/content/register-form.php',
		'user-added' => '/admin/content/register-success.php',
				);
Its a basic array that mixed witht he rest of my codding allows me to include the specified file into my index page using index.php?nav=(aray variable)
For instance index.php?nav=user-add would bring up the /admin/content/register-form.php inside my formated index page. Fairly simple code to edit and modify. However here comes my issue. I want to use if statements. My project is to create a modular based website, and instead of adding all th possible page links, I want to include files if the exist. The above segment is for employee and time punches, the basis of my program. I want to add a module for payroll, so I want to figure out how to create an include that has just the payroll array info, something like below

Code: Select all

'payroll-admin' => '/admin/content/payroll-admin.php',
		'payroll-payout' => '/admin/content/payroll-payout.php',
		'payroll-search' => '/admin/content/payroll-search.php',
		'payroll-view' => '/admin/content/payroll-view.php',
I would want to include these lines only if the payroll module exists.

I was looking at the array_merge() function, but again using it with if statements looks impossible.

Does anyone know of a way that I can append my pages array? SO that if it exists, I can inject these lines into the pages array.

I hope I explained it properly. Let me know if you don't understand and I will try and explain it a little better understand.

Re: Need help with mutile arrays and if statements

Posted: Wed Aug 04, 2010 6:24 pm
by AbraCadaver
I think I understand, but maybe not. Here's an idea (pretty simple):

Code: Select all

if(file_exists('payroll.php')) { // not sure how you want to do this
   $payroll_pages = array(
                'payroll-admin' => '/admin/content/payroll-admin.php',
                'payroll-payout' => '/admin/content/payroll-payout.php',
                'payroll-search' => '/admin/content/payroll-search.php',
                'payroll-view' => '/admin/content/payroll-view.php'
   );

   $pages = array_merge($pages, $payroll_pages);
}

Re: Need help with mutile arrays and if statements

Posted: Wed Aug 04, 2010 6:59 pm
by Lostnode
That would work, if I only had the one module, I was looking into this, however on a larger scale, say I had 10 possible modules, how would I write it assuming that not all 10 may exist? it would be so much easier if there was a array_inject() function that would allow me to inject them all in (if they existed) directly into the pages array.

Could I do the following?

Code: Select all

if(file_exists('payroll.php')) { // not sure how you want to do this
   $payroll_pages = array(
                'payroll-admin' => '/admin/content/payroll-admin.php',
                'payroll-payout' => '/admin/content/payroll-payout.php',
                'payroll-search' => '/admin/content/payroll-search.php',
                'payroll-view' => '/admin/content/payroll-view.php'
   );

   $pages = array_merge($pages, $payroll_pages);

if(file_exists('module2.php')) { // not sure how you want to do this
   $module2_pages = array(
                'mod2-admin' => '/admin/content/mod2-admin.php',
                'mod2-payout' => '/admin/content/mod2-payout.php',
                'mod2-search' => '/admin/content/mod2-search.php',
                'mod2-view' => '/admin/content/mod2-view.php'
   );

   $pages = array_merge($pages, $module2_pages);
In essence keep adding to pages by merging them in order as such merging the new pages with what ever comes after? Is that even possible?

Here is how I would like it layed out

Index.php (after the original pages array)

Code: Select all

if(file_exists('../admin/modules/payroll/array.php')) {
include("../admin/modules/payroll/array.php");
}
if(file_exists('../admin/modules/module2/array.php')) {
include("../admin/modules/module2/array.php");
}
The file ./admin/modules/payroll/array.php would contain the following

Code: Select all

$payroll_pages = array(
                'payroll-admin' => '/admin/content/payroll-admin.php',
                'payroll-payout' => '/admin/content/payroll-payout.php',
                'payroll-search' => '/admin/content/payroll-search.php',
                'payroll-view' => '/admin/content/payroll-view.php'
   );

   $pages = array_merge($pages, $payroll_pages);
The file ../admin/modules/module2/array.php would contain

Code: Select all

 $module2_pages = array(
                'mod2-admin' => '/admin/content/mod2-admin.php',
                'mod2-payout' => '/admin/content/mod2-payout.php',
                'mod2-search' => '/admin/content/mod2-search.php',
                'mod2-view' => '/admin/content/mod2-view.php'
   );

   $pages = array_merge($pages, $module2_pages);
would this work? IF they both existed would each instance just add tot he pages array?

Re: Need help with mutile arrays and if statements

Posted: Wed Aug 04, 2010 8:12 pm
by superdezign
Well, couldn't you just handle these things in different files?

MAIN
The main file determines which file to go to by generating it dynamically with the nav variable in the GET request query string. After generating the file path, it checks file_exists(), and then includes the file if it exists. Otherwise, 404.

PAYROLL-ADMIN
A file like this would be included by the MAIN file. Each file only belongs to one module, so you could define the navigation array of each module somewhere and load it into the appropriate files. Then, the MAIN file could merge this with the main navigation array.

PAYROLL
Depending on how you define your main module files, these could hold the navigation instead of a single navigation file. Then, each file in the module would include their parent module file, and then the MAIN file would use the data from it.


But, I could be reading you wrong.

Re: Need help with mutile arrays and if statements

Posted: Thu Aug 05, 2010 10:19 am
by AbraCadaver
The way you have it laid out now, just glob() the /admin/modules/ dir for subdirs and if it exists, include the array.php, then array_merge().

Re: Need help with mutile arrays and if statements

Posted: Sat Aug 07, 2010 3:07 pm
by Lostnode
Sound's like a plan, never used glob(), however theoretically this should work right?

Code: Select all

<?php
foreach (glob("../admin/modules/*") as $incdir) {
if (file_exists('../admin/modules/$incdir/array.php")) {
    include('../admin/modules/$incdir/array.php');
}
}
?>

Re: Need help with mutile arrays and if statements

Posted: Sat Aug 07, 2010 4:27 pm
by AbraCadaver
Theoretically. But you should use the GLOB_ONLYDIR flag and use double quotes around the file_exists and include the way you have it. Also, echo out the $incdir because it may already contain admin/modules.