Using index.php

Now that we understand xoops_version, lets install our module and start work

With the module installed lets click on "tutorial" on main menu. You will see your xoops site with an empty page. Now open index.php.

<?php
// Tutorial
// Created by KaotiK
require('../../mainfile.php');
require(XOOPS_ROOT_PATH.'/header.php');

require(XOOPS_ROOT_PATH.'/footer.php');
?>

You will notice 3 new lines here. Your code should be placed inbetween the lines "...header" and "....footer". So, as a test lets write this:

<?php
// Tutorial
// Created by KaotiK
require('../../mainfile.php');
require(XOOPS_ROOT_PATH.'/header.php');

echo "Hello world!";

require(XOOPS_ROOT_PATH.'/footer.php');
?>

Now if you click "tutorial" on main menu, the words "Hello world!" will appear on your site.

Creating a List

Now lets take this a step further a create a simple list of your users.
Lets replace:

echo "Hello world!";

with:

$member_handler =& xoops_gethandler('member');
$foundusers =& $member_handler->getUsers();
foreach (array_keys($foundusers) as $j) {
echo $foundusers[$j]->getVar("uname").'<br>';
}

Click on tutorial on your main menu and you will now see a list of your current users. Now lets review what each line does. The first line calls a xoops handler which deals with member and assigns it to $member_handler. The second line grabs all users and places them in $foundusers as an array. Now we can loop through all the results using foreach. What foreach is doing is basicly saying "I'm going to take the whole stack and 'do something' to each 'line' ". If you wanted to see everything inside the array try this:
Warning: Only try the following code if you are using a test site with a small list of users.

$member_handler =& xoops_gethandler('member');
$foundusers =& $member_handler->getUsers();
foreach (array_keys($foundusers) as $j) {
//echo $foundusers[$j]->getVar("uname").'<br>';
print_r($foundusers[$j]);
echo '<br><br><br>';
}

You will get a big list that will look confusing but shows all variables concerning each of your users.

Moving on. Lets properly format the list and get some more info on each of your users.

First let's create a header bar for our table. As I said before, this is a simple tutorial, so I will show you the easy way, which isn't necessarily the correct way of doing it. I will however use more elaborate and professional methods in upcoming tutorials.
To avoid confusion I'm going to show the whole index.php file:

<?php
// Tutorial
// Created by KaotiK
require('../../mainfile.php');
require(XOOPS_ROOT_PATH.'/header.php');
?>
<table width="100" border="0">
<tr>
<td bgcolor="#99CC99">Name</td>
<td bgcolor="#66CC99">Email</td>
</tr>
</table>
<?php
require(XOOPS_ROOT_PATH.'/footer.php');
?>

Now click on tutorial from your main menu. You will now see a table heading saying Name and Email. Great! Lets place some info into it.

<?php
// Tutorial
// Created by KaotiK
require('../../mainfile.php');
require(XOOPS_ROOT_PATH.'/header.php');
?>
<table width="100" border="0">
<tr>
<td bgcolor="#99CC99">Name</td>
<td bgcolor="#66CC99">Email</td>
</tr>
<?php
$member_handler =& xoops_gethandler('member');
$foundusers =& $member_handler->getUsers();
foreach (array_keys($foundusers) as $j) {
echo '<tr><td>'.$foundusers[$j]->getVar("uname").'</td><td>'.$foundusers[$j]->getVar("email").'</td></tr>';
}
?>
</table>
<?php
require(XOOPS_ROOT_PATH.'/footer.php');
?>

Click on tutorial from main link and you will now see our table populated with user's name and email. Nice! However the code is very sloppy, going back and forth between php and html. Now let's do the whole thing in php:

<?php
// Tutorial
// Created by KaotiK
require('../../mainfile.php');
require(XOOPS_ROOT_PATH.'/header.php');

echo '<table width="100" border="0">
<tr>
<td bgcolor="#99CC99">Name</td>
<td bgcolor="#66CC99">Email</td>
</tr>';
$member_handler =& xoops_gethandler('member');
$foundusers =& $member_handler->getUsers();
foreach (array_keys($foundusers) as $j) {
echo '<tr><td>'.$foundusers[$j]->getVar("uname").'</td><td>'.$foundusers[$j]->getVar("email").'</td></tr>';
}
echo '</table>';
require(XOOPS_ROOT_PATH.'/footer.php');
?>

Ahhh. Much better. Same end result but now the code is more legible. Let's bring some more xoops standards into it. First thing to do is replace the text "name" and "email" with some language constants. Lets create a folder called language (important: folder name should be all in lower case). Now, inside that folder lets create another folder called english. Inside the folder english create a file called main.php. Now place this code inside main.php:

<?php
define('TT_NAME','Name');
define('TT_EMAIL','Email');
?>

Create another file in this same folder called modinfo.php Inside this file place:

<?php

?>

What we are doing here is creating 2 language constants to replace our current text. Language constants allow users of your module to easily replace module text with their own native language. Now open index.php and change the following:

<?php
// Tutorial
// Created by KaotiK
require('../../mainfile.php');
require(XOOPS_ROOT_PATH.'/header.php');

echo '<table width="100" border="0">
<tr>
<td bgcolor="#99CC99">'.TT_NAME.'</td>
<td bgcolor="#66CC99">'.TT_EMAIL.'</td>
</tr>';
$member_handler =& xoops_gethandler('member');
$foundusers =& $member_handler->getUsers();
foreach (array_keys($foundusers) as $j) {
echo '<tr><td>'.$foundusers[$j]->getVar("uname").'</td><td>'.$foundusers[$j]->getVar("email").'</td></tr>';
}
echo '</table>';
require(XOOPS_ROOT_PATH.'/footer.php');
?>

Excelent! We now have language constants in our module.

Previous
Next