Page 1 of 1
$_GET mutliple of the same...SOLVED
Posted: Tue Jun 03, 2008 8:53 am
by psurrena
Here's an odd one, the below is a url from a javascript that shows which item is where in a sortable list:
Code: Select all
load.php?sort1=newsFeeder&sort1=news&sort2=shop&sort3=links&sort3=images
Using
I only get the last occurrence of sort1. So the question is, is there a way to get all values of sort1(2,3...).
Just incase this helps, here's the javascript function:
Code: Select all
function serialize(s){
serial=$.SortSerialize(s);
i=serial.hash;
p=i.replace(/[[]]/g,"");
window.open("load.php?value="+p);
};
Re: $_GET mutliple of the same...
Posted: Tue Jun 03, 2008 11:13 am
by Bruno De Barros
Change sort1=value to sort1[]=value.
Then, $_GET['sort1'] will be an array containing all sort1's.
Re: $_GET mutliple of the same...
Posted: Tue Jun 03, 2008 11:31 am
by psurrena
Thank you but it does not work
Code: Select all
$sort1[]=$_GET['sort1'];
print_r($sort1);
How does one implode an array in JS? Maybe I should do that in the JS function BUT, I'm just starting to figure out JS and not sure.
Re: $_GET mutliple of the same...
Posted: Tue Jun 03, 2008 11:45 am
by Bruno De Barros
Google - Man's Best Friend - Forget the dog.
Direct Link to your solution:
Javascript equivalent for PHP's implode
Google Search:
implode array js

Hope this helps.
Re: $_GET mutliple of the same...
Posted: Tue Jun 03, 2008 12:04 pm
by hansford
this doesn't make since to me when using $_GET
sort1=newsFeeder&sort1=news&sort2=shop&sort3=links&sort3=images
this string has sort1=something and then again sort1=somethingelse
the $_GET will only grab one of them because they have the same names. To get the keys and their values use: _REQUEST
foreach($_REQUEST as $key => $value){
echo $key . " " . $value . "<br>";
}
If you intend on having URL query strings with the same keys you'll have to process it in this sort of way:
---------------------
$str = 'sort1=newsFeeder&sort1=news&sort2=shop&sort3=links&sort3=images';
$strarray = explode("&",$str);
$newarray = array();
$count=0;
foreach($strarray as $val){
$valuearray = explode("=", $val);
$newarray[$count]["name"] = $valuearray[0];
$newarray[$count]["value"] = $valuearray[1];
$count++;
}
for($i = 0; $i < sizeof($newarray); $i++){
echo $newarray[$i]['name'] . " " . $newarray[$i]['value'] . "<br>";
}
Re: $_GET mutliple of the same...
Posted: Tue Jun 03, 2008 12:15 pm
by Luke
Re: $_GET mutliple of the same...
Posted: Tue Jun 03, 2008 12:19 pm
by Bruno De Barros
Hanford, I'd never had thought of that.
Nevertheless, I think it would be easier to just make PHP understand it as an array (sort1[]=something&sort1[]=something). It doesn't require such a big code change.
Anyway, Hanford, you could even go the extra mile there

PHP has, I think, $_SERVER['QUERY_STRING'] (if I'm not mistaken), so $str in your could could take its value directly from the query string

.
--
psurrena, your code:
Code: Select all
$sort1[]=$_GET['sort1'];
print_r($sort1);
could simply be:
$_GET['sort1'] is an array containing all occurrences of sort1 (edit: if you have your javascript generating the proper GET request).
Re: $_GET mutliple of the same...
Posted: Tue Jun 03, 2008 1:25 pm
by psurrena
if you have your javascript generating the proper GET request.
Could you explain? That must be the problem since no matter what I do it will only pull the last occurrence of a "sort".
The function:
[js]function serialize(s){ serial=$.SortSerialize(s); i=serial.hash; //
http://localhost/sort/load.php?sort1[]= ... sort3[]=19 p=i.replace(/[[]]/g,""); window.open("load.php?"+p);};[/js]
The link:
[js]<a href="#" onClick="serialize(['sort1','sort2','sort3']); return false;" >Save</a>[/js]
One more thing, I'm using the Interface plugin for jQuery, not jQuery UI.
Re: $_GET mutliple of the same...- SOLVED
Posted: Tue Jun 03, 2008 2:17 pm
by psurrena
Thanks everyone, especially
Bruno De Barros! The problem was, I was removing the brackets on the javascript side but they were necessary in the URL.
URL:
Code: Select all
http://localhost/sort/load.php?sort1[]=1&sort1[]=2&sort2[]=16&sort2[]=3&sort3[]=19&sort3[]=21&sort3[]=23
So....This will now allow me to save the position or elements in sortable lists. Each sort# array will hold the id of the people so they can be loaded back into the correct row.
This is the example it's based off of.
http://interface.eyecon.ro/demos/sort.html
Re: $_GET mutliple of the same...SOLVED
Posted: Tue Jun 03, 2008 3:11 pm
by Bruno De Barros
psurrena, I was going to mention that to you, but you posted before I did

. I'm glad I helped you

. I don't get to do that everyday xD.