last day (14 days later) » 

8:48 AM
0
A: creating dynamic string based on values submitted using php

George GarchagudashviliTry this: $string = ''; if ('POST' == $_SERVER['REQUEST_METHOD']) { $fields = array_filter($_POST); // filter array from empty values $string = http_build_query($fields); // build url query string same as your case } For manuals: array_filter http_build_query Update: $string = '';...

 
how to pass my own string like filed1,filed2 etc to the string
 
you have $_POST array, if you don't want to use it, pass your own array to array_filter, or you can add your own key/value pairs to $fields
But again, even in your edited example you show that filed1,filed2 comes from post by user, so it's already included in my answer
 
I am getting everything fromm the form like this _search=false&nd=1414130004257&rows=-1&page=1&sord=asc&clientUrlpass=bc&clients=‌​‌​api&location=9921&cmpt=true&datefirst=08%2F24%2F2014&datesecond=10%2F24%2F2014.‌​I need only follwoing url string clientUrlpass=bc&clients=api&location=9921&cmpt=true&datefirst=08%2F24%2F2014&da‌​‌​tesecond=10%2F24%2F2014 . How to remove unwanted parts
 
I've updated answer you can check it
 
I solved it another way.Please check my answer below
$arrayPost = array('clientId'=>$clients,
'locationIds'=> $location,
'personIds' =>$person,
'status'=>$status,
'competitor' =>$cmpt,
'startDate'=>$firstDate,
'endDate'=>$secondDate,
'sourceIds' => $source
);

$fields = array_filter($arrayPost); // filter array from empty values
$filterOutKeys = array('_search','nd','rows','page','sord');
$filteredArr = array_diff_key( $fields, array_flip( $filterOutKeys ) );
$stringToPass = http_build_query($filteredArr); // build url query string same as your case
 
8:51 AM
so you also have to do code for $clients, $location, $person, etc.. separately is it nice?
 
Not nice way but to get an answer I used this way
 
how you manage those variables? yes we can simplify the code
 
Why I have hardcoded because
I need to use specific names like in clientId etc in php side.Not able to give this name for textfileds in clientside due to some restrictions
I don't have acces to client page .I mean html page
I see,I can remove $filterOutKeys,$filteredArr
 
 
3 hours later…
11:57 AM
I mean this part
$arrayPost = array('clientId'=>$clients,
'locationIds'=> $location,
'personIds' =>$person,
'status'=>$status,
'competitor' =>$cmpt,
'startDate'=>$firstDate,
'endDate'=>$secondDate,
'sourceIds' => $source
);
for example: where is $location described and assigned value to it?
 
12:41 PM
yes,I have assigned value to it
sorry for late reply
 
1:07 PM
it's ok, but where this data comes from which you assign to that variables? are them from db or user input?
 
Its a user input
As I have mentioned I have no acces to htmlpage and I cannot give names to html fields
because I don't have html pages.I have only names and I need to submit the values to a webservice
My webservice expect different names
I cannot use html fields names
I hope you got the idea
 
Sure now I understand, So anyway your last solution works right?
Or need further help?
 
No thanks.It really helped me to solve the issue and was able to think properly
and How to code in a better way
 
Code improvements comes after practices, so don't worry much about it at this time, much you learn much more you can ;)) glad to help you thanks, your welcome anytime
 
Thanks,Have a nice weekend
 
1:23 PM
Thank you very much, you are nice ;) Wish all the best for you, take care
 

last day (14 days later) »