I have a modules array for my software, and I need to know how I would add to the array through PHP without the user having to directly add it (i.e. automated). I can do this with a simple array that looks like this:
$array = array( 'key'=>'value' );
however, my array looks like this:
$modules = array('Forums'=>array('file'=>)...
So, how could I add values to the array with a PHP function where say, a user clicks a button to add a new module, and all it asks for is the name of the module and the filename?
foreach($modules as $name => $module) if ($module['enabled']) {
require_once('include/scripts/'.$module['file']);
}
If the above were used to load the module, would #Darren's comment still apply?
What I commented is how to do it. I assume you're storing this array in a cache/session where it's semi-persistent right? What you want to do is append the item to the array. Say your array looks like this:
$modules = array(
'Forums' => array('file' => 'link/to/file.php', 'enabled' => TRUE),
.....etc
);
All you need to do is add it to the array:
$modules['Example_Module'] = array('file' => 'link/to/this/module', 'enabled' => TRUE);
Which will allow you to continue using that include code block you have.
See this: Example
It sticks to the structure you require.
Related
The code I had help with the last couple weeks works great. The problem is it's creating way more needed includes and external files than I first thought and getting to be a challenge to keep track of.
I was told to use MySQL. That would be fine if the data was going to be used over again. The data is only used long enough to build the pages, print to pdf and then it's no longer needed and the files are deleted.
I have three templates that are used to create all the needed pages. Only the data is different but never the same to allow it to be saved beyond it's use.
The problem I started having is when 30+ pages are loaded into a single browser window so it can get processed to pdf, this is calling a few hundred includes and some are being missed. When each page is called by itself it all loads fine.
The other thing I can think of is to try and get the variables belonging to each page in it's own single file and have the page access that file. When I call the file with include "file.php"; it just prints everything to the screen and not where they are needed. That way each file would have 10 - 15 variables in it for each page This would eliminate over 400 external files down to 1+ images for each page.
Is putting them all in one separate file and then called possible?
I hope I explained this correctly.
Thanks in advance.
// What I would like in one file.
$item1 = "Data for Item one";
$photo1 = "img src string to image for item 1";
etc...
$item12 = "Data for Item 12";
$photo12 = "img src string to image for item 12";
This would then call the items in the proper location of the page.
echo "$item1";
echo "photo1";
etc...
echo "$item12";
echo "photo12";
You can use a MySQL database to store information like that. (Settings, etc.)
But you can also use arrays for this which is probably more advisable.
Solution with arrays
An array gives you the possibility to easily store and manage data of a similar type.
You create a new array like this:
$settings = array();
To store a value in it, you have several options:
name it as an integer (0, 1, 2, 3 etc.)
name it as a string ('item1', 'path2' etc.)
$settings = array('path1', 'path2');
This just stored 0 => 'path1' and 1 => 'path1'
To get the value of a key in an array:
echo $settings[0]; //or $settings{0}, outputs 'path1'
echo $settings[1]; //outputs 'path2'
Or you store it as a string:
$settings = array('picture1' => 'path1', 'picture2' => 'path2');
echo $settings['picture1']; //outputs 'path1'
Also, multidimensional arrays are possible:
$settings = array(
'paths' => array(
'picture1' => 'path1',
'picture2' => 'path2'
),
'language' => 'english'
);
You get a value of a multidimensional array like this:
//for every dimension a new [], outputs 'path1'
echo $settings['paths']['picture1'];
Then you can just easily store all your settings and require_once 'settings.php';.
If you want to learn more about arrays, go to the php.net documentation.
Example:
/php/settings.php
$settings = array(
'items' => array(
'item1' => 'Data for Item1',
'item12' => 'Data for Item12',
),
'photos' => array(
'photo1' => 'img src string to image for item 1',
'photo12' => 'img src string to image for item 12',
),
);
index.php
<?php
require_once '/php/settings.php';
echo $settings['items']['item1']; //outputs 'Data for Item1'
//Or you can even use a foreach loop
foreach($settings['items'] as $key) {
echo $key;
echo '<br>';
}
That prints out:
Data for Item1
Data for Item12
Hope this helped.
Back in CodeIgniter, I could have set:
$config['media'] = '/media/';
$config['media_users'] = $config['media'] . 'users/';
But in Laravel4, the configuration (even the custom entries) is just one big array entries. Is there a way to concatenate configuration entires?
As far as I know laravel configuration files must return an array which will be used in configuration. There can be some logic code in there which produces wanted configuration.
Instead of following usual pattern:
<?php
return array(
'media' => '/media/',
'media_users' => '/media/users',
};
You can do this instead:
<?php
$mediaDir = '/media/';
return array(
'media' => $mediaDir,
'media_users' => $mediaDir.'users',
);
Laravel configuration files are simply PHP files returning arrays. You can do anything you wish inside it, as long as you return the array at the end. So, say you wanted to work with it like you did in CI, you could do it like this:
<?php
$app = array(
'media' => '/media/'
// ..
);
$app['media_users'] = $app['media'] . 'users/';
return $app;
PS: This seems ugly though.
This is just a curious question, the reasoning behind it is purely to be slightly more lazy on my part. Here is what I mean..
Say I have a website, where htaccess makes nice urls, and sends that data to the $_GET['p'] array key as the current 'page'. In the index file, I setup the page, and the first thing I do is setup some page settings in a config file, $_PAGE array. Now, say I have multiple pages I want to have the same settings, (and down in the page, other things may slightly change that do not correspond to the settings. So currently, I have something that looks like the following 2 php files.
// index.php
include('page.array.php');
echo '<title>'.$_PAGE[$_GET['p']]['title'].'</title>';
// page.array.php
$_PAGE = array(
'some/page/' => array(
'title' => 'This is an example'
)
)
$_PAGE['some/aliased/page/'] = $_PAGE['some/page/'];
Notice that at the end ofthe page array, in order to 'alias' a page I must add this to the end after the array has been created.
Is there any method in php that maybe I am just unaware of, that could make me a tad bit lazier (and at the same time add to cleaner code), and make it so I can simply alias the key? I notice the following doesn't work, and I suppose my question is, is there any way to create the alias within the same array during the creation of the array?
This example deosn't work:
// page.array.php
$_PAGE = array(
'some/page/' => array(
'title' => 'This is an example'
),
'some/aliased/page/' => $_PAGE['some/page/']
)
Maybe a way to refer to "this" array, from within itself?
If this is not possible, I don't have an issue with the "Not Possible" answer. Though if you have a better method of solving this, other then the way I have described above, in the sake of being lazier, I would be interested in reading it :)
I don't believe you can have array values that mirror other values in the array like this. The first thing that comes to mind though would be for you to construct your $_PAGE array from within a switch statement, using fall-through values as aliases:
// Define path for testing, and empty page array
$path = "some/aliased/page";
$page = Array();
// Time to evaluate our path
switch ($path) {
// If it's either of these two cases
case "some/page":
case "some/aliased/page":
// Assign this array to $page
$page = Array("Title" => "Two Paths, One Page.");
break;
// If it's this case
case "some/other/path":
// Assign this array to $page
$page = Array("Title" => "Something else.");
break;
// If the path isn't found, default data
default:
$page = Array("Title" => "Page not found");
}
// Output the result
var_dump($page);
Execute it: http://sandbox.onlinephpfunctions...ebd3dee1f37c5612c25
It's possible:
$_PAGE = array('some/page/' => array('title' => 'This is an example'));
$_PAGE['some/aliased/page/'] = &$_PAGE['some/page/'];
$_PAGE['some/page/'] = 7;
var_dump($_PAGE);
Use the & to get a reference to a (non-object) variable instead of its value.
All right so I have a problem and I'm looking for the best way to model it.
At the moment I have an array called $lang and it is defined in multiple files. It is initialized as so in each file: $lang = array_merge($lang, array( "Key" => "Value", )); so that when multiple files are included on a page, the $lang array contains all keys and values from their respective files into one big array.
Now I want to build a front-end where it displays all of the attributes from the array, a user can change the attributes, and save them. At the moment I am including them as so:
foreach(glob("language/*.php") as $filename){
include $filename;
}
I display them all fine, but when I want to re-submit them as a form, I don't know how to specify which Key => Value belonged to which file, as they were all merged when they were included.
Is there some clever way I can differentiate which file a certain Key => Value belonged to as I have set it up right now, or should I step back and set up the model differently?
sounds like you need to store the filename in each array using a multidimensional array, eg
array("filename"=>array("Key" => "Value")
Perhaps you could make some kind of language key to filename mapping:
$map = array();
foreach(glob("language/*.php") as $filename){
$lang = array();
include $filename;
foreach($lang as $k=>$v){
$map[$k] = $filename;
}
}
EDIT:
But it's probably a better idea to refactor your code and use some of the other answers suggestions.
your input fields could look something like this, with the filename in them:
<input type="text" name="data[file1][key1]" value="new value" />
<input type="text" name="data[file2][key1]" value="new value" />
That way you can differentiate them and write the files back in different files.
The two ways I can think of this are sorting by file:
array(
'filename' => array(
'key' => 'value',
)
)
or sorting by key:
array(
'key' => array(
'value',
'filename'
)
)
It really depends on how you want to deal with it later. I don't think there's a "correct" answer here.
The main problem I see with your code is that you hardencode the $lang variable plus some functional magic inside the data-file(s). Consider the following instead to differ more between data and logic:
language/sample.php:
return array("key" => "value");
loading script:
foreach(glob("language/*.php") as $filename){
$filedata = include($filename);
$lang[$filename] = $filedata;
# - OR -
$lang = array_merge($lang, $filedata);
}
You can now use the language data-files more modular because they are not bound to $lang any longer. For example to display an editor per file. Or to add the needed meta-data as well.
OK, so I have this external SOAP based webservice, and PHP SoapClient. Everything is fine with basic requests, but I need to create a parameter set that looks like this:
<DatasetList>
<DatasetID>K0001</DatasetID>
<DatasetID>K0002</DatasetID>
</DatasetList>
For a single nested DatasetID tag I'd do:
$req = array( "DatasetList" => array( "DatasetId" => "K0001" ));
$client->getWebserviceCall($req);
but I need multiple DatasetID tags... I've tried assigning DatasetID as an array, but I just get:
<DatasetList>
<DatasetID>Array</DatasetID>
</DatasetList>
Anyone help?
Did you try the array this way?
$req = array( "DatasetList" => array("DatasetID" => array("K0001", "K0002));
You can do this only by wrote the Part with the identical tags by hand. But, the rest of values can you define in a array:
// Define multiple identical Tags for a part of the Array
$soap_var= new SoapVar('
<DatasetID>1</DatasetID>
<DatasetID>2</DatasetID>
';
// Define the other Values in the normal Way as an array
$req = array(
"DatasetList" => $soap_var,
'value2'=>array('other'=>'values'
);