Fork me on GitHub

New/Custom Mod Not Running When Initialising  Bottom

  • OK, I'm sure this has been covered somewhere but I just CAN NOT find it and I'm at my wit's end.

    I'm creating a module for a gaming organization that I run and I need to be able to add/manage/display different online games. I thought this would be a simple enough concept for my first module.

    I followed the MDG and I'm in section 7.4 or 7.5 depending on how you look at it.

    I created my inistialisation files (pninit.php) the way I should and the query I have added will create the table if I run it in a SQL tool. So I know the query works.

    HOWEVER - function my_module_init() doesn't seem to ever get run...

    The module is added to the module list successfully and I can even go as far as to activate it and have it show up in the administration section, but the tables are never created and nothing I put inside that function ever gets run (I tried printing some output, etc).

    I'm sure I'm missing something obvious, but for the life of me I don't know what it is.

    mySQL, Apache, PHP, PN - all the latest/greatest versions directly from the distributors.

    Any help will be GREATLY appreciated.

    Thank in advance!
  • If you post the contents of pninit.php i'll take a look.

    -Mark
  • Here's the init function that doesn't seem to be working, or for that matter getting called.

    Thanks a ton for your quick response...

    Code

    /**
     * initialise the lag_games module
     * This function is only ever called once during the lifetime of a particular
     * module instance
     */

    function lag_games_init()
    {
        // Get datbase setup - note that both pnDBGetConn() and pnDBGetTables()
        // return arrays but we handle them differently.  For pnDBGetConn()
        // we currently just want the first item, which is the official
        // database handle.  For pnDBGetTables() we want to keep the entire
        // tables array together for easy reference later on
        list($dbconn) = pnDBGetConn();
        $pntable = pnDBGetTables();

        // It's good practice to name the table and column definitions you
        // are getting - $table and $column don't cut it in more complex
        // modules
        $lag_games_table = $pntable['lag_games'];

        // Create the table - the formatting here is not mandatory, but it does
        // make the SQL statement relatively easy to read.  Also, separating out
        // the SQL statement from the Execute() command allows for simpler
        // debug operation if it is ever needed
        $sql = "CREATE TABLE $lag_games_table {
                lag_gameid int(10) NOT NULL auto_increment,
                lag_gamename varchar(100) NOT NULL default '',
                lag_gamedescription text NOT NULL default '',
                lag_gameimage varchar(100) NOT NULL default '',
                lag_gamelink varchar(100) NOT NULL default '',
                PRIMARY KEY(lag_gameid))"
    ;
        $dbconn->Execute($sql);
    )

        // Check for an error with the database code, and if so set an
        // appropriate error message and return
        if ($dbconn->ErrorNo() != 0) {
            pnSessionSetVar('errormsg', _CREATETABLEFAILED);
            return false;
        }
       
        // insert default game
        $gameid = $dbconn->GenID($lag_games_table);
        $sql = "INSERT INTO $lag_games_table (lag_gameid, lag_gamename, lag_gamedescription, lag_gameimage, lag_gamelink)
                VALUES($gameid,'Default','Default Game','image.gif','http://www.google.com')"
    ;
        $dbconn->Execute($sql);
        if ($dbconn->ErrorNo() != 0) {
            pnSessionSetVar('errormsg', $dbconn->ErrorMsg());
            return false;
        }

        // Set up an initial value for a module variable.  Note that all module
        // variables should be initialised with some value in this way rather
        // than just left blank, this helps the user-side code and means that
        // there doesn't need to be a check to see if the variable is set in
        // the rest of the code as it always will be
        // pnModSetVar('LaG_Games', 'bold', 0);
        // pnModSetVar('LaG_Games', 'itemsperpage', 10);

        // Initialisation successful
        return true;
    }
  • I've given the code a quick look over can have spotted one potential problem

    Code

    ....
    $sql = "CREATE TABLE $lag_games_table {
    .....


    should read

    Code

    ...
    $sql = "CREATE TABLE $lag_games_table (
    ....


    Note round bracket rather than a brace.

    If this doesn't take care of the problem then if you zip the existing code up and let me know where I can download it i'll go through the code on one of my test installs.

    -Mark
  • Again, thanks for your response but that didn't seem to take care of the issue. That syntax error was actually left over from me trying to cause SQL errors to be generated so I could prove to myself that the code was being run. No such luck, I still got "Module initialised" as my message.

    You can take a look at the entire module (note that I've only gotten as far as section 7.4-7.5 in the guide so I've only done the initialisation steps in the code you'll see) here: http://files.lividandgifted.com/users/S3VYN/LaG_Games.zip
  • Donnie,

    I've isolated the problem for you. The problem is a missing comma in the pntables array definition in pntables.php.

    I've corrected this and updated the SQL create to use the pntables column as per the MDG.

    New file can be found at http://www.markwest.…oads/LaG_Games.zip/. Let me know when you've grabbed the updated copy so I can remove it.

    Good luck with the module.

    -Mark
  • Awesome, thanks so much Mark. I got the file and it seems to fix everything as advertised.

    With this kind of service this may be a community I can sink my teeth into, a credit to open source development. :)
  • S3VYN

    Awesome, thanks so much Mark. I got the file and it seems to fix everything as advertised.

    With this kind of service this may be a community I can sink my teeth into, a credit to open source development. :)


    I'm always willing to try and help a developer get started. I was in the same position myself a while ago beginning my first module using the then new MDG and API standards.

    -Mark
  • 0 users

This list is based on users active over the last 60 minutes.