$_GET mutliple of the same...SOLVED

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

$_GET mutliple of the same...SOLVED

Post 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

Code: Select all

$sort1=$_GET['sort1'];
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);
};
Last edited by psurrena on Tue Jun 03, 2008 2:27 pm, edited 1 time in total.
Bruno De Barros
Forum Commoner
Posts: 82
Joined: Mon May 12, 2008 8:41 am
Location: Ireland

Re: $_GET mutliple of the same...

Post by Bruno De Barros »

Change sort1=value to sort1[]=value.

Then, $_GET['sort1'] will be an array containing all sort1's.
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: $_GET mutliple of the same...

Post 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.
Bruno De Barros
Forum Commoner
Posts: 82
Joined: Mon May 12, 2008 8:41 am
Location: Ireland

Re: $_GET mutliple of the same...

Post 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.
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: $_GET mutliple of the same...

Post 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>";
}
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: $_GET mutliple of the same...

Post by Luke »

He said use http://www.example.com?sort[]=foo&sort[]=bar

You forgot the brackets.
Bruno De Barros
Forum Commoner
Posts: 82
Joined: Mon May 12, 2008 8:41 am
Location: Ireland

Re: $_GET mutliple of the same...

Post 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 :P.

--

psurrena, your code:

Code: Select all

$sort1[]=$_GET['sort1'];
print_r($sort1);
could simply be:

Code: Select all

print_r($_GET['sort1']);
$_GET['sort1'] is an array containing all occurrences of sort1 (edit: if you have your javascript generating the proper GET request).
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: $_GET mutliple of the same...

Post 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.
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: $_GET mutliple of the same...- SOLVED

Post 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

Code: Select all

$sort1=$_GET['sort1'];
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
Last edited by psurrena on Tue Jun 03, 2008 3:22 pm, edited 1 time in total.
Bruno De Barros
Forum Commoner
Posts: 82
Joined: Mon May 12, 2008 8:41 am
Location: Ireland

Re: $_GET mutliple of the same...SOLVED

Post 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.
Post Reply