Advertisement:

Using context array in another php file

Aloittaja anir, heinäkuu 11, 2013, 03:49:42 IP

« edellinen - seuraava »

anir

Hello in my website I require some information, how can I use information in the context array in another PHP file which is not related to SMF ? How can I relate it to SMF to be able to use $context array...
SMF is the best forum I have ever used, efficient support system has always helped me and the very nice documentation too!

Kays

Hi, you can do that if you include SSI.php first. :)

SSI Readme

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Colin

If you are looking for some more specific help, you will need to provide some existing code and we can help you work SMF integration into it!

Welcome to SMF
"If everybody is thinking alike, then somebody is not thinking." - Gen. George S. Patton Jr.

Colin

anir

Hello Colin and Kays, thankyou very much for replying,


File named test.php is in the index directory. I want to echo out $context['time_logged_in'], this variable was inside the Profile.Template.php.


This is code in the test.php (it does't work):


<?php
require("SSI.php"); 

echo 
$context['time_logged_in'];  //This was inside the profile.template.php, but its not working.
?>



SMF is the best forum I have ever used, efficient support system has always helped me and the very nice documentation too!

Colin

Sure. You will need to declare the global context.


<?php
require("SSI.php"); 
GLOBAL 
$context;

echo 
$context['time_logged_in'];  //This was inside the profile.template.php, but its not working.
?>



"If everybody is thinking alike, then somebody is not thinking." - Gen. George S. Patton Jr.

Colin

Illori


Colin

"If everybody is thinking alike, then somebody is not thinking." - Gen. George S. Patton Jr.

Colin

Illori

given they want data from the profile, i would assume profile.php

anir

Thanks guys, I managed to make it work somehow. But I had to directly link with forum database.

The $context works only for ['user'], nothing else, it doesn't work for ['time_logged_in']

I had done only what Colin said, @Illori, I have not tried profile.php but I think the information is coming from Load.php so I just linked it with database directly.
SMF is the best forum I have ever used, efficient support system has always helped me and the very nice documentation too!

Colin

@Illori - gotcha

I am curious, mind showing us the final version of the script?
"If everybody is thinking alike, then somebody is not thinking." - Gen. George S. Patton Jr.

Colin

Illori

actually it is defined in Profile-View.php

Illori

$context['time_logged_in'] = ($timeDays > 0 ? $timeDays . $txt['totalTimeLogged2'] : '') . ($timeHours > 0 ? $timeHours . $txt['totalTimeLogged3'] : '') . floor(($user_profile[$memID]['total_time_logged_in'] % 3600) / 60) . $txt['totalTimeLogged4'];

looks like without $user_profile[$memID] defined using this function outside the profile would not work out too well.

anir

#12
Hello,
Colin, since you are curious, here is how I did, in simple way:


<?php
require("SSI.php"); 

echo 
'Hello '.$context['user']['name'].'<br> Your total time online is: '
$total_time_logged_in 4050607//Obtained from database: Just a raw query for explanation: SELECT * FROM smf_members WHERE id_member=$context['user']['id']

$time=secondsToTime($total_time_logged_in);

echo 
$time['d'].' days, '.$time['h'].' hours, '.$time['m'].' minutes, '.$time['s'].' seconds ';


function 
secondsToTime($inputSeconds) {

    
$secondsInAMinute 60;
    
$secondsInAnHour  60 $secondsInAMinute;
    
$secondsInADay    24 $secondsInAnHour;

    
// extract days
    
$days floor($inputSeconds $secondsInADay);

    
// extract hours
    
$hourSeconds $inputSeconds $secondsInADay;
    
$hours floor($hourSeconds $secondsInAnHour);

    
// extract minutes
    
$minuteSeconds $hourSeconds $secondsInAnHour;
    
$minutes floor($minuteSeconds $secondsInAMinute);

    
// extract the remaining seconds
    
$remainingSeconds $minuteSeconds $secondsInAMinute;
    
$seconds ceil($remainingSeconds);

    
// return the final array
    
$obj = array(
        
'd' => (int) $days,
        
'h' => (int) $hours,
        
'm' => (int) $minutes,
        
's' => (int) $seconds,
    );
    return 
$obj;
}
?>




Output:
Koodi (Output) [Valitse]

Hello NAME
Your total time online is: 46 days 21 hours 10 minutes 7 seconds.




If anyone finds even better way then please let me know, actually I don't really wish to connect to the forum database, but looks like I have no other choice for now because it is working alright.
SMF is the best forum I have ever used, efficient support system has always helped me and the very nice documentation too!

Kays

You can use the following:


$context['user']['total_time_logged_in']['days']
$context['user']['total_time_logged_in']['hours']
$context['user']['total_time_logged_in']['minutes']


Tip: Use print_r() to see what values are set for an array.


echo '<pre>', print_r($context), '</pre>';


If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

margarett

Lainaus käyttäjältä: Kays - heinäkuu 13, 2013, 08:28:01 AP
Tip: Use print_r() to see what values are set for an array.


echo '<pre>', print_r($context), '</pre>';



One of the best things I read for a long time :)
Thanks

Sorry for this small thread hijacking :)
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

LainaaOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

anir

Omg thankyou very much Kays!

And thankyou everyone else who helped, and thanks to SMF community who has professional coders + support and made best forum ever.


Marked topic as solved.
SMF is the best forum I have ever used, efficient support system has always helped me and the very nice documentation too!

Advertisement: