How to add a context var!?

Started by nvcnvn, November 17, 2010, 08:06:18 AM

Previous topic - Next topic

nvcnvn

Hi you! I'm try to design a Mod wich need to use some global var!

I trying to add these value into a context var so I can use them any where!

The question is how can I create a context var!?

Please help me! Thanks!

All Colours Sam

$context['my_value'] =  $my_variable   that will work,  then you could use $context['my_value']  everywhere  :)
Oh, wouldn't it be great if I *was* crazy? ...then the world would be okay
Suki

nvcnvn

Quote from: Miss All Sunday on November 17, 2010, 08:10:43 AM
$context['my_value'] =  $my_variable   that will work,  then you could use $context['my_value']  everywhere  :)

Do I need to add this in a specific file!? Or any file!? (My own filw with a required SSI.php)!?

All Colours Sam

as far as I know you can do this in any file inside SMF or out side by calling SSI.php
Oh, wouldn't it be great if I *was* crazy? ...then the world would be okay
Suki

IchBin™

But it also depends on when you want that context variable to be available. For example, ou put it in Boardindex.php it will only be available on the board index page.
IchBin™        TinyPortal

nvcnvn

Thanks, that help! Beacuse I have a small proplrem like this:
in test.php there is some line read the content from a text file, put it in a var! and a function use this var as a global var!
if I run test.php in the test2.php (test2.php require SSI.php test.php), ok. But I try require test.php in BoardIndesm.template.php and it does not work!!

Test.php

<?
$var_name = file_get_content(home/nvcnvn/smf/'mytext.txt');
function
printtext(){
global
$var_name;
echo
$var_name;
}
?>


test2.php

<?
require(
'SSI.php');
//When I do the same in BoardIndex.template.php It's not work!
require('test.php');
printtext();
?>

IchBin™

When you include the file in your boardindex file you should probably use the full path to the php files. I don't know off the top of my head what the correct path would be, but using the full path should work.

require('/home/path/to/test.php');
IchBin™        TinyPortal

nvcnvn

yes, i have tried like this! But....for an example, in BoardIndex.php (I put these line in the main function):

require('home/path/test2.php')
echo
$var_name;
printtext();


if all the thing ok, I mus see two more line (because printext is simply echo $var_name out) but in my case, I just get 1!!

IchBin™

That is a relative path you have shown. The full path ALWAYS starts with a forward slash.
IchBin™        TinyPortal

nvcnvn

Quote from: IchBin™ on November 20, 2010, 01:51:21 PM
That is a relative path you have shown. The full path ALWAYS starts with a forward slash.

Yes, I know!!!
But if my path is wrong, how can I still get the #1 result!?

IchBin™

Can you post the code you have now? The code you posted above has an error:

This: $var_name = file_get_content(home/nvcnvn/smf/'mytext.txt');

Should be this: $var_name = file_get_content('/home/nvcnvn/smf/mytext.txt');
IchBin™        TinyPortal

nvcnvn

Can I send you my file and you can test it for me!

My coding a mod for SMF to, when I get over this error I hope I can submit it to SMF :D

IchBin™

Sorry, I've got too many things on my todo list to spend the time testing for you. I don't mind stopping and looking at some code if you want to post it. Otherwise, someone else will have to help.
IchBin™        TinyPortal

nvcnvn

OK, this is my file:
test.php

<?php
$textlic
= file_get_contents('/home/nvcnvn/license.txt'); //replace this text with your FULL path to liceense.txt

function textlicshow(){
global
$textlic;
echo
$textlic;
echo
'ZZZZZZZZZZZZZZZZZZZZZ';
}
textlicshow();
?>

run the file alone and make sure it print out the content of license.txt

After that, with the Index.template.php, I have add these code after <body> tag

require('/home/nvcnvn/test.php');


After do ing that, I though I would get the content of license.txt but all I get is ZZZZZZZZZZZZZZ

All Colours Sam

why not include $textlic = file_get_contents('/home/nvcnvn/license.txt');  inside the function?
Oh, wouldn't it be great if I *was* crazy? ...then the world would be okay
Suki

nvcnvn

Quote from: Miss All Sunday on November 24, 2010, 11:09:20 AM
why not include $textlic = file_get_contents('/home/nvcnvn/license.txt');  inside the function?

Because this var is using for some other function! $textlic is just an example, in fact, I have do some thing else with these var before using it!

Any Ideal about this issue!?

All Colours Sam

make $textlic  a $context['textlic']  and use it on your functions:

global $context;
$context['textlic'] = $textlic;

my_function() {

global $context;

return $context['textlic'];
}


$one = my_function();

echo $one;
Oh, wouldn't it be great if I *was* crazy? ...then the world would be okay
Suki

spottedhog

Here is one way to do what I think you are trying to do:

You could place all the text files into one folder so you can then call any of them in the same function using code like this:

function textlicshow($filename){
include("/home/yourname/public_html/ext_files_path/$filename");  // change to your own path to files
echo '<br />';
echo 'ZZZZZZZZZZZZZZZZZZZZZ';
}
$filename = "license.txt";
textlicshow($filename);

nvcnvn


Advertisement: