[support] Some questions re custom PHP pages in Drupal

Walter Garcia-Fontes walter.garcia at upf.edu
Tue Feb 19 09:30:24 UTC 2008


* Donn [19/02/08 08:13]:
> Can I use the tools it provides (like login and templates etc.) to create 
> a 'section' on a website where my own PHP code runs a
> mini-application like a  
> shopping-basket (browse, filter, add, view, checkout) or something like a 
> track-a-parcel system and so on? 

I guess you can do it, I'm doing it at a much simpler level, but I
don't see why you can do it combining your code, drupal theming and
features and your external database. 

Here is my example:

I use db_set_active to change to another db, perform the queries, then
go back to drupal to use its theming capabilities and rest. Here is an
example where I query another db and then create a table in drupal,
paged and sortable. You can define multiple databases in settings.php,
look at http://drupal.org/node/18429. 

Here is an example, I enter this direcly into a content type where I
allow php in the body only to the admin. "ampa" is a table in an
external database:

<?php

db_set_active('ampa');

$sqlCount = "SELECT COUNT(*) FROM ampa";

$header = array(
  array('data' => 'Nom', 'field' => 'nom'),
  array('data' => 'Centre', 'field' => 'centre'),
  array('data' => 'Població', 'field' => 'poblacio','sort' => 'asc'),
  array('data' => 'Comarca', 'field' => 'comarca')
);

$sql = "SELECT * FROM ampa";
$sql .= tablesort_sql($header);

$ta = array('id' => 'my-table', 'align' => 'center');

$result = pager_query($sql,  30, 0, $sqlCount);

$trows = array();
while ($row = db_fetch_object($result)) {
     $trows[] = array(
array('data'=>$row->nom,'valign'=>'top'),
array('data'=>$row->centre,'valign'=>'top'),
array('data'=>$row->poblacio,'valign'=>'top'),
array('data'=>$row->comarca,'valign'=>'top')
);
}

db_set_active('default');

    $output = theme('table', $header, $trows,$ta);
    $output .= theme('pager', NULL, 30, 0);
 
print $output;

?>



-- 
Walter Garcia-Fontes
Barcelona



More information about the support mailing list