Shortening if else for multiple $_GET variables - php

I'm using Laravel for an API and trying to fetch multiple _GET variables a user would type.
The code looks like this when all filters are used:
public function MUIntervalAPICall(Request $dte)
{
$date = $dte->dte;
$element_language = $dte->language;
$element_customer = $dte->customer;
$element_contract = $dte->contract;
$element_subcontract = $dte->subcontract;
$element = $dte->element;
//all filters
if(isset($_GET['customer']) && isset($_GET['language']) && isset($_GET['contract']) && isset($_GET['subcontract']) && isset($_GET['element']))
{
$where = ['dte' => $date, 'element_language' => $element_language, 'element_customer' => $element_customer, 'element_contract' => $element_contract, 'element_subcontract' => $element_subcontract, 'element' => $element];
}
$mu_interval = MUInterval::select('element_customer', 'element_contract', 'element_subcontract', 'element_language', 'element_site', 'element', 'src_id', 'src_type_id', 'dte', 'intvl', 'val_src_id', 'exception_name', 'duration_seconds', 'duration_fte')
->where($where)
->get()->toArray();
function array_to_xml( $data, &$xml_data ) {
foreach( $data as $key => $value ) {
if( is_array($value) ) {
$key = 'Exception';
$subnode = $xml_data->addChild($key);
array_to_xml($value, $subnode);
} else {
$xml_data->addChild("$key",htmlspecialchars("$value"));
}
}
}
$xml_data = new SimpleXMLElement('<?xml version="1.0"?><muExceptions></muExceptions>');
array_to_xml($mu_interval,$xml_data);
$result = $xml_data->asXML();
return Response::make($result, '200')->header('Content-Type', 'text/xml');
}
Now a user would want to filter it out only with few variables in place. e.g. date and customer, or language and contract only.
I'm currently writing a long block of if-else statements to produce results (really long, to include all possibilities for six $_GET variables):
elseif(isset($_GET['subcontract']) && isset($_GET['element']))
{
$where = ['dte' => $date, 'element' => $element, 'element_subcontract' => $element_subcontract];
}
Is there a shorter method for such a long if-else statements?

Yes, you should learn about how Laravel validation and form request validation work:
https://laravel.com/docs/5.1/validation
https://laravel.com/docs/5.1/validation#form-request-validation
It's much shorter and more convenient way to do validation.

isset accepts multiple arguments at once:
if (isset($_GET['customer'], $_GET['language'], $_GET['contract'], ...))
You can use array intersections/diffs:
if (!array_key_diff(array_flip(['customer', 'language', ...]), $_GET))
If array_key_diff returns an empty array, all those keys exist in the $_GET array.

Write your conditions as below:-
if(isset($_GET['subcontract'],$_GET['element']) && !isset($_GET['customer'], $_GET['language'], $_GET['contract'])){
$where = ['dte' => $date, 'element' => $element, 'element_subcontract' => $element_subcontract];
}
elseif(isset($_GET['customer'], $_GET['language'], $_GET['contract'], $_GET['subcontract'],$_GET['element'])){
$where = ['dte' => $date, 'element_language' => $element_language, 'element_customer' => $element_customer, 'element_contract' => $element_contract, 'element_subcontract' => $element_subcontract, 'element' => $element];
}
Suggestion:- You should use Laravel's Validator Class for validations.
Refer below links.
http://www.sitepoint.com/data-validation-laravel-right-way/
https://laravel.com/docs/5.1/validation

Related

Removing an associated key/value pair from array (nested)

