code igniter form_input - php

A couple questions here..
What is the following syntax?
What do all the pieces mean?
(( ! is_array($data)) ? $data : '')
How is it used in the function at the end?
function form_input($data = '', $value = '', $extra = '')
{
$defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
return "<input "._parse_form_attributes($data, $defaults).$extra." />";
}
Thankyou. Blake

Ok let me try as far as I understand you question.
What is the following syntax?
It is a function definition.
What do all the pieces mean?
This is the ternary operator. It means: If $data is not an array (!is_array($data)), return $data (? $data) otherwise return an empty string (: '')
It is a shorthand for if-else.
How is it used in the function at the end?
Not sure what you mean here. A function _parse_form_attributes($data, $defaults) is called witch seems to return a string.
If it in your question refers to $defaults, then it is just an array that gets build and that contains the following values:
Array (
'type' => 'text',
'name' => $data, // or empty string if $data is an array,
'value' => $value
);
It is used to build an input element, that will look like this:
<input type="text"
name="(value of $data or empty)"
value="(value of $value)"
(value of $extra)
/>

Ok this seems to generate the HTML for a form input with ome useful default values.... To answer your Qs in order:
It's a function that takes optional parameters. You could call eg form_input('SomeData', 'SomeValue');
It creates the HTML for the element and includes whatever information you give it, using defaults if no value is provided
A difficult one to answer... In short you're defining a new function. If you provide parameters, they will be used to create a form element. If you omit them, defaults will be used - eg type will default to text for output like <input type="text" [blah]>
I'm not quite sure what you mean here - Append as key-value pairs eg <input type="text" key="Value" [blah]> ? If this is the case, I would personally lose the $extra variable and have an array of key->value pairs instead. Failing that, you need to et $extra as appropriate eg key1="value1" key2="value2"

It's an inline expression of:
function form_input($data = '', $value = '', $extra = '')
{
if (is_string($data)) { // or better: is_scalar()
// leave as-is
}
else {
$data = '';
}
...
(Slightly variated.)
It basically ensures that the $data parameter is never an array. Maybe it was allowed to be an array in previous versions of the function, and this is safety code to ensure correct operation.
Note that type enforcement is another approach (often overkill, but sometimes advisable):
function xy($data, $value, $z) {
assert( ! is_array($data) );
assert( is_string($value) );

1) It returns an input field;
2) Pardon?
3) Function takes three values. $value is the default value of the input field (the one you find in the textbox); $data will be the name of the form if it isn't an array, otherwise the name will be blank; $extra is a string containing other stuff that will go in the form. Please refer to the function _parse_form_attributes;
4) link
By the way, I sincerely hope that _parse_form_attributes() does something amazing, since that looks like an incredibly verbose function and codeigniter has some handy built-in form generation functions.

Related

Handle GET parameters individually PHP

