donkey.ear-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2007-Mar-14 23:40 UTC
Adding an array to a hash
Hi, Been stuck on this for the past 6 hours, so hope you guys can help, I have an array of ''images'' I want to supply to the AJax.Request and Im building a hash so I can just convert the whole hash to a query string. Each image should have two keys, an id and a src so this is what I want to build. var h = $H({ somekey: 1 ''images[]'': [ { id: 0, src: ''http://'' }, { id: 1, src ''http://'' } ] }); However I need to build the pictures by grabbbing the element by classname, heres what I have currently var h = $H({ listingTitle: $F( ''listingTitle'' ), listingUserID: $F( ''listingUserID'' ), }); var pictures = $ ( ''picturesHolder'' ).getElementsByClassName( ''smallPicture'' ); var a = []; pictures.each( function(item) { var moo = $H( { uid: item.id, src: item.src } ) a.push( moo ); } ); My question is how do I get my array a into my hash? Ive tried h.merge( { ''userimages[]'': a } ); Which does not work. I need the [] in the keyname so that PHP understands its an array when the Request is posted. Am I going to have to do things the hard way and convert the hash to a string then run through the pictures concatenating the string? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
donkey.ear-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2007-Mar-14 23:56 UTC
Re: Adding an array to a hash
I had a little thought on the way home, I think it sets the object up okay, it could be the .toQueryString() thats making things go screwy. Any help still appreciated :) donkey.ear-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org wrote:> Hi, > > Been stuck on this for the past 6 hours, so hope you guys can help, I > have an array of ''images'' I want to supply to the AJax.Request and Im > building a hash so I can just convert the whole hash to a query > string. Each image should have two keys, an id and a src so this is > what I want to build. > > var h = $H({ > somekey: 1 > ''images[]'': [ { id: 0, src: ''http://'' }, { id: 1, src > ''http://'' } ] > }); > > However I need to build the pictures by grabbbing the element by > classname, heres what I have currently > > var h = $H({ > listingTitle: $F( ''listingTitle'' ), listingUserID: > $F( ''listingUserID'' ), > }); > > var pictures = $ > ( ''picturesHolder'' ).getElementsByClassName( ''smallPicture'' ); > var a = []; > pictures.each( function(item) { > var moo = $H( { uid: item.id, src: item.src } ) > a.push( moo ); > } ); > > My question is how do I get my array a into my hash? Ive tried > > h.merge( { ''userimages[]'': a } ); > > Which does not work. I need the [] in the keyname so that PHP > understands its an array when the Request is posted. Am I going to > have to do things the hard way and convert the hash to a string then > run through the pictures concatenating the string?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Let''s just take a step back and talk about what your HTML looks like and what the expected POST parameters should look like. Then we can write some JavaScript to solve that problem. Say your HTML looks like: <ul> <li><img src="http://www.domain.com/images/picture1.png" /></li> <li><img src="http://www.domain.com/images/picture2.png" /></li> <li><img src="http://www.domain.com/images/picture3.png" /></li> </ul> Would the following POST parameters work for you? picture[1]=http://www.domain.com/images/picture1.png&picture[2]=http://www.domain.com/images/picture2.png&picture[3]=http://www.domain.com/images/picture3.png I''m not sure how that would translate in PHP, but in Rails it would end up looking like this on the server: picture = { ''1'' => ''http://www.domain.com/images/picture1.png, ''2'' => ''http://www.domain.com/images/picture2.png'', ''3'' => ''http://www.domain.com/images/picture3.png'' If that looks OK to you so far, then the following JavaScript would do the job (there is probably a cleaner way to do this): var postData = ''''; $$(''ul li img'').each(function(image, index){ postData += ''picture['' + index + '']='' + escape(image.getAttribute(''src'')) + ''&''; }); Hope this helps. -justin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 15, 7:56 am, "donkey....-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org" <donkey....-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> I had a little thought on the way home, I think it sets the object up > okay, it could be the .toQueryString() thats making things go screwy.It looks like you need prototype v1.5.1_rc1 in order to get toQueryString() to generate arrays properly. You can download the newest version here: http://prototypejs.org/download --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
I''m not 100% sure if I understand what you are trying to do, but I think the problem is that A) toQueryString has a bug, B) toQueryString doesn''t recurse The bug is that it doesn''t add the [] after array names.. doh.. For now you''ll just have to build it manually (PITA) or submit a patch but good luck with the latter having any effect if you aren''t a member of the Prototype Core team. There is a discussion about this here: http://groups.google.com/group/prototype-core/browse_frm/thread/140fdf88eed057d and it appears toQueryString is soon to be rewritten so I wouldn''t bother with a patch, just chime in and let them know you''d like to see support for all standard platforms (e.g. fix lack of []) and you''d like to see full recursion or at least a few levels. Good luck, Colin donkey.ear-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org wrote:> Hi, > > Been stuck on this for the past 6 hours, so hope you guys can help, I > have an array of ''images'' I want to supply to the AJax.Request and Im > building a hash so I can just convert the whole hash to a query > string. Each image should have two keys, an id and a src so this is > what I want to build. > > var h = $H({ > somekey: 1 > ''images[]'': [ { id: 0, src: ''http://'' }, { id: 1, src > ''http://'' } ] > }); > > However I need to build the pictures by grabbbing the element by > classname, heres what I have currently > > var h = $H({ > listingTitle: $F( ''listingTitle'' ), listingUserID: > $F( ''listingUserID'' ), > }); > > var pictures = $ > ( ''picturesHolder'' ).getElementsByClassName( ''smallPicture'' ); > var a = []; > pictures.each( function(item) { > var moo = $H( { uid: item.id, src: item.src } ) > a.push( moo ); > } ); > > My question is how do I get my array a into my hash? Ive tried > > h.merge( { ''userimages[]'': a } ); > > Which does not work. I need the [] in the keyname so that PHP > understands its an array when the Request is posted. Am I going to > have to do things the hard way and convert the hash to a string then > run through the pictures concatenating the string? > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type"> </head> <body bgcolor="#ffffff" text="#000000"> Ok, I couldn''t stand it any longer :)<br> It really isn''t that hard to write a recursive function so I don''t know what all of the fuss is about.. Here is my rewrite (from scratch) of the Hash.toQueryString function.<br> <br> It will *properly* encode any conglomeration of hashes, arrays and other values you throw at it (as long as it can sensibly be converted to a string). It uses brackets and array indexes that will give you the closest translation to PHP variables as you can get, I don''t know about other server side languages as I don''t regularly use them. The $H().toQueryString method uses this function so there is no need to overwrite it, just past the code below into any js file that is included after prototype.<br> <br> Test it out here: <a class="moz-txt-link-freetext" href="http://colin.mollenhour.com/posttest.php">http://colin.mollenhour.com/posttest.php</a><br> <br> Colin<br> <br> --------------------------------------------------------<br> Hash.toQueryString = function(obj){<br> var pairs = $H(obj).findAll(function(pair){ return pair.value !=undefined; });<br> if(!pairs) return '''';<br> return pairs.collect(function(pair,index){<br> return Hash._toQueryString(pair.key, pair.value);<br> }).findAll(function(arg){ return !!arg; }).join(''&'');<br> };<br> Hash._toQueryString = function(key,value){<br> if(value === null) value = '''';<br> if(typeof value != ''object''){<br> return key+''=''+value.toString();<br> }<br> if(value.constructor == Array){<br> var items = value.findAll(function(arg){ return arg !=undefined; });<br> if(!items.length) return Hash._toQueryString(key, '''');<br> var result = items.collect(function(val,index){<br> return Hash._toQueryString(key+''[''+index+'']'',val);<br> });<br> }else{<br> var pairs = $H(value).findAll(function(pair){ return pair.value !== undefined; });<br> if(!pairs.length) return Hash._toQueryString(key, '''');<br> var result = pairs.collect(function(pair,index){<br> return Hash._toQueryString(key+''[''+pair.key+'']'',pair.value);<br> });<br> }<br> return result.join(''&'');<br> };<br> -----------------------------------------------------------<br> <br> Colin Mollenhour wrote: <blockquote cite="mid:45F8A8EB.3030005-NPSFNn/7+NYVo650/ln6uw@public.gmane.org" type="cite"> <pre wrap="">I''m not 100% sure if I understand what you are trying to do, but I think the problem is that A) toQueryString has a bug, B) toQueryString doesn''t recurse The bug is that it doesn''t add the [] after array names.. doh.. For now you''ll just have to build it manually (PITA) or submit a patch but good luck with the latter having any effect if you aren''t a member of the Prototype Core team. There is a discussion about this here: <a class="moz-txt-link-freetext" href="http://groups.google.com/group/prototype-core/browse_frm/thread/140fdf88eed057d">http://groups.google.com/group/prototype-core/browse_frm/thread/140fdf88eed057d</a> and it appears toQueryString is soon to be rewritten so I wouldn''t bother with a patch, just chime in and let them know you''d like to see support for all standard platforms (e.g. fix lack of []) and you''d like to see full recursion or at least a few levels. Good luck, Colin <a class="moz-txt-link-abbreviated" href="mailto:donkey.ear-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org">donkey.ear-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org</a> wrote: </pre> <blockquote type="cite"> <pre wrap="">Hi, Been stuck on this for the past 6 hours, so hope you guys can help, I have an array of ''images'' I want to supply to the AJax.Request and Im building a hash so I can just convert the whole hash to a query string. Each image should have two keys, an id and a src so this is what I want to build. var h = $H({ somekey: 1 ''images[]'': [ { id: 0, src: ''<a class="moz-txt-link-freetext" href="http://">http://</a>'' }, { id: 1, src ''<a class="moz-txt-link-freetext" href="http://">http://</a>'' } ] }); However I need to build the pictures by grabbbing the element by classname, heres what I have currently var h = $H({ listingTitle: $F( ''listingTitle'' ), listingUserID: $F( ''listingUserID'' ), }); var pictures = $ ( ''picturesHolder'' ).getElementsByClassName( ''smallPicture'' ); var a = []; pictures.each( function(item) { var moo = $H( { uid: item.id, src: item.src } ) a.push( moo ); } ); My question is how do I get my array a into my hash? Ive tried h.merge( { ''userimages[]'': a } ); Which does not work. I need the [] in the keyname so that PHP understands its an array when the Request is posted. Am I going to have to do things the hard way and convert the hash to a string then run through the pictures concatenating the string? </pre> <pre wrap=""> </pre> </blockquote> <pre wrap=""><!----> </pre> </blockquote> <br> --~--~---------~--~----~------------~-------~--~----~<br> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. <br> To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <br> To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <br> For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en <br> -~----------~----~----~----~------~----~------~--~---<br> </body> </html> <br>
donkey.ear-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2007-Mar-16 20:54 UTC
Re: Adding an array to a hash
I worked around this in the end, may not be the most elegant solution but it works. Created two arrays and pushed the url''s into one and the id''s into another, then merged these into the main hash. Thanks a lot for the help, anyway, could of done it so much easier if I was not using PHP but the project s tied to it. Oh well.... Cheers --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---