Page 1 of 1

How to access Joomla global variables from non-Joomla sub-directory

Posted: Mon Oct 29, 2007 3:29 pm
by emille3000
Hi there,

This is probably 100% newbie question with a simple answer...but anyway....

I've created a custom dir located in joomla root dir with a custom PHP file (file.php), but I do not know
how to call/access global Joomla variables $databse and $my directly from my custom file "file.php".

joomla-root-dir/my-custom-dir/file.php

what code do I need to include () into the "file.php" if I want to access
global Joomla variables $database and $my?

Best regards,

Emille

Re: How to access Joomla global variables from non-Joomla sub-directory

Posted: Thu Nov 01, 2007 3:46 pm
by RedGerry
I would also appreciate an answer to this.

I'm using a custom file with Ajax pagination (http://www.dynamicdrive.com/dynamicinde ... /index.htm) and am using a query to get the number of pages required then dynamically generating the array for output. Problem is that the query needs access to $database global. As it is called as a standalone page it's not happening. For anyone interested the code is below:

Code: Select all

<?php
global $database;
$page = (intval($_GET['page'])-1);
if ($page < 0){$page = 0;}
$inc_per_page = 6;      //set number of incidents to appear in each page
$start = $page*$inc_per_page;
$end = $start+$inc_per_page;
$query = '
    SELECT cf_id, incident, station, units, description, summary, closure
    FROM #__chronoforms_1
    ORDER BY incident DESC
    LIMIT '.$start.', '.$end.'
';
echo "$query";      // Debug line to display query
$database->setQuery( $query );
      if ($incidents = $database->loadObjectList()){
          foreach ($incidents as $incident){
// Get ID
         $id = $incident->cf_id;
// Modify MySQL times to Unix
             $idate = strtotime($incident->incident);
             $cdate = strtotime($incident->closure);
// Generate station link
             $istat = $incident->station;
             $istatlink = "about-us/local-fire-stations/$istat.html";
             $statlink = "<a href=$istatlink>$istat</a>";
// Tidy date displays
             $inicedate = date("g:ia D jS M",$idate);
             $cnicedate = date("jS M g:ia",$cdate);

            echo "<div id=incident_line>
               <div id=incident_lineone>
                   <div id=incident_station><b>Attending Station</b>: $statlink</div>
                   <div id=incident_status><b>Call</b>: $inicedate</div>
               </div>
               <div id=incident_linetwo>
                   <div id=incident_description><b>Report</b>: $incident->description</div>
                   <div id=incident_units><b>Units Despatched</b>: $incident->units</div>
                   <div id=incident_summary><b>Details</b>: $incident->summary</div>
               </div>
               <div id=incident_linethree>
                   <div id=incident_closetime><b>Closed</b>: $cnicedate</div>";
                $my_username = $my->username;
                if ($my_username == "control"){
                      echo "<div id=incident_delete>
                      <form action="statements/form-acknowledgements/incident-deleted.html" method="post">
                      <input type="hidden" name="id" value=$id>
                      <input type="submit" value="DELETE">
                      </form>
                      </div>";
                      }

               echo "</div>
            </div>";

         }
      }
?>

Re: How to access Joomla global variables from non-Joomla sub-directory

Posted: Fri Nov 02, 2007 2:17 pm
by RedGerry
anyone?
do'able?
not possible?

???

Re: How to access Joomla global variables from non-Joomla sub-directory

Posted: Sat Nov 03, 2007 4:27 pm
by lobos
Well you can always access global variables like this, but I am not sure if it will work as the database global might be tied to other functionality...

This is how you access a global variable:

$GLOBALS['database'];

or at the top of your function:

function myfunction(){

globals $database;

Re: How to access Joomla global variables from non-Joomla sub-directory

Posted: Fri Nov 09, 2007 9:25 am
by RedGerry
Thanks for the reply lobos.

Using jumi to 'embed' a php page into content this approach works fine. This is how this page is being generated:

http://www.centralscotlandfire.gov.uk/n ... dents.html

The incident list is a seperate php page called with the following line:

{jumi [includes/jumi/incident_list.php]}

and it works a treat.

However when i include a file inside incident_list.php  like this:

Code: Select all

global $database, $my;
$inc_per_page = 6;      //set number of incidents to appear in each page
echo "<div id=incident_container>
<h3>Most Recent Incidents</h3>";


$query = '
    SELECT cf_id, incident, station, units, description, location, summary, closure
    FROM #__chronoforms_1
    ORDER BY incident DESC
';

$database->setQuery( $query );
      if ($incidents = $database->loadObjectList()){
         $tot_incidents = count($incidents);
         $req_pages = ceil($tot_incidents/$inc_per_page);
         if ($req_pages > 10){ $req_pages = 10;}
         $i = 1;
         $idarray=array();
         while ($i <= $req_pages){
            array_push($idarray, "'incident_page.php?page=" . $i . "'");
            $i++;
         }
/*
         echo "<div id="paginate-top"> </div>
         <div id="pcontent"> </div>
         <div id="paginate-bottom"> </div>

         <script type="text/javascript">

         var ilpage={} //arbitrary variable to hold page settings for this book
         ilpage.page=[" . implode(",", $idarray) . "]\n
         ilpage.selectedpage=0 //set page shown by default (0=1st page)

         var mypages=new ajaxpageclass.bindpages(ilpage, "pcontent", ["paginate-top", "paginate-bottom"])

         </script>";
*/


The file incident_page.php which is listed in my previous post is using 'GET' to generate paged display. The problem is it is not picking up the joomla globals. i have even tried ammending the top of the file to:

Code: Select all

define( '_VALID_MOS', 1 );
require_once ( '/srv/www/htdocs/globals.php' );
global $database;


which i picked up from another post as a possible solution but to no avail. The Ajax pagination I'm trying to impliment here is sexy as bits and would be a major improvement to the display of this information but I continue to scratch my head  :'(

SOLVED: How to access Joomla global variables from non-Joomla sub-directory

Posted: Mon Nov 12, 2007 10:28 am
by RedGerry
well now got this working. The second page only includes a funtion call:

Code: Select all

<?php
require_once( $mosConfig_absolute_path . '/includes/func_common.php' );
paged_incidents();
?>


The head section of the function:

Code: Select all

function paged_incidents(){
define( '_VALID_MOS', 1 );
require ( 'globals.php' );
include ('includes/joomla.php' );
global $database, $my;


Works a treat:

http://www.centralscotlandfire.gov.uk/n ... dents.html

Re: How to access Joomla global variables from non-Joomla sub-directory

Posted: Sun Nov 25, 2007 6:30 am
by Babysittah
Hi, thanx 4 the info anyway:)