Forums  ›  General  ›  General discussions
 

json data

 

 

$GLOBALS['MySQL']->

 Can you elaborate a bit on this?

Dolphin/Cheetah stores references to commonly used things in a globally accessible array.

The $GLOBALS['MySQL'] entry is assigned in db.inc.php and is a reference to a instance of the ChWsbDb class.

 

$GLOBALS['MySQL']->

 Can you elaborate a bit on this?

 

 

The getAll function i pointed out in my last example would eliminate the need to you to use that loop.

Looks like you need to take a look in the database classes of cheetah. There are a number of built in functions that would save you quite a bit of extra coding.

Don't mean to hijack the thread but this does bring up a question about the database functions.  I will need to convert a module that has php mysql functions coded into the module instead of Cheetah db functions.  Are all the functions I need contained in ChWsbDb.php  or are there other database function classes as well?

Yes. All of them are in ChWsbDb.php.

There are also functions in inc/db.inc.php which just call the ones in ChWsbDb.php. The only time you would use the functions in inc/db.inc.php is in scripts outside the modules own database class which would be rare.

You will also find that some scripts outside of a module such as the ones in the inc/classes folder use those other functions or will call the ones in ChWsbDb.php by prefixing the function name with $GLOBALS['MySQL']-> which is how the functions in inc/db.inc.php call them so the result is the same.

However, when in a module, all database functions should be used in the modules db class and will use only the functions in ChWsbDb.php

 

The getAll function i pointed out in my last example would eliminate the need to you to use that loop.

Looks like you need to take a look in the database classes of cheetah. There are a number of built in functions that would save you quite a bit of extra coding.

Don't mean to hijack the thread but this does bring up a question about the database functions.  I will need to convert a module that has php mysql functions coded into the module instead of Cheetah db functions.  Are all the functions I need contained in ChWsbDb.php  or are there other database function classes as well?

yes, I do realize that there are several functions. since I was using a code sample, I was trying to make sure things worked one step at a time. now that you have helped me pass this hurdle, hopefully I will be able to get everything else I need to figured out.

thanks again.

The getAll function i pointed out in my last example would eliminate the need to you to use that loop.

Looks like you need to take a look in the database classes of cheetah. There are a number of built in functions that would save you quite a bit of extra coding.

originally i was just using a php page inside the module (modules/ccf/apps/top_menu.php) just to make sure that the query and results was working properly. after that was working, then I was trying to add it into the module as my originally question asked. I did get it figured out with:

 

      header("Content-type: application/json; charset=utf-8");

  

        $result = db_res('SELECT *  FROM `sys_menu_top` WHERE `Parent` = 0 AND `Visible` LIKE \'%non%\' AND `Type` IN (\'top\', \'custom\') ORDER BY `Parent` ASC');

        $dbdata = array();

 

        while ( $row = $result->fetch())  {

          $dbdata[]=$row;

        }

 

    echo json_encode($dbdata);

 

I was trying to use global values but got a bit confused. probably because I need to get some sleep. thanks

It looks like your writing this as if it was outside of a cheetah module.

So i have to ask. Is this a actual module your working with? The reason i ask is because if it is, then there is no need to open a database connection as you are because cheetah does it for you. Also no need to use database prepare and fetch functions either because cheetah has built in functions for dealing with the database. Also no need to include header.inc.php because a actual cheetah module already does.

So i have to question your module because it does not seem to me that you are using a actual module.



Try this.

function actionJson () {

     $query = 'SELECT * FROM `sys_menu_top`';

     $rows = $GLOBALS['MySQL']->getAll($query);

     echo json_encode($rows);

}

And technically the database call should be in the modules database class which would eliminate the need to use $GLOBALS['MySQL'] and the above would simply call a function from the database class reducing the function above to one line of code. But one step at a time.

Right now i am trying to determine if you have written an actual module, or are just putting a php page into the root of your site. The type and format of the code sample you have provided has me quite confused.

ok, so this is what I have and it does work, but I don't want to hard code the database info. I want to use the /inc/header.inc.php page, but can't seem to get it to work. I have tried several different ways to use php to turn mysql queried data into json. I get a blank page with everything else that I have tried.

 

function actionJson ()

    {

      header("Content-type: application/json; charset=utf-8");

      //require_once('inc/header.inc.php');

      

      $dns = 'mysql:host=localhost;dbname=dbname';

      $user = 'username';

      $pass = 'dbpassword';

 

      try{

        $db = new PDO ($dns, $user, $pass);

 

      }

      catch( PDOException $e){

          $error = $e->getMessage();

          echo $error;

      }

      $query = 'SELECT * FROM `sys_menu_top`';

      $stm = $db->prepare($query);

      $stm->execute();

      $rows = $stm->fetchAll(PDO::FETCH_ASSOC);

      echo json_encode($rows);

    }

In you modules main module class. That's the class ending with module.php in the classes folder.

Just add a action function.


function actionGetjson() {
    header("Content-type: application/json; charset=utf-8");
    $query = 'SELECT * FROM `datable`';
    $stm = $db->prepare($query);
    $stm->execute();
    $rows = $stm->fetchAll(PDO::FETCH_ASSOC);
    echo json_encode($rows);
}


And that's it. Any function in your main module class prefixed with the word action in front of the name can be called on the url.

so, I created a new module.

I need a page that just returns json data - which is info I am going to pull in from the database.

 

this is the code:

 

header("Content-type: application/json; charset=utf-8");
$query = 'SELECT * FROM `datable`';
$stm = $db->prepare($query);
$stm->execute();
$rows = $stm->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($rows);


I want to access this page as you would any other module page using the following url scheme for example:  http://cheetahsite.com/m/newmodule/getjson

 

any help would be greatly appreciated.