I'm modifying a web app (care2x) which is uses smarty for templating.
as you all know smarty uses two files .php for logic code and .tpl for template.
In .php file some php variables are used to assign to smarty variables
for example
$this->smarty->assign('template-variable',$php-variable);
I can't figure out where $php-variable gets its value from?
is it same as passing values from controller to view in ci?
Any help is much appreciated.
I mean that $php-variable is variable from PHP because in variable names you cannot use - sign.
Those variables of course come simple from PHP, so what you should do is to search simply in PHP files this variable. They can be defined in the same file or in other files depending on system architecture.
The easiest way of course is when variable is defined just before assigning its value to Smarty as following
$phpVar = 2;
$this->smarty->assign('template-variable',$phpVar);
but in case it's not, you should probably use your editor or for example Total Commander to find this variable.
Without any more details and full code it's hard to say where it can be defined.
Related
I am in a situation .
My php folder structure is like
UI
user
login.php
logout.php
jquery
somejs
css
somecss
blah.php
blah.php
Now to import any css ,js or any php file i am using the file path like
localhost/UI/user/index.php // example
Now i am trying to define a global variable on any page like
<?php
$somevar = "localhost";
GLOBAL $somevar;
?>
So that i could import any css js like
<?php echo $somevar ;?>/UI/user/index.php // example
Problem : It is working on that page only where i declared the variable as GLOBAL
I want to use the variable on each page and don't want to use include
Is there any other alternative to define a variable for files folder in php ?
You do not declare variable with global. You just make it available within you method or function body even it was set (or using your terminology "declared") outside of it. So there's no way to have the variable unless it is declared. And there's no way to do that without running the PHP code (simplification, but it does not matter here). And code is not coming from nowhere, hence the need of include or require of said code that sets variable.
You may try to use php.ini's auto_include_file to have your variables auto-included, but still, the PHP code needs to be used for that.
But you generally doing it wrong. move all global variables into class, set autoloader and access i.e. statically. The code will be much cleaner.
Well, my experience says .. global is used for accesing variables inside the function those which defined outside the function.
There can be many different solutions to the same, one i will suggest is use sessions or cookies. Store the data in session / cookies and access it across wherever required.
You have to use define() function for this purpose
config.php
<?php
define("host", "localhost");
?>
now include this page wherever you want to access HOST variable.
you can access this variable like this echo host;
You can use the special PHP-defined $GLOBALS array. The $GLOBALS array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element. Notice how $GLOBALS exists in any scope, this is because $GLOBALS is a superglobal
You may not need to define a global variable to apply the css, js or any static files viz Images etc to your application. you may use relative path to include js and css files. You can refer to this article to know more about relative and absolute path.
This is a newbie question, and I know it.
Template structure is your usual index.php, with a few require_once()'s for the header/footer etc.
I define a var at the top of index.php before any of the require_once()'s for the base url, such as $url = 'http://url';
I then want to echo this out into all template files, header/index/footer etc, it works inside index.php as expected, but fails with a undefined var in all template files that are included in.
I know it's a var scope issue, but I'm totally perplexed how to fix it.
I'm aware that the manual says vars are available to included files, however they aren't. Could it be a issue with my local PHP install?
edit : Created a couple of test files, and a var is defined between 2 files, so why are they not working on my main site files?
Any helps gracefully recieved.
Many Thanks
if you use functions or methods (functions in classes) then you need to do global $variable inside the function. Otherwise you will not have access to it, you also could define it as constant. A constant is always global.
define('MYURL', $url);
You might want to use a PHP framework, if you not already do so.
Hey guys, Im building a fairly large website here, I am using quite a bit of php along with it, but what I was wondering, I have a header that does't change throughout the website, and I was wondering if I could create a function in some of my php code where all I would have to do is call like a function getHeader() and it will return the header. Now this header has some php in it also like a search bar and a username container... I was just wondering if this was possible on a fairly simple scale so I don't have to place the header code in each php file. which is fine but if I happen to make an update I have to update file which could take some time...
Thanks in advance!
Just create a header file (e.g. header.php) and include it.
http://php.net/manual/en/function.include.php
You can either have the code directly in the header.php not in any functions, and it will run by default, or put it in a function and call it manually.
A simple include() at the top of every file is what you'll need http://php.net/manual/en/function.include.php
You could use a function. That's what WordPress does. You could even put PHP inside them. You'd just have to keep in mind that the PHP inside your function is... well... inside a function. So you'd have to explicitly access global variables, etc.
Another option would be to put the header in its own, single file and just include that file, instead of calling a function. Whatever floats your boat.
I am working on a new PHP project now, this time I want to get the basics right at the beginning. Previously I've found requiring/including files in php a bit of pain, please consider the following structure:
/application_root/index.php
/js/...
/css/...
/php/...
/conf/...
...
In the index.php I can certainly use something like:
<link rel="stylesheet" href="css/sample.css" ... />
<script type="text/javascript" src="js/sample.js"></script>
To refer to the included css and js, or even php snippets. However, this would only work in the index.php which resides under the root of my application folder. I reckon this is no good.
I came across Java application configuration file "web.xml" where you can define application scope variables that you can simply refer to. .NET with C# has a similar thing. How to achieve this in simple php code so that from any php file in my app, I can type:
<?php echo "<link href='".$application_root_url."/php/sample.css' ..."; ?>
And it will evaluate to the right location?
I am thinking to use:
Global variables <== bad practice as violation to OOP? I stop doing this since c programming;
set_include_path <== so php will look for it, requires unique name and proper naming convention?
load variables from ini files? <== how to make this happen?
any new thoughts?
You don't want to use global variables because they break encapsulation.
set_include_path will do no good especially if you are using those variables in HTML, because the include_path is relative to the application's filesystem path and not to its base url.
Determining the application base paths is generally not done from a configuration file, as it easy to detect when your application has a gateway script. Since those are constant values, it makes sense to define a constant:
define('APP_ROOT', dirname(__FILE__));
define('APP_URL', dirname($_SERVER['PHP_SELF']));
If you want to parse INI files however, you could use parse_ini_file, or parse_ini_string (>=5.3.0):
$config = parse_ini_file(APP_ROOT.'/'.CONFIG_DIR.'/database.ini');
echo $config['host'];
The path here is a URI--not a filesystem location. You may be trying to go about this the wrong way anyway.
You have to use relative path. So just "/css/sample.css" instad of "css/sample.css".
That would than always load from yourdomain.com/css/sample.css even if your .php is in yourdomain.com/somefolder/file.php
The PHP script only produces an output that the browser interprets. And the scope is the URL in the browser not on the filesystem.
So the value of the $application_root_url variable is for the browser not for the PHP script!
If you want to use INI files, you can use the parse_ini_file() function of PHP.
no reason for using global variables except for lazy coding
is not efficient, and PHP will get harder to figure which file to be included if two same filename on different path
parse_ini_file is what you looking for
However, I prefer using constant, I did not ask to define constant everywhere, just put all your essential path into a config file, and require that at the beginning on your application.
Some might say constant is slow, comparing using class constant which might require you to include multiple files, which does better ? And the best things is once constant defined, no-one or code can override it.
example to illustrate
define('css_root', '/home/user/apache/css'); <-- server absolute path
define('css_web_root', '/css'); <-- web root, for HTML
define('css_cache_root', '/cache/css'); <-- cache directory
You might want to try something like the MVC pattern. As an example the ZendFrameworkds MVC passes a variable $this->base_url to the views. The views are where you HTML resides so you will be able to do the following in the view:
<link rel="stylesheet" href="<?php echo $this->base_url; ?>/css/sample.css" ... />
Your problem is exactly the reason that led me to the MVC pattern and it's many advantages.
I am wondering if there is a way to pass a variable to a file you are including via include()?
I tried this but got an error:
include("header_alt.php?img=hey");
Is there a way to do that?
Thanks!
Just define your variable in the first file ; for instance, in temp.php :
<?php
$my_var = 10;
include 'temp-2.php';
die;
?>
And use it in the second file ; temp-2.php :
<?php
var_dump($my_var);
?>
And it should work : I'm getting this output, from temp-2.php :
int 10
The query-string syntax, using stuff like ?img=hey is used when you are requesting some data from a distant server (like when you are using your browser to surf on websites), not when including a file that is on the same server.
Smarty provides a really good mechanism for this sort of thing. Plus, using Smarty just makes for better php applications.
http://www.smarty.net/manual/en/language.function.include.php
Variables can be passed to included
templates as attributes. Any variables
explicitly passed to an included
template are only available within the
scope of the included file. Attribute
variables override current template
variables, in the case when they are
named the same.
All assigned variable values are
restored after the scope of the
included template is left. This means
you can use all variables from the
including template inside the included
template. But changes to variables
inside the included template are not
visible inside the including template
after the {include} statement.