Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I would like to ask if there is any option how to change URL with Array in GET to CLEAN.

Example:

From this: example.com/school/4/?city[]=1&city[]=2&city[]=3
I need this: example.com/school/4/?city=1,2,3

(I use Codeigniter framework.)

I was thinking about mod_rewrite but I'm unable to do it.
I would really appreciate your help here.
Thank you.

// To explain it more: I need to change first mentioned url to second one.

share|improve this question

1 Answer

If you know it's going to come in in that format, you could just do:

$_GET['city'] = explode(",", $_GET['city']);

The only problem you will run into is when a value actually contains a comma.

If you need to get your form to actually submit it that way, it's a different problem. I'll assume you are using jQuery for simplicities sake, but you could do:

$('input[type="submit"]').on('click', function() {
   var cities = '';
   $('[name="city"]').each(function() {
        cities = cities + $(this).val() + ',';
   });
   cities = cities.substr(0,cities.length-1);
   $('[name="city"]').remove();
   $('<input name="city" value="' + cities + '">').appendTo('form');
});

This will remove all of your city fields when they click submit, and add a new city field with the values in a comma seperated list. You may need to attach it to the form submit action instead, but I'm not sure how jquery handles elements being added on the submit event.

share|improve this answer
 
wouldn't it be implode() if he's trying to go from an array (city[]=1&city[]=2&city[]=3) to a comma separated string (city=1,2,3)? –  user623952 yesterday
 
I was thinking he wanted them to send in a comma seperated string in the url, and now he needed to turn it into an array so he could use it in his app. I could be mistaken. –  dave yesterday
 
Is it possible to send it in this format (city=1,2,3) from HTML Form? –  John Valihora yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.