I have two function to add remove parameters to the query string. The "add_query_params" (thanks to this forum) is working nicely and I can now add multiple tags to the query string of the same type.
For example
http://example.com?tags[]=flowers&tags[]=shrubs&category[]=garden
As you can see, I can add multiple of the same parameters, I am also querying these nicely using queryfilters.
However my newest problem, is simply removing a single tag type without affecting the rest of the query string. I will then rebuild the query without the deleted tag.
Someone kindly yesterday helped me to to a point but this removes ALL tag key values, not just the specified tag.
So if I was to delete say $tags[]shrubs from the above URL it would actually delete BOTH tag[]shrubs AND $tags[]flowers.
This obviously isn't very intuitive for a filter system I am devising. What I would like to know is how to remove just the single key value pair and leave the other keys pairs intact.
Here is my helper function
//Accept a param array which passthrough through tag type eg category/tag and value
function remove_query_params(array $params = [])
{
//Set to array
$existingParams = [];
$existingParams = request()->query();
foreach($params as $key=>$value){
if (isset($existingParams[$value])) {
unset($existingParams[$value]);
}
}
$query = http_build_query($existingParams);
return url()->current() . '?' . $query;
}
//Need to return: user removes tag from filter in blade, URL recontructs without the passed through tag value
//Before
//http://example.com?tags[]=flowers&tags[]=shrubs&category[]=garden
//After
//http://example.com?tags[]=flowers&category[]=garden
This does not work, if I change $value to $key then it will will, but it will remove all keys of the same type, not the behaviour I would like.
I activate this behaviour via a call in the blade template, this forms a href
//Pass through parameter type and parameter value
{{remove_query_params(['category' => $category->id]) }}
Has anybody got any pointers as to where I go next?#
Thanks and fingers crossed I am not far off :)
Adam
I hope this solution will help you:
<?php
function remove_query_params(array $params = [])
{
//Set to array
$existingParams = [
'tags' => [
'aaaa',
'bbbb'
],
'category' => 'ccc'
];
// go trough all parameters
foreach ($existingParams as $key1 => $value1) {
// go to the parameters, which need to be deleted
foreach ($params as $key2 => $value2) {
// only if the keys equals, do something
if ($key1 === $key2) {
// if the param is an array
if (is_array($value1)) {
foreach ($value1 as $k => $v) {
// if the elements to delete are an array
if (is_array($value2)) {
foreach ($value2 as $b => $r) {
if ($v == $r) {
unset($existingParams[$key1][$k]);
}
}
} else {
if ($v == $value2) {
unset($existingParams[$key1][$k]);
}
}
}
} else {
if (isset($existingParams[$key2])) {
unset($existingParams[$key2]);
}
}
}
}
}
$query = http_build_query($existingParams);
return $query;
}
echo remove_query_params(['tags' => 'aaaa']);
echo "\n";
echo remove_query_params(['tags' => ['aaaa', 'bbbb']]);
echo "\n";
echo remove_query_params(['category' => 'ccc']);
echo "\n";
tags is not an associated array. It is just a list of strings. Also, look at the value of $existingParams = request()->query(); It is not the tags array. It is an object that contains it. That is why when you use $key it works but deletes everything because $key is tags. So, in your check $existingParams['tags'] should be checked for the shrubs value. in_array is what you are looking in this case.
Hope this will solve your problem.I just provided the core function to get the things done in a way
$query = "tags[]=flowers&tags[]=shrubs&category[]=garden";
echo (remove_query_params( [ 'tags' => 'shrubs' ], $query ));
function remove_query_params(array $params = [], $query )
{
parse_str( $query, $existingParams );
$existing_keys = array_keys( $existingParams);
foreach($params as $key=>$value){
if( in_array( $key, $existing_keys ) ){
foreach ($existingParams[$key] as $param_key => $param_value) {
if( $param_value == $value ){
unset( $existingParams[$key][$param_key] );
}
}
}
}
$query = http_build_query($existingParams);
return $query;
}

Avoid Nested Loop in PHP