First, I'm still fairly new to PHP, so please forgive my lack of understanding. I'm working on a form that takes in options from multiple selects, and provides the user results based on these options. This form also stores these search queries so that they can be exported later on.
Initially, the form only had four parameters needing to be stored, but now I've added a fifth which is making things not so cooperative. I've searched, and tried multiple different things but I've hit a wall and am seeking assistance.
What is intended to happen is the user selects one, or all, of the available options. The first four selects are simple Yes/No questions with values of 1 for Yes, 0 for No. The fifth is a series of county names with the values being set to their id in the database. The county select options are populated dynamically via Smarty.
Here's the PHP used to store these values.
public function recordFilter($args)
{
$db = DBManager::getConnection();
$_POST['type'] = 'f'; // type of search, find providers
foreach($args as $key => $a)
{
if(isset($_GET['county']) && is_numeric($_GET['county'])
{
$_POST[$key] = $a ? (int)$_GET['county'] : 0; // store value of last parameter, or 0 if not set
}
$_POST[$key] = $a ? "y" : "n"; // store 'y' if $a is set, 'n' if not
var_dump($_POST[$key]);
}
parent::save();
}
Currently what is happening is I'm able to get all the values into this function, and iterate through them. But since I've introduced this fifth field (and through the different approaches I've tried to piece this together) is either my fifth parameter gets set to 'y', which won't store in the database as it's field is an int(2), or the set values of the first four parameters take on the value of the fifth parameter, and wind up having the id associated with the county in their fields.
What I'm looking to learn is what better approach is there to handle this type of problem? I thought perhaps a while loop would be appropriate to iterate through the first four parameters and handle the fifth after those are complete, but figuring out the syntax for that is a bit beyond me. I also tried a switch statement but that simply didn't work. Having the if statement seems to be the big wrench in the situation, as it throws the whole loop off if 'county' is set. Any assistance will be greatly appreciated.
Since writing code doesn't look properly on comment, I post my guess here. If you describe more of your code, such as what $args is, or how you handle the request, it'll help people to understand your problem.
Treat the last request separately
Since it's a $_GET request, you can't iterate it with $_POST
foreach ($args as $key => $a) {
$_POST[$key] = $a ? "y" : "n"; // store 'y' if $a is set, 'n' if not
}
if (isset($_GET['county']) && is_numeric($_GET['county']) {
$_POST['county'] = $a ? (int)$_GET['county'] : 0; // store value of last parameter, or 0 if not set
}
And the second, I think this is better approach
Don't rely on super global variable
Assign them to another variable and don't forget to refactor the code on parent::save() method
public function recordFilter($args)
{
$db = DBManager::getConnection();
$request = [];
foreach ($args as $key => $a) {
//Sounds like you forgot to check if the $_POST[$key] is set
$request[$key] = isset($_POST[$key], $a) ? "y" : "n";
}
//You don't have to assign a value to super global $_POST with key of type
$request['type'] = 'f';
//Or you may need if statement if this field doesn't a required field
$request['county'] = (int) $_GET['county'];
parent::save($request);
}
And somewhere in your parent class
protected function save(array $request)
{
//this is your bussiness
}
Since I'm just guessing on what $args is, this is better than the better one
Assign it to local variable with filter_input() and filter_input_array()
public function recordFilter($args)
{
$db = DBManager::getConnection();
//This array will be used for validating the input $_POST.
//First, I grab the key of $args then assign the value
//to $validate with the same key to return it as bool
$validate = [];
foreach(array_keys($args) as $key) {
$validate[$key] = FILTER_VALIDATE_BOOLEAN;
}
//Get the post request value
$request = filter_input_array(INPUT_POST, $validate);
//You don't have to assign a value to super global $_POST with key of type
$request['type'] = 'f';
//Receive 'county' only if it's int and greater than 1 and lower than 99
$options = ['options' => ['min_range' => 1, 'max_range' => 99]];
$request['county'] = filter_input(INPUT_GET, 'county', FILTER_VALIDATE_INT, $options);
parent::save($request);
}
Again, the parent::save() method needs to be refactored.

Is there a php function that mimics the " " behavior?

i'm trying to create a table view helper in Zend Framework , it takes an array of models and generate an html table that displays the model properties.
The user can add extra columns to display operations like update ,delete models ,.
so the user can add a string like that
$columnContent = '<a href=\'update/$item[id]\'>update</a>' ;
note that i use simple quotes to cache the string to be evaluated later
my problem is , is there a way to evaluate that string in a context , later on ?
so i need to mimic the " " behavior of strings in Php , thanks.
something like :
// in the context , where $item is a row of an array of models :
$myVar = evaluatemyString($columnContent);
EDIT :
i'm not looking for the eval function which doesnt work in my case , ( i think ).
EDIT 2 :
i need to put the result of the in a variable too.
The eval function in PHP
eval($columnContent);
Use "templates" (the quotes are intended) instead. Have a look at intl, especially messageformatter. Also there are the good old printf()-functions (inlcuding sprintf() and so on)
Here is a simple UTF-8 safe string interpolation example. The regular expression forces variables with the following rules:
Prefixed with #
object:property notation (denotes an associative array key "object" whose value is also an associative array)
In other words, instead of variables like: $item[id]
You will have variables like: #user:id
<?php
// update
//$template = 'update';
// update
$template = 'update';
// fixture data
$db[499] = array('user' => array('id' => 499));
$db[500] = array('user' => array('id' => 500));
// select record w/ ID = 500
$context = $db[500];
// string interpolation
$merged = preg_replace_callback('/#(?:(?P<object>[^:]+):)(?P<property>[\w][\w\d]*){1}/iu', function($matches) use($context) {
$object = $matches['object'];
$property = $matches['property'];
return isset($context[$object][$property])
? $context[$object][$property]
: $matches[0];
}, $template);
echo "TEMPLATE: ${template}", PHP_EOL;
echo "MERGED : ${merged}", PHP_EOL;

php: concatenate str var and str literal as key for a POST value

Having trouble with this. I want to reference the post superglobal using a key which is generated by the concatenation of a string literal and a string variable.
$guests = array();
$guest_data = $this->compile_guests_for_hcp('g1');
array_push($guests, $guest_data($hcp_id));
function compile_guests_for_hcp($guest_identfier){
return function($hcp_id) use ($guest_identfier){
$guest = array(
'fname' => $_POST["fname_$guest_identifier"],
'lname' => $_POST["lname_$guest_identifier"],
'city' => $_POST["city_$guest_identifier"],
'state' => $_POST["state_$guest_identifier"],
'degree' => $_POST["degree_$guest_identifier"],
'zip' => $_POST["zip_$guest_identifier"],
'specialty' => $_POST["specialty_$guest_identifier"],
'email' => $_POST["email_$guest_identifier"],
'phone' => $_POST["pref_phone_$guest_identifier"],
'job_title' => $_POST["job_title_$guest_identifier"],
'office_addr_line_1' => $_POST["office_addr_line_1_$guest_identifier"],
'office_addr_line_2' => $_POST["office_addr_line_2_$guest_identifier"],
'hcp_data_idhcp_data' => $hcp_id
);
return $guest;
};
}
PHP is throwing undefined variable errors as it's trying to reference for example $_POST["fname_"] and $_POST["guest_identifier"]
I'm trying to access $_POST["fname_g1"] etc.
Second to testing if the variable is set using isset() before you try to access its value. You can even be more direct in your syntax and write the string concatenation in:
$var = isset( $_POST[ 'fname_' . $guest_identifier]) ?
$_POST[ 'fname_' . $guest_identifier] : 'default_value'
Your post makes it sound like PHP is trying to access two separate variables, $_POST["fname_"] and $_POST["$guest_identifier"], which shouldn't be the case, especially with the above syntax.
Just use the operator like: $_POST["fname_".$guest_identifier]
I'm thinking that your approach is a bit overly complicated, but your issue is not in the array index concatenation: there are no syntax errors in your code as you've presented it.
For example, this works like a charm:
$a = array("foo"=>1);
$o = "o";
print $a["fo$o"]; // prints 1
Your issue is that the $guest_identifier value is not being set properly so you are looking for POST indexes which simply aren't there.
There are a few different ways around this. My first suggestion would be to simply verify that the ID is there to begin with before calling this function. If that is not an option, then I would probably create a helper:
function ifInPost($val, $default = NULL){
if( isset( $_POST[ $val ] ) ) return $_POST[ $val ];
return $default;
}
Then, you could simply replace the $_POST["fname_$guest_identifier"] with ifInPost("fname_$guest_identifier")
GOT IT!
(Facepalm)
Simple spelling error in my argument so it was not being passed. I had identfier. Missing the i. I should pay more attention to the browser spellcheck when posting code hahaha!
function compile_guests_for_hcp($guest_identfier){
return function($hcp_id) use ($guest_identfier){
This will help you in making dynamic insertion of n number of product.
$loopLength=$_POST['ProudctSize'];
for ($i=1; $i <=$loopLength; $i++)
{
$productid=$_POST['P'.$i];
$quatity=$_POST['Q'.$i];
$rate=$_POST['R'.$i];
$Insert=mysql_query("INSERT INTO saledisplay ( `saleid`, `productid`, `quantity`, `rate`)
VALUES('$oid','$productid','$quatity','$rate')");
}
Replace all calls to $_POST[... with #$_POST[... to repress the errors.
Or check the value before using it like isset($_POST[...]) ? $_POST[...] ...
Or suppress PHP warnings by calling error_reporting(E_ALL ^ E_NOTICE)

PHP: testing for existence of a cell in a multidimensional array

I have an array with numerous dimensions, and I want to test for the existence of a cell.
The below cascaded approach, will be for sure a safe way to do it:
if (array_key_exists($arr, 'dim1Key'))
if (array_key_exists($arr['dim1Key'], 'dim2Key'))
if (array_key_exists($arr['dim1Key']['dim2Key'], 'dim3Key'))
echo "cell exists";
But is there a simpler way?
I'll go into more details about this:
Can I perform this check in one single statement?
Do I have to use array_key_exist or can I use something like isset? When do I use each and why?
isset() is the cannonical method of testing, even for multidimensional arrays. Unless you need to know exactly which dimension is missing, then something like
isset($arr[1][2][3])
is perfectly acceptable, even if the [1] and [2] elements aren't there (3 can't exist unless 1 and 2 are there).
However, if you have
$arr['a'] = null;
then
isset($arr['a']); // false
array_key_exists('a', $arr); // true
comment followup:
Maybe this analogy will help. Think of a PHP variable (an actual variable, an array element, etc...) as a cardboard box:
isset() looks inside the box and figures out if the box's contents can be typecast to something that's "not null". It doesn't care if the box exists or not - it only cares about the box's contents. If the box doesn't exist, then it obviously can't contain anything.
array_key_exists() checks if the box itself exists or not. The contents of the box are irrelevant, it's checking for traces of cardboard.
I was having the same problem, except i needed it for some Drupal stuff. I also needed to check if objects contained items as well as arrays. Here's the code I made, its a recursive search that looks to see if objects contain the value as well as arrays. Thought someone might find it useful.
function recursiveIsset($variable, $checkArray, $i=0) {
$new_var = null;
if(is_array($variable) && array_key_exists($checkArray[$i], $variable))
$new_var = $variable[$checkArray[$i]];
else if(is_object($variable) && array_key_exists($checkArray[$i], $variable))
$new_var = $variable->$checkArray[$i];
if(!isset($new_var))
return false;
else if(count($checkArray) > $i + 1)
return recursiveIsset($new_var, $checkArray, $i+1);
else
return $new_var;
}
Use: For instance
recursiveIsset($variables, array('content', 'body', '#object', 'body', 'und'))
In my case in drupal this ment for me that the following variable existed
$variables['content']['body']['#object']->body['und']
due note that just because '#object' is called object does not mean that it is. My recursive search also would return true if this location existed
$variables->content->body['#object']->body['und']
For a fast one liner you can use has method from this array library:
Arr::has('dim1Key.dim2Key.dim3Key')
Big benefit is that you can use dot notation to specify array keys which makes things simpler and more elegant.
Also, this method will work as expected for null value because it internally uses array_key_exists.
If you want to check $arr['dim1Key']['dim2Key']['dim3Key'], to be safe you need to check if all arrays exist before dim3Key. Then you can use array_key_exists.
So yes, there is a simpler way using one single if statement like the following:
if (isset($arr['dim1Key']['dim2Key']) &&
array_key_exists('dim3Key', $arr['dim1Key']['dim2Key'])) ...
I prefer creating a helper function like the following:
function my_isset_multi( $arr,$keys ){
foreach( $keys as $key ){
if( !isset( $arr[$key] ) ){
return false;
}
$arr = $arr[$key];
}
return $arr;
}
Then in my code, I first check the array using the function above, and if it doesn't return false, it will return the array itself.
Imagine you have this kind of array:
$arr = array( 'sample-1' => 'value-1','sample-2' => 'value-2','sample-3' => 'value-3' );
You can write something like this:
$arr = my_isset_multi( $arr,array( 'sample-1','sample-2','sample-3' ) );
if( $arr ){
//You can use the variable $arr without problems
}
The function my_isset_multi will check for every level of the array, and if a key is not set, it will return false.

Does PHP have "named parameters" so that early arguments can be omitted and later arguments can be written? [duplicate]

This question already has answers here:
Does PHP allow named parameters so that optional arguments can be omitted from function calls?
(17 answers)
Closed 2 years ago.
In PHP you can call a function with no arguments passed in so long as the arguments have default values like this:
function test($t1 ='test1',$t2 ='test2',$t3 ='test3')
{
echo "$t1, $t2, $t3";
}
test();
However, let's just say I want the last one to be different but the first two parameters should use their default values. The only way I can think of is by doing this with no success:
test('test1','test2','hi i am different');
I tried this:
test(,,'hi i am different');
test(default,default,'hi i am different');
Is there clean, valid way to do this?
Use arrays :
function test($options = array()) {
$defaults = array(
't1' => 'test1',
't2' => 'test2',
't3' => 'test3',
);
$options = array_merge($defauts, $options);
extract($options);
echo "$t1, $t2, $t3";
}
Call your function this way :
test(array('t3' => 'hi, i am different'));
You can't do that using raw PHP. You can try something like:
function test($var1 = null, $var2 = null){
if($var1 == null) $var1 = 'default1';
if($var2 == null) $var2 = 'default2';
}
and then call your function, with null as the identifier of the default variable. You can also use an array with the default values, that will be easier with a bigger parameter list.
Even better is to try to avoid this all, and rethink your design a bit.
The parameters with default values have to be last, after the others, in PHP and all the others up to that point must be filled in when calling the function. I don't know of anyway to pass a value that triggers the default value.
What I normally do in those situations is to specify the parameters as an array instead. Have a look at the following example (untested):
<?php
test(array('t3' => 'something'));
function test($options = array())
{
$default_options = array('t1' => 'test1', 't2' => 'test2', 't3' => 'test3');
$options = array_merge($default_options, $options);
echo $options['t1'] . ', ' . $options['t2'] . ', ' . $options['t3'];
}
?>
you could define the function like:
function grafico($valores,$img_width=false,$img_height=false,$titulo="title"){
if ($img_width===false){$img_width=450;}
if ($img_height===false){$img_height=300;}
...
}
and call to it without the lasting params or
replacing one or several with "false":
grafico($values);
grafico($values,300);
grafico($values,false,400);
grafico($values,false,400,"titleeee");

Categories