Jamroom Logo Jamroom 5 Core
is now Open Source!
User Support Forum Archive (Read Only)
Jamroom Developers:
Using $jamroom_db in include dir of my module
dizpers



Joined: 03 Oct 2011
Posts: 15

Posted: 10/11/11 10:03 
Hi! I create new module. I have a main file (in jamroom root dir: /myModule.php ). I use this file as router. The code is like this:


Code

<?php

require('include/jamroom-include.inc.php');
require("modules/myModule/include/myModule.model.php");

$GLOBALS['JR_SCRIPT_NAME'] = 'myModule.php';


$mMyModule = new MyModule_Model();

$_POST = getPostVars();
$_user = sessionVerify();
$language = getLanguage($_user['user_language']);

// make routing
switch ($_POST['action']) {
    case 'show':

        if (checkType($_POST['band_id'], 'number_nz')){
            $mMyModule::showBand($_POST['band_id']);
        }elseif($_POST['band_id'] == 'all'){
            $mMyModule::showAll();
        }else{
            //error
        }

        exit;
        break;

    default:
        echo 'Not enough actual parameters!';

        exit;
        break;
}

?>


And in myModule.model.php I have the code like this:



Code
<?php

defined('IN_JAMROOM') or exit();


class myModule_Model {

    public function __construct(){
       
    }

    public function showAll(){
        $req = "SELECT album_name
                FROM {$jamroom_db['jrSongInfo']}";
        $res = dbQuery($req, 'album_name');
        htmlShowTemplate('myModule', 'myModule.tpl', $res);
    }

    public function showBand($band_id){
        $req = "SELECT album_name
                FROM {$jamroom_db['jrSongInfo']}
                WHERE band_id = $band_id";
        $res = dbQuery($req, 'album_name');
        htmlShowTemplate('myModule', 'myModule.tpl', $res);
    }
}

?>


For debug this code, I use print_r($req) in methods. And I see that


Code
{$jamroom_db['jrSongInfo']}


don't insert the table name... but in /modules/myModule/include.php I have:


Code
$jamroom_db['jrSongInfo'] = $config['db_prefix'] .'song_info';


Why I can't use {$jamroom_db['jrSongInfo']} to retrieve the name of the table?

Back to top
Michael
Jamroom Team


Joined: 22 Apr 2008
Posts: 3423
Location: Tokyo

Posted: 10/11/11 20:18 
do you use an IDE? I recommend PhpStorm, its awesome.


Code
    public function showBand($band_id){
        $req = "SELECT album_name
                FROM {$jamroom_db['jrSongInfo']}
                WHERE band_id = $band_id";
        $res = dbQuery($req, 'album_name');
        htmlShowTemplate('myModule', 'myModule.tpl', $res);
    }


If you had that file open in PhpStorm the error checking would put a red line under $jamroom_db and say "this variable is undefined."

you need to add global to your function and call the var you want to use in.

Code
    public function showBand($band_id){
global $jamroom_db;
        $req = "SELECT album_name
                FROM {$jamroom_db['jrSongInfo']}
                WHERE band_id = $band_id";
        $res = dbQuery($req, 'album_name');
        htmlShowTemplate('myModule', 'myModule.tpl', $res);
    }



_________________
Michael Ussher
Jamroom Network Team Member: http://www.jamroom.net
Priority Support: http://www.jamroom.net/Support_Center
Back to top
dizpers



Joined: 03 Oct 2011
Posts: 15

Posted: 10/11/11 23:37 
Thx, I will try this now:)

PS
Hm, I use phpStorm (and yes - its really awesome!) and it's no red underlining $jamroom_db ...

Back to top
Michael
Jamroom Team


Joined: 22 Apr 2008
Posts: 3423
Location: Tokyo

Posted: 10/11/11 23:43 
This is what it looks like for me:



_________________
Michael Ussher
Jamroom Network Team Member: http://www.jamroom.net
Priority Support: http://www.jamroom.net/Support_Center
Back to top
dizpers



Joined: 03 Oct 2011
Posts: 15

Posted: 10/13/11 10:56 
Hm... I added


Code
global $jamroom_db;


and it's dont solve the problem

any suggestions?

upd:

I do like this:



Code
<?php

defined('IN_JAMROOM') or exit();

class AlbumsList_Model {

    private $jamroom_db;

    public function __construct(){
        $this->jamroom_db = $GLOBALS['GLOBALS']['jamroom_db'];
    }

    public function showAll(){
        print_r($jamroom_db);
        $req = "SELECT song_album
                FROM {$jamroom_db['jrSongInfo']}";
        $temp = dbQuery($req, 'album_name');
        foreach ($temp as $key => $value){
            $res['ALBUMS'][$key] = $value['song_album'];
        }
        htmlShowTemplate('jrAlbumsList', 'jrAlbumsList.tpl', $res);
    }

    public function showBand($band_id){
        print_r($this->jamroom_db);
        $req = "SELECT song_album
                FROM {$jamroom_db['jrSongInfo']}
                WHERE band_id = $band_id";
        print_r($req);
        $temp = dbQuery($req, 'NUMERIC');
        foreach ($temp as $key => $value){
            $res['ALBUMS'][$key] = $value['song_album'];
        }
        htmlShowTemplate('jrAlbumsList', 'jrAlbumsList.tpl', $res);
    }
}

