I'm developing a module that uses JpGraph to draw some graphs. JpGraph works by generating an image, so you set an img tag's src to be some .php file. I'd like the graph to grab some data from the Drupal database, but the graph .php file isn't defined within any hook functions, so it doesn't have access to any Drupal functions.

Does anyone know how I could get this to work, short of 1) passing database values as parameters into the .php file (bad once I have many parameters to graph) or 2) directly connect to the database within the .php file, thus bypassing the standard db_query code? Neither option sounds that great, and I imagine other modules must come up against similar problems...

Comments

dwees’s picture

I think you can use something like include('path/to/bootstrap.inc'); to accomplish this, but I'd look up the instructions for bootstrapping.

Dave

Arthaey’s picture

At Dave's suggestion, I searched for info on bootstrapping. Another forum thread suggested looking at the Devel module for example code. And the documentation for drupal_bootstrap() says,

The most important usage is that if you want to access Drupal database from a script without loading anything else, you can include bootstrap.inc, and call drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE).

Below is what I got to work, from the directory drupal/modules/mymodule/script.php:

chdir('./../../'); // for relative path includes to work
include_once "includes/bootstrap.inc";

drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
$result = db_query("SELECT title FROM {node} n WHERE type = 'blog'");
while ($node = db_fetch_object($result)) {
   $str .= $node->title;
}

If there are issues with using chdir() like this, please let me know.

neubreed’s picture

here's some additional notes on how to get the full environment: http://drupal.org/node/70006

macm’s picture

Hi Works fine this solution.

I can get data from my database. nice!

Is it possible call Drupal function into php file?

Something like:

<?php
chdir('./../../'); // for relative path includes to work
include_once "includes/bootstrap.inc";

drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);

?????????????????????????????

$language = i18n_get_lang(); // <= Module Drupal function
pint $language;

Regards

papile’s picture

Once you are bootstrapped, you can call any drupal function

macm’s picture

So Help me with this

Drupal node-form render is slow. (with hundreds of terms and Super_Select module).

So I'm thinking do this hack.

in my page.tpl.php I will have

// page.tpl.php
elseif (arg(0)=="node" && arg(1)=="myccktype") { 
  include('page-mycustomccktype.tpl.php');
  return;
}

in page-mycustomccktype.tpl.php I will have a

// page-mycustomccktype.tpl.php
<script>
$(document).ready(function(){
$("#page_content").load("/drupal/themes/garland/page-cckajax.tpl.php");
});
</script>

What I am trying to do is in "page-cckajax.tpl.php" tell to Drupal.
that this page is in node/add/myccktype

So I tried

// "page-cckajax.tpl.php" 
chdir('node/add/myccktype'); // for relative path includes to work
include_once "includes/bootstrap.inc";
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
arg(0)=="node";
arg(1)=="add";
arg(2)=="myccktype";
print $content;

or

<?php 
chdir('./../../'); // for relative path includes to work
include "includes/bootstrap.inc";
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$_SERVER["REQUEST_URI"] = $base_url . '/node/add/myccktype';
$form['form_id']['#value'] == 'myccktype_node_form';
print $form;


or 
include('page-nodeaddmyccktype.tpl.php')

NOT WORKING I tried lot of things

How can I force the path to page-cckajax.tpl.php? (this page will load with AJAX) Every time run index.php

You can see I will let my slow node-form run with ajax.

Or better I will run page-mycustomccktype.tpl.php and behind the scenes I will run "page-cckajax.tpl.php"

I will let my user fill a simple container with title and body and after this using Jquery I will redirect everything to next step, the true form (load from page-cckajax.tpl.php) and finnaly user could fill images and categories. And it is done. Will be amazing.

Two big problems

1) I need tell in page-cckajax.tpl.php the correct path and avoid index.php. like I said
2) I need tell to Drupal that the form from node/myccktype is same from node/add/myccktype or something like that

So

So I would like to know if is possible tell to drupal that 'page-cckajax.tpl.php' is in node/add/myccktype

Is it possible? In other word I am trying pass argument. Where argument is the path. I have two chances one in page-cckajax.tpl.php other in page-nodeaddmyccktype.tpl.php

Because Drupal understand that 'page-cckajax.tpl.php' is in 'node/' directory.

So drupal could print $content. Other chance is create a module and try print theme_mycck but still not working. I dont know but I cant invoke this theme_mycck from my module in 'page-cckajax.tpl.php'.

function theme_mycck($form) {
		$output = drupal_render($form);
		return $output;
}

Regards

johnjn’s picture

Hi,

I am unable to get the following code to work (to enable access to DRUPAL's database). On running the script nothing happens. Apache error log pasted below. Apparently some of the code in bootstrap.inc does not execute completely for some reason.

Would appreciate advice. Thanks

Script - all_nodes.php as below

<?php

chdir('C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/drupal78');

include_once('./includes/bootstrap.inc');

drupal_bootstrap(DRUPAL_BOOTSRTAP_DATABASE);

$result = db_query('SELECT nid, title FROM {node}');

foreach ($result as $row) {
echo $row->title . "
";
}

DRUPAL Stack - Windows 7/ Apache 2.2.x/ PHP 5.3.8/ MySQL/ DRUPAL 7.8

Apache error log -

[Tue Jan 24 13:03:51 2012] [error] [client 127.0.0.1] PHP Stack trace:
[Tue Jan 24 13:03:51 2012] [error] [client 127.0.0.1] PHP 1. {main}() C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\all_nodes.php:0
[Tue Jan 24 13:03:51 2012] [error] [client 127.0.0.1] PHP 2. drupal_bootstrap() C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\all_nodes.php:7
[Tue Jan 24 13:03:51 2012] [error] [client 127.0.0.1] PHP 3. _drupal_bootstrap_configuration() C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\drupal78\\includes\\bootstrap.inc:2071
[Tue Jan 24 13:03:51 2012] [error] [client 127.0.0.1] PHP 4. drupal_settings_initialize() C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\drupal78\\includes\\bootstrap.inc:2183
[Tue Jan 24 13:03:51 2012] [error] [client 127.0.0.1] PHP 5. _drupal_error_handler() C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\drupal78\\includes\\bootstrap.inc:720