News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

The Wal-Mart Guide to an Index Action Array

Started by Wal-Mart Security, May 15, 2007, 03:10:54 PM

Previous topic - Next topic

Wal-Mart Security

What is an index action array?

An index action array is the common term for the script that utilizes index.php?action=whatever in the url.  SMF and YaBB are two good examples, may other softwares also use this.

Alright, so you know what it is, how do you do it?

To explain how to use this script, I'll be using a Hello World format.  You'll need to create a few files in your test folder:  settings.php(optional), index.php, content.template.php, and index.template.php.

Inside settings.php, put the following as an example:

<?php
    ini_set
('display_errors'true);
    
error_reporting(E_ALL E_NOTICE);
?>


This code makes any errors that PHP comes across print across the screen.  This is only recommended to use for development purposes, after we are finished with this tutorial we'll be removing those two lines.

In index.php, put the following code in:

<?php
  
    
//load ssi for the forum stuff and the site settings
    //only include this if you are actually using settings.php ;)
    //include_once('settings.php');
    
    //'action=' array variables  'action name' => array('File function is in..', 'Function name')
    
$actionArray = array(
        
'hello' => array('content''hello'),
        
'hello2' => array('content''hello2'),
        
'hello3' => array('content''hello3'),
        );

    if (!isset(
$_REQUEST['action']) || !isset($actionArray[$_REQUEST['action']]))
    {
        
$file 'index';
        
$template 'template_hello';
    }
    else
    {
        
$file $actionArray[$_REQUEST['action']][0];
        
$template 'template_'.$actionArray[$_REQUEST['action']][1];
    }

include_once(
'index.template.php');
include_once(
$file.'.template.php');
template_smain_above();
$template();
template_smain_below();
?>



Make a file called index.template.php with this in it:

<?php
function template_smain_above()
{
echo 
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>'
;
}

function 
template_smain_below()
{
echo 
'
</body>

</html>'
;
}
?>


Next, let's make the file called content.template.php:

<?php
function template_hello()
{
echo 
'Hello world!';
}

function 
template_hello2()
{
echo 
'<span style="color: red;">Hello world!</span>';
}

function 
template_hello3()
{
echo 
'<span style="color: blue;">Hello world!</span>';
}
?>


Now that we have all the files created, let's move on to explaining what each file is doing.
Achilleus Technologies
Ace Gaming Syndicate
PART TIME SMF CHARTER MEMBER 4LIFE

SMF slays phpbb, puts it in an IPB coffin, and buries them both with the ashes of vBulletin.

Wal-Mart Security

#1
Settings.php

This file sets all the error messages to display, except for notices.  Notices are okay, but they can be confusing sometimes as you might see a notice even though your script works just fine.  For the purposes of this tutorial I have them turned off.

index.php

In index.php, we have the main powerhouse for the entire test site.  I'll go over each line individually:

    //load ssi for the forum stuff and the site settings
    require("forums/SSI.php");
    include_once('settings.php');


This does just what the comment says it does:  It loads settings.php, which turns on error reporting.  It also includes SSI.php so you can use it on any part of the content or headers/footers.

    //'action=' array variables  'action name' => array('File function is in..', 'Function name')
    $actionArray = array(
        'hello' => array('content', 'hello'),
        'hello2' => array('content', 'hello2'),
        'hello3' => array('content', 'hello3'),
        );


This is the actual index action array.  And it's just that--an array.  What you are doing here is telling index.php--which is the only file that the browser ever goes to--where to find all your content templates.

    if (!isset($_REQUEST['action']) || !isset($actionArray[$_REQUEST['action']]))
    {
        $file = 'index';
        $template = 'template_hello';
    }
    else
    {
        $file = $actionArray[$_REQUEST['action']][0];
        $template = 'template_'.$actionArray[$_REQUEST['action']][1];
    }


This right here tells index.php what to do if there is not any action set, and if the action is set, what to do with that information.  It uses the array that I explained above to determine what to do if the action is set.

include_once('index.template.php');
include_once($file.'.template.php');
template_smain_above();
$template();
template_smain_below();


This is the last part, the part that actually executes whatever functions it determines it should do and prints the stuff to the browser.  First, it includes index.template.php which contains the headers and footers.  Then it executes the header function(in index.template.php), the content function(which in this tutorial is always in content.template.php), and lastly the footer function(in index.template.php).

index.template.php

<?php
function template_smain_above()
{
echo 
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>'
;
}

function 
template_smain_below()
{
echo 
'
</body>

</html>'
;
}
?>


This file is relatively simple.  It contains the header and footer of your site.  This way, you only have to update this one file if a link changes on a side menu.  If you currently have an index.html already, all you have to do is simply copy/paste that file in to these two functions.  From the very beginning of the html file, copy all the way to the beginning of your actual content, and put that in to template_smain_above().  From the end of your content, copy from there all the way to the end of the page and put that in to template_smain_below().


content.template.php

<?php
function template_hello()
{
echo 
'Hello world!';
}

function 
template_hello2()
{
echo 
'<span style="color: red;">Hello world!</span>';
}

function 
template_hello3()
{
echo 
'<span style="color: blue;">Hello world!</span>';
}
?>


This entire file contains anything you might have for content.  If you notice in index.php, the file name was always 'content'.  This is that file.  The function to execute in index.php MUST have a matching function in this file or you will get an error.
Achilleus Technologies
Ace Gaming Syndicate
PART TIME SMF CHARTER MEMBER 4LIFE

SMF slays phpbb, puts it in an IPB coffin, and buries them both with the ashes of vBulletin.

Wal-Mart Security