I am writing a method which takes an array of $topicNames and an array of $app and concatenates each $app to $topicNames like the following
public function getNotificationTopicByAppNames(array $topicNames, array $apps)
{
$topics = [];
foreach ($topicNames as $topicName) {
foreach ($apps as $app) {
$topic = $app . '_' . $topicName;
$topics[] = $topic;
}
}
return $topics;
}
}
The input and result are like the following...
$topicNames = [
'one_noti',
'two_noti',
'three_noti'
];
$apps = [
'one_app',
'two_app'
];
// The return result of the method will be like the following
[
'one_app_one_noti',
'two_app_one_noti',
'one_app_two_noti',
'two_app_two_noti',
'one_app_three_noti',
'two_app_three_noti'
]
My question is instead of doing nested loops, is there any other way I can do? Why do I want to avoid nested loops? Because currently, I have $topic. Later, I might want to add languages, locations etc...
I know I can use map, reduce, array_walks, each those are basically going through one by one. Instead of that which another alternative way I can use? I am okay changing different data types instead of the array as well.
If you dont care about the order you can use this
function getNotificationTopicByAppNames(array $topicNames, array $apps)
{
$topics = [];
foreach($apps as $app){
$topics = array_merge($topics, preg_filter('/^/', $app.'_', $topicNames));
}
return $topics;
}
print_r(getNotificationTopicByAppNames($topicNames,$apps));
Output
Array
(
[0] => one_app_one_noti
[1] => one_app_two_noti
[2] => one_app_three_noti
[3] => two_app_one_noti
[4] => two_app_two_noti
[5] => two_app_three_noti
)
Sandbox
You can also switch loops and use the $ instead to postfix instead of prefix. Which turns out to be in the same order you had. I thought of prefixing as a way to remove the loop. Then i thought why not flip it.
function getNotificationTopicByAppNames(array $topicNames, array $apps)
{
$topics = [];
foreach($topicNames as $topic){
$topics = array_merge($topics, preg_filter('/$/', '_'.$topic, $apps));
}
return $topics;
}
print_r(getNotificationTopicByAppNames($topicNames,$apps));
Output
Array
(
[0] => one_app_one_noti
[1] => two_app_one_noti
[2] => one_app_two_noti
[3] => two_app_two_noti
[4] => one_app_three_noti
[5] => two_app_three_noti
)
Sandbox
The trick here is using preg_filter.
http://php.net/manual/en/function.preg-filter.php
preg_filter — Perform a regular expression search and replace
So we search with ^ start or $ end which doesn't capture anything to replace and then we just add on what we want. I've used this before when I wanted to prefix a whole array with something, etc.
I couldn't test it in a class, so I made it a regular function, so adjust as needed.
Cheers!
You can use :
<?php
public function mergeStacks(...$stacks)
{
$allStacks = call_user_func_array('array_merge', $stacks);
return $this->concatString($allStacks);
}
private function concatString(&$stack, $index = 0, &$result = [])
{
if(count($stack) == 0){
return '';
}
if($index == count($stack)){
return $result;
}
array_walk($stack, function($value, $key) use($index, &$result, $stack){
if($key > $index){
array_push($result, $stack[$index] . '_' . $value);
}
});
$index = $index + 1;
return $this->concatString($stack, $index, $result);
}
And then when you want to get the array, no matter if you have languages or topics etc, you can just do :
$this->mergeStacks($languages, $topics, $locations, .....);
Where $languages, $topics, $locations are simple arrays.
Instead of accepting only topics name parameter try something like this:
function getNotificationTopicByAppNames(array $apps, array ...$names)
{
$topics = [];
foreach ($names as $nameArray) {
foreach ($nameArray as $topicName) {
foreach ($apps as $app) {
$topic = $app . '_' . $topicName;
$topics[] = $topic;
}
}
}
return $topics;
}
$topicNames = [
'one_noti',
'two_noti',
'three_noti'
];
$languagesNames = [
'test_en',
'test_other',
'test_other2'
];
$apps = [
'one_app',
'two_app'
];
print_r(getNotificationTopicByAppNames($apps,$topicNames,$languagesNames));
you can pass any number of arrays to array.

Error "indirect modification of overloaded property has no effect " when attempting to update array

