add a dynamic variable data into include function in php - php

I am trying to add a dynamic variable into include function in php
Here is the code I am trying to do but didn't work
$theme = "1";
include("s-include/themes/starecom-theme".$theme."/index.php");
$theme data is 1/2/3
and I want to include folder no 1/2/3 into include
its didn't give any error only blank page view.
actual url : s-include/themes/starecom-theme1/index.php but theme1/2/3 changed dynamically from the database which selected
Please help me to solved this issue
Thanks to All

If you get 1/2/3 from data-base and you have to include each file (seperated with /) then try below code. It will explode your data then include each file.
$theme = "1/2/3";
$theme_array = explode("/", $theme);
foreach ($theme_array as $key => $value) {
include("s-include/themes/starecom-theme".$value."/index.php");
}

Related

how to copy php file multiple file name using php?

i want to copy same php file using php , working with copying one file, I can copy one file , but I have problem when copying multi files using array.
$copys = file('copy.txt');
foreach($copys as $copy) {
copy('page1.php', '$copy');
}
copy.txt is name files:
page2.php
page3.php
page4.php
i want to copy page1 to page2, page3, page4 ,.. page100
But this code not work !
Could you give me a solution :(
Thanks for any help !
If you want to generate a specific number of copies and the numbers for the new file names you will have to use a for loop
<?php
$master = 'page1.txt';
$copy_to = 'page%d.txt';
$num_copies = 10;
// start you rloop at 2 so we start copying to `page2.txt`
// and dont overwrite page1.txt
for ($i=2; $i < $num_copies+2; $i++) {
copy($master, sprintf($copy_to, $i));
}
Do something like this:
$copys = arary('from.txt'=>'to.txt', 'from2.txt'=>'to2.txt');
foreach($copys as $from => $to) {
copy($form, $to);
}
Or if you want to copy the same file multiple times
$copys = file('copy.txt');
foreach($copys as $to) {
copy('page1.php', $to.".php");
}

Accessing variables in PHP files

Is there anyway to access variables of one php file into another?
- I am trying to validate a form. I need to access variables from the validationConditions.php file in form.php file.
I have tried creating sessions but they are error prone.
I am using the latest version of dreamweaver.
Is it possible to use $_POST to achieve the results?
Any example will be welcome...
Thanks a lot in advance.
The easiest would be to use session variables. In the first page you could set the values like this
session_start();
$_SESSION['myvar'] = 'My Data';
Then in the second page you can retrieve the data like this...
session_start();
$myvar = $_SESSION['myvar'];
Which in turn sets $myvar to "My Data"
You can read a php file with another php file. Assume that we have 2 php files.
new.php
<?php
$a="something";
?>
serach.php ---> search in new.php
<?php
// What to look for
$search = '$a'; //WE are searching $a
$lines = file('new.php'); // in new.php
foreach($lines as $line)
{
// Check if the line contains the string we're looking for, and print if it does
if(strpos($line, $search) !== false)
echo $line;
}
?>
Something like that. You have to edit this file.
A somehow devious solution:
ob_start();
$request = array(
"_id"=>"0815",
"user"=>"john",
"email"=>"john#dunbar.com",
"phone"=>NULL
); // Don't know. Just how u need your request to look like.
include('validationConditions.php');
$response = getValidationConditions($request); // Function in 'validationConditions.php' that responds an array or a JSON
$out = ob_get_clean();
echo json_encode($out);

Including a php file with includes into a smarty template

I want to include my header into a script that uses smarty templates. From searching this site, I am partially there, but not quite:
{include file='/home/username/public_html/header.php'}
This successfully includes the image in the header, but neither of two includes the header contains. One of the includes is a php file, and the other is html (my bootstrap nav bar). I seems from my searches that I need to make a plugin, which according to one post is "easy", but I can't find an example of how to accomplish this?
based on codefreaks inststructions, here's what I did. I'm sure the instructions are correct, I'm just not interpreting them correctly, as this isn't displaying anything.
Here are the three files, with their paths in relation to the public_html directory, and what I added to them. Everything is exactly as I put it: no words here are placeholders.
file 1
sitewide/myheader.php
<?
ob_start();
--- I didn't change the original content here --
$output = ob_get_contents();
ob_end_clean(); ?>
File 2
newscript/includes/page_header.php
$smarty = new Smarty();
require "/home/username/public_html/sitewide/myheader.php";
$smarty->assign('myheader', $output);
$smarty->display('../themes/default/template/header.tpl');
File 3
newscript/themes/default/template/header.tpl
{$myheader}
I dont think you can include your php file in smarty.
As samrty is a template engine,
PHP pages executes first, then it sends the data to your smarty page.
Solution : Pass all data needed from php page to smarty, and include html page, with smarty variables.
After getting feedback from you and testing myself, I figured out you might not have set up smarty properly. Follow instructions at http://www.smarty.net/quick_install to install it properly. My final directory structure after setting up properly looks like:
Once you have it set it up properly this is code I used in files:
index.php
<?php
echo "hello";
require_once "libs/Smarty.class.php";
$smarty = new Smarty();
$smarty->setTemplateDir('smarty/templates');
$smarty->setCompileDir('smarty/templates_c');
$smarty->setCacheDir('smarty/cache');
$smarty->setConfigDir('smarty/configs');
require "header.php";
$smarty->assign('header', $output);
$smarty->testInstall();
$smarty->display('smarty.tpl');
echo "hello";
?>
header.php:
<?php
ob_start();
?>
hello honey bunny
<?php
$output = ob_get_contents();
ob_end_clean();
?>
Put smarty.tpl in templates folder!
{$header}hellos
I am trying to do a very similar thing except all I want is some global PHP constants that I have defined in a stand alone constants.php file using define.
I want those same said constants in my smarty templates so I've been trying to include constants.php in the smarty template but this is better a better way to do this:
In constants.php
$_CONSTANT['TBL'] = TRUE;
$_CONSTANT['FLD'] = FALSE;
$_CONSTANT['DB_READ_SUCCESS'] = 1;
$_CONSTANT['DB_WRITE_SUCCESS'] = 1;
$_CONSTANT['DB_WRITE_ERROR_UNKNOWN'] = -100;
//*** Copy the $_CONSTANT array to PHP define() to make global PHP constants
foreach($_CONSTANT as $key => $value) {
define($key, $value);
}
Then in my smarty set up function I do this:
foreach ($_CONSTANT as $key => $value) {
Smarty->assign($key, $value);
}
And really I think variable (or constant) values is all you really want in your Smarty templates to keep your view layer separate from your model and controller layers.
Post Script:
In fact you can use this technique to pass your PHP constants, using Smarty, to JavaScript allowing you to declare you constants in one place for three different environments: PHP, Smarty, and JavaScript.
Here's how:
Call the following from your smarty set up function:
public function AssignJSConstants($values) {
$js = '';
foreach ($values as $key => $value) {
if ($key != 'CR') {
if (is_string($value)) {
$js = $js.$key.' = "'.$value.'";';
} else {
$js = $js.$key.' = '.$value.';';
}
$js = $js.CR.' ';
}
}
$this->Smarty->assign('JavaScriptConstants', $js);
}
And then declare the following in your smarty template:
<script>
//php constants that exported to JavaScript
{$JavaScriptConstants}
</script>
Which will give you this in your HTML:
<script>
//php constants that exported to JavaScript
TBL = 1;
FLD = 0;
DB_READ_SUCCESS = 1;
DB_READ_ERROR_UNKNOWN = -10;
DB_WRITE_SUCCESS = 1;
DB_WRITE_ERROR_UNKNOWN = -100;
</script>

i need to get the name of a variable from a file

As the title said i need a way to set the variable name depending of what the name of the picture is (i got over 100 different pictures)
Since i got custom classes in another php file for each picture (like tags) like for example:
$picture1 = "hillside sunset";
$picture2 = "beach cyprus";
and so on, so i need to fetch each variable for each picture
Heres the current loop where the div class is going to be each pictures name ($PICTURENAME is just to define where this code goes and is irelevant codewise):
<?php
foreach (glob("img/*.jpg") as $filename)
{
$path = $filename;
$file = basename($path);
$file = basename($path, ".jpg");
echo '<div class="'.$PICTURENAME.'" id="'.$file.'"><img src="'.$filename.'"> '.$file.' </div>';
}
?>
Don't use 100+ variables. Using a database would make far more sense, but if you don't want to get into learning that (you should, though), using a data structure would still make far more sense.
You could create one array (and use it as a map), and have the filename as the key, and the value would be the tags.
In PHP, you can address a variable using another variable:
$name = "foo";
${$name} = "bar";
echo $foo; // prints "bar"
echo ${$name}; // the same as above
However, as Kitsune already recommended, you are better off using something else, e.g., an array.

How to save php config?

I have a standard config file: $variable = 'value';, but at the last moment came up to use the web interface to configure it. So what is the best way to read the file, find the value of variables and then resave the file again?
At the moment I have 2 ideas:
1) RegExp
2) Keep somewhere array example
Store all config values in an associative array like so:
$config = array(
'variable' => 'value'
);
For the web interface, you can easily loop over the entire array:
foreach($config as $key=>$value) { ... }
After making changes, loop over the array and write it back to the file. (You really should be using a DB for this, though).
When you include the file, you can either use it like this:
include('config.php');
echo $config['variable']
// or
extract($config);
echo $variable;
Note: If you extract, it will overwrite any variables by the same name you might have defined before extracting.
PS - To make it easier to read and write to and from a file, I would just use json encoding to serialize the array.
Use a db
If your config is user defined - it would be better to store the config in a database. Otherwise you have this "novel" problem to solve but also potentially introduce security problems. I.e. for any one user to be able to edit your config files - they must be writeable to the webserver user. That opens the door to injecting malicious code into this file from a web exploit - or simply someone with direct access to your server (shared host?) finding this writeable file and updating it to their liking (e.g. putting "<?php header('Location: my site'); die;" in it).
One config variable
If you must manage it with a config file, include the file to read it, and var_export the variables to write it. That's easiest to do if there is only one config variable, that is an array. e.g.:
function writeConfig($config = array()) {
$arrayAsString = var_export($config, true);
$string = "<?php\n";
$string .= "\$config = $arrayAsString;\n";
file_put_contents('config.php', $string);
}
Allow partial updates
If you are changing only some variables - just include the config file before rewriting it:
function writeConfig($edits = array()) {
require 'config.php';
$edits += $config;
$arrayAsString = var_export($edits, true);
$string = "<?php\n";
$string .= "\$config = $arrayAsString;\n";
file_put_contents('config.php', $string);
}
Multiple config variables
If you have more than one variable in your config file, make use of get defined vars and loop on them to write the file back:
function writeConfig($_name = '', $_value) {
require 'config.php';
$$_name = $_value; // This is a variable variable
unset($_name, $_value);
$string = "<?php\n";
foreach(get_defined_vars() as $name => $value) {
$valueAsString = var_export($value, true);
$string .= "\$$name = $valueAsString;\n";
file_put_contents('config.php', $string);
}
}
The above code makes use of variable variables, to overwrite once of the variables in the config file. Each of these last two examples can easily be adapted to update multiple variables at the same time.

Categories