I'm sure there's a way, like using the PHP snippets but adding the database information but I can't seem to make it work.

What I want is to print node titles from a particular term outside Drupal's directory. Any know how to do this? I've searched and Googled with no luck.

Comments

cog.rusty’s picture

What do you mean "outside Drupal's directory"?

Do you mean to write a php application which connects to Drupal's database, runs queries and prints some results?

Duplika’s picture

I'd like to know exactly that. How do I use any of the PHP snippets in a website in a different directory, outside Drupal (but on the same domain)?
--
Duplika | Web Hosting

cog.rusty’s picture

Oh, I see... The snippets have been written to be used from within a running Drupal installation, but I remember someone doing something like this with a script.

For this to work in Drupal 5.2 you need to set $cookie_domain='example.com'; in settings.php, or else you will always be an "anonymous" visitor. In older versions that is not necessary. And of course you need to be logged-in as a user with the proper permissions.

$drupal_dir = "/home/[blahblah]/public_html";  // wherever Drupal is
$current_dir = getcwd();
chdir($drupal_dir);

require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// Here goes the snippet code, for example:
// -----------------------------------------
print $user->uid . '  ' . $user->name;
print '<br /><br />';

$terms = array(7, 9, 13, 14);
$termstr = '(' . implode(', ', $terms) . ')';

$query = "SELECT n.nid, n.title FROM {node} n JOIN {term_node} tn ON tn.nid = n.nid WHERE n.status = 1 AND tn.tid IN $termstr ORDER BY n.nid ASC";
$result = db_query(db_rewrite_sql($query));
$output = "";
while ($item = db_fetch_object($result)) {
  $output .= '<br />' . $item->nid . '  ' . $item->title;
}
print $output;
// --------------------------------------
// ...
// ...
chdir($current_dir);
return;
Duplika’s picture

Thanks for your answer cog.rusty, it seems to work for me but I get some errors.

I'd like to make a list of latest nodes from a given type so, with your code, I use:

<?php
$drupal_dir = "/home/duplikac/public_html/blog";  // wherever Drupal is
$current_dir = getcwd();
chdir($drupal_dir);

require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// Here goes the snippet code, for example:
// -----------------------------------------
$node_type = "story";
$list_no =5;
$sql = "SELECT node.title, node.type, node.nid FROM {node} WHERE node.type = '$node_type' AND node.status = 1 ORDER BY node.created DESC LIMIT $list_no";
$output .= "<ul>";
$result = db_query($sql);
while ($anode = db_fetch_object($result)) {
$output .= "<li>".l($anode->title, "node/$anode->nid")."</li>";
}
$output .= "</ul>";
print $output;
// --------------------------------------
// ...
// ...
chdir($current_dir);
return;
?>

And where I use it, I get:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/yadayada/public_html/index2.php:6) in /home/yadayada/public_html/blog/includes/bootstrap.inc on line 862

Warning: Cannot modify header information - headers already sent by (output started at /home/yadayada/public_html/index2.php:6) in /home/yadayada/public_html/blog/includes/bootstrap.inc on line 531

Do you have any clue what could be wrong?
--
Duplika | Web Hosting

cog.rusty’s picture

Make sure that your index2.php file does not contain spaces, empty lines or anything else outside the <?php ... ?> tags. That's what usually causes a "headers already sent" error.

Or don't use a closing ?> tag at all (like all drupal modules do).

Sometimes this also happens with some text editors which add an invisible Unicode "bom" marker character at the beginning of the file.

Duplika’s picture

I looked for empty spaces or lines without success and finally got it working like this:

<?php
$drupal_dir = "/home/duplikac/public_html/blog";  // wherever Drupal is
$current_dir = getcwd();
chdir($drupal_dir);

require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); ?>

html yada yada html yada yada
html yada yada html yada yada

<?php // Here goes the snippet code, for example:
// -----------------------------------------
$node_type = "story";
$list_no =5;
$sql = "SELECT node.title, node.type, node.nid FROM {node} WHERE node.type = '$node_type' AND node.status = 1 ORDER BY node.created DESC LIMIT $list_no";
$output .= "<ul class='lista'>";
$result = db_query($sql);
while ($anode = db_fetch_object($result)) {
$output .= "<li>".l($anode->title, "node/$anode->nid")."</li>";
}
$output .= "</ul>";
print $output;
// --------------------------------------
// ...
// ...
chdir($current_dir);
return;
?>
more html code yada yada
more html code yada yada

And I don't get any errors but problem now is that everything after the second part of the php code doesn't show on the browser. And if I delete the last line return;, it does show but with all the special characters like accents (á é í, etc.) with weird symbols.
--
Duplika | Web Hosting

Duplika’s picture

I'm also trying to do this "old way", not using bootstrap. At the beggining of the php, I use mysql_connect, I run the query on the SQL database and that's it but I can't manage to make this:

$sql = "SELECT node.title, node.type, node.nid FROM {node} WHERE node.type = 'story' AND node.status = 1 ORDER BY node.created DESC LIMIT 5";
$output .= "<ul class='lista'>";
$result = db_query($sql);
while ($anode = db_fetch_object($result)) {
$output .= "<li>".l($anode->title, "node/$anode->nid")."</li>";
}
$output .= "</ul>";
print $output;

... work without bootstrap or any other Drupal file.
--
Duplika | Web Hosting

cog.rusty’s picture

db_query(), db_fetch_object() and l() are Drupal functions (the {node} braces too). They do not exist without Drupal.

cog.rusty’s picture

No idea what is happening. I can reproduce your error message ("at index2.php:6") only if I add 5 empty lines on the top of the file.

Of course the last "return" would terminate the script and it was not needed anyway.

Duplika’s picture

Thanks for your patience cog.rusty. See here for what happens with the site if I take out the return.

Everything is ok except that every special character is displayed as a symbol. For example, "Además" is displayed as "Adem�s".
--
Duplika | Web Hosting

cog.rusty’s picture

I see...

My browser says that the character encoding is utf-8 (as Drupal would be)
The page source says "charset=iso-8859-1"

If I force my browser to iso-8859-1, then the bottom text is ok but the last link's text is ruined.

Are there characters coming from two different application on that page? From another one's in iso-8859-1 and fom drupal in utf-8? Which one comes from the script?

--------

By the way, if you have static text to convert, there is little free Windows text editor, Notepad2 (http://www.flos-freeware.ch/notepad2.html), which I use all the time to convert text to/from utf8. Except if you are familiar with php iconv.

Duplika’s picture

That was it! Drupal's content "came" with utf-8 and the page was in iso-8859-1! I've changed the page to utf-8 and everything is perfect now. Thanks a lot cog.rusty!
--
Duplika | Web Hosting

misty3’s picture

subscribed

US421’s picture

subscribe

moksa’s picture

and work ! thanks a lot.