?>
 


And print_r() don't print me my defined (in include.php of my module) table names. My include.php:


Code
<?php


defined('IN_JAMROOM') or exit();


$jrAlbumsList = "Albums List";

// Our version
//default stuff goes here
//...


// Define custom database tables
// HERE I define table names
$jamroom_db['jrAlbumsList'] = $config['db_prefix'] .'jrAlbumsList';
$jamroom_db['jrSongInfo'] = $config['db_prefix'] .'song_info';
?>


Back to top
Michael
Jamroom Team


Joined: 22 Apr 2008
Posts: 3423
Location: Tokyo

Posted: 10/13/11 17:03 
Try this:


Code
<?php

defined('IN_JAMROOM') or exit();

class AlbumsList_Model {

    public function showAll(){
        global $jamroom_db;
        print_r($jamroom_db);
        $req = "SELECT song_album
                FROM {$jamroom_db['jrSongInfo']}";
        $temp = dbQuery($req, 'album_name');
        foreach ($temp as $key => $value){
            $res['ALBUMS'][$key] = $value['song_album'];
        }
        htmlShowTemplate('jrAlbumsList', 'jrAlbumsList.tpl', $res);
    }

    public function showBand($band_id){
        global $jamroom_db;
        print_r($jamroom_db);
        $req = "SELECT song_album
                FROM {$jamroom_db['jrSongInfo']}
                WHERE band_id = $band_id";
        print_r($req);
        $temp = dbQuery($req, 'NUMERIC');
        foreach ($temp as $key => $value){
            $res['ALBUMS'][$key] = $value['song_album'];
        }
        htmlShowTemplate('jrAlbumsList', 'jrAlbumsList.tpl', $res);
    }
}



_________________
Michael Ussher
Jamroom Network Team Member: http://www.jamroom.net
Priority Support: http://www.jamroom.net/Support_Center
Back to top
dizpers



Joined: 03 Oct 2011
Posts: 15

Posted: 10/14/11 07:23 
Doesn't work.

PS
In $GLOBALS array I can't see my vars (assigned in include.php of my module) - I think - it's my main problem here.

Back to top
Michael
Jamroom Team


Joined: 22 Apr 2008
Posts: 3423
Location: Tokyo

Posted: 10/14/11 19:32 
why are you looking in $GLOBALS? what do you expect to find there?


Code
$somevalue = "hello world";
first();
second();

function first(){
    print "the first value is: ". $somevalue;
}

function second(){
    global $somevalue;
    print "<br>the second value is: ". $somevalue;
}


will print:

Code
the first value is:
the second value is: Hello World


Because $somevalue was defined outside of the functions it is inaccessible INSIDE the functions. so we need to put that global line in the function in order to make it accessible from within the function.

do this:
print_r(get_defined_vars());

and see if any variables you want are anywhere in there, then you can get the name of the var you want.


_________________
Michael Ussher
Jamroom Network Team Member: http://www.jamroom.net
Priority Support: http://www.jamroom.net/Support_Center
Back to top
dizpers



Joined: 03 Oct 2011
Posts: 15

Posted: 10/16/11 04:29 
Nevertheless defining $jamroom_db as global variable works fine. You are right, thank you!Smile But one problem does not disappear: table names which I define in include.php of my module are not in $jamroom_db now.


Code
<?php


defined('IN_JAMROOM') or exit();


$jrAlbumsList = "Albums List";

// Our version
//default stuff goes here
//...


// Define custom database tables
// HERE I define table names
$jamroom_db['jrAlbumsList'] = $config['db_prefix'] .'jrAlbumsList';
$jamroom_db['jrSongInfo'] = $config['db_prefix'] .'song_info';
?>


So, I don't see 'jrSongInfo' and 'jrAlbumsList' in global $jamroom_db.

PS
I think, that in include.php of my module I should define $jamroom_db as global variable too.

Back to top
Michael
Jamroom Team


Joined: 22 Apr 2008
Posts: 3423
Location: Tokyo

Posted: 10/17/11 19:04 
your includes info looks correct to me.

If they are not in $jamroom_db the places i would check are:
* is the include.php in the correct location for your module
* is the module turned on in the admin panel
* are there any errors in the admin panel in the php error log
* are there any errors in the admin panel in the activity log.


_________________
Michael Ussher
Jamroom Network Team Member: http://www.jamroom.net
Priority Support: http://www.jamroom.net/Support_Center
Back to top
jamesd116



Joined: 05 Jun 2006
Posts: 1559
Location: Rochester Pa

Posted: 10/17/11 19:53 
it may be in your schema if something in there is incorrect so if you check your php log file it should tell u where a mistake may be made if thats where the problem exists at.


_________________
One day the court system will learn that a childs mother is not the only option...... Question is will it be too late by that time...
Back to top
Display posts from previous:   
User Support Forum Archive (Read Only)
Jamroom Developers

 
Solutions
• Social Media Platform
• Social Networking Software
• Musician Website Manager
• Community Builder
Products
• Jamroom Core
• Jamroom Addons
• Jamroom Modules
• Jamroom Marketplace
Support
• Support Forum
• Documentation
• Support Center
• Contact Support
Community
• Community Forum
• Member Sites
• Developers
Company
• About Us
• Contact Us
• Privacy Policy
©2003 - 2010 Talldude Networks, LLC.