I'm attempting to update a nested array in php. However, my updates have no effect. Here's the relevant code:
foreach($form["fields"] as $field){
Populate Checkbox Fields
if($field['type'] == 'checkbox'){
$inputs = $field['inputs'];
$count = '0';
foreach($inputs as $input){
if(($user_meta[$input['id']] !== '') && (isset($user_meta[$input['id']]))){
$select = true;
}
else{
$select = false;
}
$field['choices'][$count] = array( 'text' => $field['choices'][$count]['text'], 'value' => $field['choices'][$count]['value'] , 'isSelected' => $select );
$count = $count + '1';
}
}
}
I've tried a few different workarounds after searching for this error, but none of them seem effective. I'm grateful for any help anyone can offer.
Just use a reference & for $field with your existing code:
foreach($form['fields'] as &$field){
Alternately, use the key and modify the main array:
foreach($form['fields'] as $key => $field){
// later in the code
$form['fields'][$key]['choices'][$count] = array( /* ... */ );

Using an array as function parameter php

I have an array that I would like to pass to a function as a parameter. These array values will be used to pull values out of another array and display them.
My Function:
function showTreadmills($listbrands) {
global $treadmills;
foreach( $treadmills as $brand=>&$features ) {
if ($brand == $listbrands) {
return '<p>'.$features["description"].'</p>';
}
}
}
Treadmills Array:
$treadmills = [
'bowflexseries3' => [
'description' => 'Bowflex Series 3',
'image' => '/images/bowflex-series-3-150x150.jpg',
'url' => '/treadmills/bowflex/series-3',
],
'solef85' => [
'description' => 'Sole F85',
'image' => '/images/sole-f85-150x150.jpg',
'url' => '/treadmills/sole/f-85',
],
'endurancet10hrc' => [
'description' => 'Endurance T10HRC',
'image' => '/images/endurance-t10hrc-150x150.jpg',
'url' => '/treadmills/endurance/t10hrc',
]
];
Values that I'm trying to pull out of array in my function:
<?php echo showTreadmills('bowflexseries3','solef85'); ?>
This only returns the first Description from the array, which is Bowflex Series 3. I'm trying to figure out how to get it to pull the description for bowflexseries3 and solef85. I'm sure it's a dumb oversight. Thanks in advance!
You're not passing an array to the function, you're passing two strings. You need to call array() to wrap an array around them:
echo showTreadmills(array('bowflexseries3','solef85'));
Then you need to change showTreadmills. You can't use == to compare a string to an array. It looks like you want to test whether the string is in the array, so it should be:
if (in_array($brand, $listbrands))
Or instead of looping through $treadmills and testing whether it's equal to one of $listbands, you could loop through $listbrands:
$result = '';
foreach ($listbrands as $brand) {
if (isset($treadmills[$brand])) {
$result .= '<p>'.$treadmills[$brand]["description"].'</p>';
}
}
return $result;
This is better, since it loops through the smaller array. And in_array() has to do a search, while accessing an associative array is just a hash lookup.
Notice that you need to concatenate the results into a string during the loop. If you use return in the loop, you'll only return the first brand found.
The problem is that you are returning on your first match:
if ($brand == $listbrands) {
return '<p>'.$features["description"].'</p>';
}
You will need to store all your matches so the entire loop can finish and then send back everything that matched.
$matches = '';
foreach( $treadmills as $brand=>&$features ) {
if ($brand == $listbrands) {
$matches .= '<p>'.$features["description"].'</p>';
}
}
return $matches;
function showTreadmills($listbrands) {
global $treadmills;
$out = '';
foreach( $treadmills as $brand=>&$features ) {
if ($brand == $listbrands) {
$out .= '<p>'.$features["description"].'</p>';
}
}
return $out;
}

Check is a string element of CodeIgniter query

Hello I wanna check if is string element of codeIgniter query, so I wanna compere to arrays.
I use this soulution but i get false in both case.
$data = array(
'Firstname' => $ime ,
'Lastname' => $prezime,
'Nick' => $username,
'EmailAddress' => $email,
'Uid' => $uid,
);
$rs = $this->db->query("Select Nick FROM cms_cart_customers");
$array = $rs->result_array();
if(!in_array($data['Nick'],$array))
{
$this->db->insert('cms_cart_customers', $data);
}
The result_array() function returns you a multi-dimensional array, even with a single column. You need to flatten the array in order to search the array linearly, try something like this:
$array = $rs->result_array();
$flattened = array();
foreach($array as $a) {
$flattened[] = $a['Nick'];
}
if(!in_array($data['Nick'],$flattened)) {
$this->db->insert('cms_cart_customers', $data);
}
Codeigniter query will return result in associative array and in_array() function will not going to do the trick.
Here is one way you can do this custom is_in_array function source
//Helper function
function is_in_array($array, $key, $key_value){
$within_array = false;
foreach( $array as $k=>$v ){
if( is_array($v) ){
$within_array = is_in_array($v, $key, $key_value);
if( $within_array == true ){
break;
}
} else {
if( $v == $key_value && $k == $key ){
$within_array = true;
break;
}
}
}
return $within_array;
}
$array = $rs->result_array();
if(!is_in_array($array, 'Nick', $data['Nick']))
{
$this->db->insert('cms_cart_customers', $data);
}
Other Method
If you are trying to avoid duplicate entry, you should use a Select query first to check that the 'Nick' = $username is already present in table, if not then Issue an insert
Example
$rs = $this->db->get_where('cms_cart_customers', array('Nick' => $username));
//After that just check the row count it should return 0
if($rs->num_rows() == 0) {
$this->db->insert('cms_cart_customers', $data);
}

Categories