#2
So, after we set up these files, you should be able to go to yoursite.com/whateverdir/index.php and it will show "Hello World!" in black letters.  Try putting index.php?action=hello2 after that, it should appear in yellow.  Try again with hello3, it should appear red.

How does this work?  I'll walk you through it ;)

I'll be listing two examples:

Lets say you go to index.php.  Index(I'm referring to the files as people) doesn't see any action set, so he sets the variables for the default file/function listed in this code, which just happens to be content.template.php and template_hello()

    if (!isset($_REQUEST['action']) || !isset($actionArray[$_REQUEST['action']]))
    {
        $file = 'index';
        $template = 'template_hello';
    }


After he decides what he is going to do, he puts in to his memory index.template.php and prints out the header, the default function, and then the footer.

If index.php has something after it, such as ?action=hello2, it would do essentially the same as above except it would execute template_hello2() instead.
Achilleus Technologies
Ace Gaming Syndicate
PART TIME SMF CHARTER MEMBER 4LIFE

SMF slays phpbb, puts it in an IPB coffin, and buries them both with the ashes of vBulletin.

Wal-Mart Security

#3
Sub Categories

Sub categories can be done in a multitude of ways.  We will be discussing two different methods for two different things you might need to use.  We will need to go much further in to the $_REQUEST variable and the two different ways to use it.

An important thing to note:  Sub actions are not always the best thing to do!  Here on this very SMF forum, you don't see index.php?board=##;topic=##.  This is completely unnecessary.  It's just as easy to list index.php?board=## and index.php?board=## as separate actions.  I would not recommend using subactions unless it is a very small script, small enough to fit in to one function and not be messy/bloated.

What is this $_REQUEST variable, and how do I use it?

Basically, what I didn't explain above is what the $_REQUEST variable actually is.  Look at the URL in your address bar right now.  You see where it says index.php?topic=171384.0?  $_REQUEST['topic'] is part of the $_REQUEST variable array, which actually contains 171384.0.  This tells SMF to pull all the messages that have 171384 as the topic.

So when you go to www.yoursite.com/index.php?action=hello2 that would store "hello2" within $_REQUEST['action'].  Note that if it is directly after index.php it MUST use a question mark, and if it is a chain of variables such as ?action=profile;u=1 then you space them with a semicolon or an ampersand( & ).

I don't want to get TOO technical in this guide, but I think we got the basics of $_REQUEST covered now.  Moving on to sub actions.

Method 1

Method 1 is by far the easiest to do, that's why I'm listing it as method 1 :P

Let's use my recoded flash gallery as an example.  This is the second way I coded it, the first way I used Method 2 so it took up a LOT more lines of code.  Method 1 has a total of 90 lines, Method 2 used 300 lines.  You can already begin to see the difference I'm sure haha.  The second edition(Method 1 being used) of my flash gallery can be viewed here, and the first edition(Method 2 being used) can be viewed here.

If you click around a little bit and maybe watch one of those movies, look at how the URL is structured.  On the Method 1 site, it shows index.php?action=flash&movie=##.  Here is a sample of code I used for this:


--in progress--
Achilleus Technologies
Ace Gaming Syndicate
PART TIME SMF CHARTER MEMBER 4LIFE

SMF slays phpbb, puts it in an IPB coffin, and buries them both with the ashes of vBulletin.

Wal-Mart Security

Achilleus Technologies
Ace Gaming Syndicate
PART TIME SMF CHARTER MEMBER 4LIFE

SMF slays phpbb, puts it in an IPB coffin, and buries them both with the ashes of vBulletin.

Wal-Mart Security

Okay, I think it's about finished.  I wrote the guide pretty quick, so it might be pretty confusing and there might also be typos.  Let me know if you see any or if you try this guide and it doesn't work :)  Later I'll be adding how to do subcategories, I gotta figure out the best way to explain that first.
Achilleus Technologies
Ace Gaming Syndicate
PART TIME SMF CHARTER MEMBER 4LIFE

SMF slays phpbb, puts it in an IPB coffin, and buries them both with the ashes of vBulletin.

Tony Reid

I was just trying to explain this to someone - your guide makes it easy - thank you :)

Tony Reid

Wal-Mart Security

Ah, completely forgot about this, I was going to make the guide on subactions(they aren't too different from regular actions).

Thanks for the comment :)
Achilleus Technologies
Ace Gaming Syndicate
PART TIME SMF CHARTER MEMBER 4LIFE

SMF slays phpbb, puts it in an IPB coffin, and buries them both with the ashes of vBulletin.


Wal-Mart Security

Thanks :)

I'm still working on the subcategories part, it's a little complicated and I'm having some trouble figuring out how to explain it.  Did it help you any?
Achilleus Technologies
Ace Gaming Syndicate
PART TIME SMF CHARTER MEMBER 4LIFE

SMF slays phpbb, puts it in an IPB coffin, and buries them both with the ashes of vBulletin.

babjusi

Quote from: Achilleus on August 30, 2007, 10:58:50 PM
Thanks :)

I'm still working on the subcategories part, it's a little complicated and I'm having some trouble figuring out how to explain it.  Did it help you any?

This thing ain''t new to me, but I am sure that plenty folks here would find that very useful. It is very thorough and detailed. Well done Achilleus

Wal-Mart Security

Thank you :)  I really appreciate the feedback :D
Achilleus Technologies
Ace Gaming Syndicate
PART TIME SMF CHARTER MEMBER 4LIFE

SMF slays phpbb, puts it in an IPB coffin, and buries them both with the ashes of vBulletin.

Advertisement: