I hardly know any OOP PHP so I don't know how to properly pass variables.
I have a function that passes my results to a .php file that has the HTML table to show the results. I want to pass a variable "$config" to that file so I make a if/else statement.
Here is the function that passes my results to the file, that I need to pass the "$config" variable to create my if/else.
public function outputBrowser($results)
{
// $rows = array();
// print_r($results);
// die();
$config = $this ->config
$this->EE->load->add_package_path($this->report_path);
return $this->EE->load->view('output_browser', array("results" => $results), TRUE);
}
The main objective is to pass the selected drop down item to my file so I can output various HTML tables based on that selection. That selected value is in my config class or $config variable.
As my comment seemed to have fixed it:
Just add the $config variable to the array you pass:
array("results" => $results, "config" => $config)
Related
I'm working on a simple session manager for my framework. Im trying to setup a more user friendly structure for the session data. Essentially my sessions are stored like this:
$app_name = "Some_App_Name";
$component = "notifications";
$key = "errors";
$value = "There was some error";
$_SESSION[$app_name][$component][$key] = $value;
The problem I am facing is creating this structure through parameters within the session class. I have a set method which should ideally set a session value. The $app_name as listed above is by default added to the session class through the constructor, but I need to find a simple way of taking the parameters passed in within the method and then creating the rest. A simple example:
// Where keys could be: $key1 = notifications, $key2 => "notices"
public static function set($key1,$key2,$value) {
$_SESSION[self::$app_name][$key1][$key2] = $value;
}
The above would work if I always have 4 parameters but in some cases I might only have 2 parameters. I could pass 2 parameters (both being an array) but I'm looking for a more streamlined approach (if such an approach exists).
With the creating of the structure and setting values I also need a similiar way of verifying if the value or last key exists:
// Where keys could be: $key1 = notifications, $key2 => "errors"
public static function exists($key1,$key2) {
if(isset($_SESSION[self::$app_name][$key1][$key2])) {
return true;
}
Any suggestions would greatly be appreciated.
$params = array(
"key1" => "value1",
"key2" => "value2",
"value" => "value"
);
public static function set($params = NULL) //default null if no value is passed
{
if (!self::exists($params)) return false;
$_SESSION[self::$app_name][$params["key1"]][$params["key2"]] = $value;
return true;
}
public static function exists($params = NULL)
{
if(isset($_SESSION[self::$app_name][$params["key1"]][$params["key2"]]))
{
return true;
}
return false;
}
In the light of assisting other members wanting to do something similiar, I want to strongly advise you against using this concept as off the bat it sounds like a good idea but your true issue comes with the management of the array itself. Working straight with the $_SESSION superglobal really is the more powerful option on the basis that:
Even with a parameter in place for say server and component ($_SESSION ['somename']['auth']), what happens when you want to access content from that level from another instance of the object? Say I have another session object instance for $_SESSION ['somename']['errors'] but need to access properties from $_SESSION ['somename']['auth'] but within scope my base within the session array is incorrect.
Adding properties is fine $this->session->add("key","name") but what if you want to append to that array (where name is actually an array and not just a value), or vise versa. Or checking for occurances if $_SESSION['somename']['auth']['key']['name'] actually has another key or value within it?
All and all having worked with this the last couple of days I can definately say that it might not be impossible to write a "fully functional" session manager class but at the end of the day for simplicity it's better to rather just work with the session array directly as it's less code and less issues as you go.
In my controller I am retrieving records from my institutions table with the following fields
$params = array(
'fields' => array(
'Institution.id',
'Institution.name',
'Institution.about',
'Institution.picture'),
);
$institutions = $this->Institution->find('all',$params);
How can I prefix each 'Institution.picture' field with the full URL address, 'Institution.picture' itself only holds the name of the file.
I would also like to perform html_entity_decode() on each 'Institution.about' value from the returned set.
I know how to do this only without the framework if I make custom queries from scratch, then I would iterate each row and apply PHP functions to the field of interest. But is there a place in CakePHP (find or paginator) that I can specify such PHP manipulation on each field value from the returned set?
NOTE: I don't like to do this in the View, as I want to output it as json directly
You can define a virtualField for model:
public $virtualFields = array('image_url' => "CONCAT('/img/', Institution.picture)");
$params = array(
'fields' => array(
'Institution.id',
'Institution.name',
'Institution.about',
'Institution.picture',
'Institution.image_url'),
);
$institutions = $this->Institution->find('all',$params);
Unfortunaly MySQL doesn't have a function to decode HTML entities. You may utilize an afterFind() callback instead of virtualField. This lets you to decode entities as well as add a prefix.
CakePHP is php
Just iterate over the array and prepare it however you want:
$institutions = $this->Institution->find('all',$params);
$prefix = '/img/'; // <- define this
foreach ($institutions as &$row) {
$row['Institution']['about'] = html_entity_decode($row['Institution']['about']);
$row['Institution']['picture'] = $prefix . $row['Institution']['picture'];
}
If this is always required it can be applied to all finds via an afterFind method in the institution class.
I think you should do it in the View. See this example.
Hash::map can be very useful here. By specifying path you can only modify slices of the set.
Hey just wondering if there is a simpler way to declare an array inside a function call besides array()
$setup = new setupPage();
$setup->setup(array(
type => "static",
size => 350
));
class setupPage {
public function setup($config){
echo $config[size] . $config[type];
}
}
Thanks :D
If you use PHP 5.4+ you can use the shorthand, however it makes no difference in performance, but in actuality may make it harder to read:
$setup->setup(['type' => 'static',
'size' => 350]);
Create a PHP program with an array (student) with the following
categories: student_id, student_name, student_address,
student_state, student_zip, and student_age. A function within
the program will accept all the values and restrict the data type
passed for each. The function creates the array and place the
values into the array. Display the values in the array. Use try/catch
to display an error message if one or more of the values are not the
proper data type.
I know it is possible to use optional arguments as follows:
function doSomething($do, $something = "something") {
}
doSomething("do");
doSomething("do", "nothing");
But suppose you have the following situation:
function doSomething($do, $something = "something", $or = "or", $nothing = "nothing") {
}
doSomething("do", $or=>"and", $nothing=>"something");
So in the above line it would default $something to "something", even though I am setting values for everything else. I know this is possible in .net - I use it all the time. But I need to do this in PHP if possible.
Can anyone tell me if this is possible? I am altering the Omnistar Affiliate program which I have integrated into Interspire Shopping Cart - so I want to keep a function working as normal for any places where I dont change the call to the function, but in one place (which I am extending) I want to specify additional parameters. I dont want to create another function unless I absolutely have to.
No, in PHP that is not possible as of writing. Use array arguments:
function doSomething($arguments = array()) {
// set defaults
$arguments = array_merge(array(
"argument" => "default value",
), $arguments);
var_dump($arguments);
}
Example usage:
doSomething(); // with all defaults, or:
doSomething(array("argument" => "other value"));
When changing an existing method:
//function doSomething($bar, $baz) {
function doSomething($bar, $baz, $arguments = array()) {
// $bar and $baz remain in place, old code works
}
Have a look at func_get_args: http://au2.php.net/manual/en/function.func-get-args.php
Named arguments are not currently available in PHP (5.3).
To get around this, you commonly see a function receiving an argument array() and then using extract() to use the supplied arguments in local variables or array_merge() to default them.
Your original example would look something like:
$args = array('do' => 'do', 'or' => 'not', 'nothing' => 'something');
doSomething($args);
PHP has no named parameters. You'll have to decide on one workaround.
Most commonly an array parameter is used. But another clever method is using URL parameters, if you only need literal values:
function with_options($any) {
parse_str($any); // or extract() for array params
}
with_options("param=123&and=and&or=or");
Combine this approach with default parameters as it suits your particular use case.
I have a sequence of number like follows
1 -> 25,
2 -> 60,
3 -> 80,
4 -> 100
and so on
which means that if input is 1 output will be 25 and so on...I need to store it in global array.I would like to use it in multiple pages also.In codeigniter where i can declare a global array and store all these?
I am trying like as follows in constants.php
$CONFIDENCEVALUE = array();
$CONFIDENCEVALUE[] = array('1'=>25,'2'=>'60','3'=>80,'4'=>100);
If it is correct how can access these array value in required pages.Help me please.I am not an expert with codeignitor.
If I were you I'd look at adding a custom config file (see https://www.codeigniter.com/user_guide/libraries/config.html).
So in eg. application/config/confidencevalue.php add the following
$CONFIDENCEVALUE = array('1'=>25,'2'=>'60','3'=>80,'4'=>100);
$config['confidencevalue'] = $CONFIDENCEVALUE;
Add the config file to your application/config/autoload.php and you'll then be able to access your array through the config class using $this->config->item('1', 'confidencevalue'); (replacing the 1 for the value you're looking for).
Store the array in a session variable:
$this->session->set_userdata('cvarray', $CONFIDENCEVALUE);
To access the array later:
$this->session->userdata('cvarray');
CodeIgniter Session Class
One way of doing this is by adding a function to a helper file that you make available globally.
I have a helper file application/helpers/main_helper.php in which I load a number of generic, common functions which are used throughout my application.
If you add the following function to the main_helper file:
/*
|--------------------------------------------------------------------------
| Function to retrieve Static Variables used Globally
|--------------------------------------------------------------------------
*/
function get_var($var = 'CONFIDENCEVALUE', $KEY = NULL) {
$r = false;
switch ($var) {
case 'CONFIDENCEVALUE':
$r = array('1'=>25,'2'=>'60','3'=>80,'4'=>100);
if($KEY !== NULL) $r = $r[$KEY];
break;
}
return $r;
}
This file is auto-loaded by editing the file application/config/autoload.php and editing the line:
$autoload['helper'] = array('main_helper');
Whenever this array (or a value from the array) is needed, call the function instead. eg.:
$CONFIDENCE = get_var('CONFIDENCEVALUE', 2);
If you include the $KEY when calling get_var(), then only the value is returned, otherwise the whole array is returned.
To make additional variables available, just add them to the switch and call them as needed.
Feedback welcome :).