[Proto-Scripty] Re: Updating List - Issue with implementation

2009-02-23 Thread vanq69

Many thanks for your help, this is all working now.

On Feb 23, 7:55 pm, Walter Lee Davis  wrote:
> You know, I think this really should be
>
> + Sortable.serialize("chunksList",{name:'order'}) +
>
> Sortable.serialize already puts the key/value pair together for you.
>
> Walter
>
> On Feb 23, 2009, at 2:47 PM, Walter Lee Davis wrote:
>
> > "&order=" + Sortable.serialize("chunksList") +
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Updating List - Issue with implementation

2009-02-23 Thread Walter Lee Davis

You know, I think this really should be

+ Sortable.serialize("chunksList",{name:'order'}) +

Sortable.serialize already puts the key/value pair together for you.

Walter

On Feb 23, 2009, at 2:47 PM, Walter Lee Davis wrote:

> "&order=" + Sortable.serialize("chunksList") +


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Updating List - Issue with implementation

2009-02-23 Thread Walter Lee Davis

On Feb 23, 2009, at 1:12 PM, vanq69 wrote:

> "&order=" + {order:Sortable.serialize("chunksList")} +


Simplify!

"&order=" + Sortable.serialize("chunksList") +

That should fix that part. (You were mixing hash syntax with string  
syntax.)

You've also got a ton and a half of redundant code on that page.  
Prototype.js already gives you a fully-featured Ajax object. You've  
effectively re-written much of it in this snippet, and probably given  
yourself a fair amount of heartache in the process. Have a read  
through the docs at http://prototypejs.org/api/ajax

Your entire rest of your script could probably be refactored as  
something close to:

new Ajax.Updater('chunksList','yourserver.php',{});

And everywhere you've written out document.getElementById('myElement')  
you could just type $('myElement').

Walter


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Updating List - Issue with implementation

2009-02-23 Thread vanq69

Thanks yet again for you taking the time to assist me, this is proving
to be most
frustrating for me (and for yourself I imagine!) as I just cant seem
to get this working :(

Im going to (hopefully) lay out the code step by step, and perhaps if
you have the time then
you can tell me where im wrong - ill understand if you dont of course
and thanks for the time spent
helping so far.

Creation of sortable list:

function startup()
{
Sortable.create("chunksList", {});

Droppables.add("trash",
{
onDrop: function(element)
{
var deleteChunk = confirm("Are you sure you want to 
delete this
Chunk?")
if (deleteChunk)
{
Element.hide(element);
delElement = element.id;
process("delChunk");
}
}
});
}

--

Sending the server request:

function process(action)
{
var getdate = new Date();  //Used to prevent caching during ajax call
if (xmlHttp) // Checks xmlHttp is valid
{
params = "";

if (action == "recreateList")
{
params = "?chunkTitle=" + null + "&chunkContent=" + 
null +
"&order=" + {order:Sortable.serialize("chunksList")} +
"&action=recreateList";
}
else if (action == "addNewChunk")
{
var chunkTitle = 
document.getElementById("txtChunkTitle");
var chunkContent = 
document.getElementById("txtChunkContent");
if (chunkTitle.value && chunkContent.value) // makes 
sure Chunk
isnt null
params = "?chunkTitle=" + chunkTitle.value + 
"&chunkContent=" +
chunkContent.value + "&chunksList=" + null + "&action=addNewChunk";
}
else if (action =="delChunk")
{
params = "?chunkTitle=" + delElement + "&chunkContent=" 
+ null +
"&chunksList=" + null + "&action=delChunk";
}

if (params) cache.push(params); // checks params isnt null then 
adds
to cache for sending

try  // try connecting to server
{
// check cache is not empty and connection is available
if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 
0) &&
cache.length>0)
{
var cacheEntry = cache.shift();
// initiate the request
xmlHttp.open("GET", "dndChunkList.php" + 
cacheEntry, true);
xmlHttp.setRequestHeader("Content-Type", 
"application/x-www-form-
urlencoded");
xmlHttp.onreadystatechange = executeStateChange;
xmlHttp.send(null);
}
else
{
setTimeout("process();", 1000);
}
}
catch (e)
{
displayError(e.toString());
}
}
}

// function that retrieves the HTTP response
function executeStateChange()
{
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
chunkUpdate();
}
else
{
displayError(xmlHttp.statusText);
}
}
}

// Processes server's response
function chunkUpdate()
{
var response = xmlHttp.responseText;

document.getElementById("chunksList").innerHTML = response;
document.getElementById("txtChunkTitle").value = "";
document.getElementById("txtChunkContent").value = "";
}

--

dndChunkList.php

Process($chunkTitle, $chunkContent, $order,
$action);
?>

--

And finally the chunkslist.class.php

public function Process($chunkTitle, $chunkContent, $order, 
$action)
{
switch($action)
{   case 'recreateList':
$newOrder = $_GET['order'];
for ($i=0; $i < count($newOrder); $i

[Proto-Scripty] Re: Updating List - Issue with implementation

2009-02-23 Thread Walter Lee Davis

This was working previously because you were using a different data  
model for the return value of your serialise() method.  
Sortable.serialize() returns an array as a single value, and if you  
add that to the GET that is sent to your server, it will be  
interpreted by PHP on the receiving end as an Array(). Where you  
currently are using `$newOrder = explode('_', $order);`, simply do  
this $newOrder = $_GET['order']. If you inspect $newOrder on the  
server side, it will be:

Array(
0 => '2',
1 => '1',
2 => '3'
)

...assuming that you followed the ID rules noted previously, and that  
you appended the updated order using this sort of syntax:

new Ajax.Request('your_server.php,{parameters:  
{order:Sortable.serialize('your_list')}});

In testing this out, I realize that I made an error previously. I had  
said that you could use this syntax:

var foo = Sortable.create('some_list',{});

and later foo.serialize() would return the result. Not so (although it  
occurs to me that it might be easy to add).

Sortable.serialize('some_list') will actually return the array with  
the current list order.

Walter

On Feb 23, 2009, at 10:26 AM, vanq69 wrote:

> I have tried several different versions of this, the one you see is
> the one that worked previously before I added the other text, it is
> also very similar to the tutorial.
>
> Any further help would be greatly appreciated, I can email the files
> if its easier to see that way and you have the time.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Updating List - Issue with implementation

2009-02-23 Thread vanq69

Thanks again for your help.

I have spent the last hour reading through the documentation and
working through the example and
also your notes.  I believe I am getting closer, but I did not
understand how your:

//php
foreach($_POST['order'] as $k=>$v){
if($item = ActiveRecord::FindById('items',$v){
$v->position = $k+1;
$v->save();

would be implemented.  I have a code file which my AJAX requests are
passed through, which contains the code:

Process($chunkTitle, $chunkContent, $order,
$action);
?>

in this "$order = $_GET['order'] is the line i have added to handle
the following call:

if (action == "recreateList")
{
params = "?chunkTitle=" + null + "&chunkContent=" + 
null +
"&order=" + fooOrder + "&action=recreateList";
}

the other parts of the javascript file are as you recommended and I
think are working.

So it comes down to the chunkslist.class.php file, I have looked at
the database as I am trying different options and its not changing at
all, either good
or bad (ie corrupting) with the following code:

public function Process($chunkTitle, $chunkContent, $chunksList,
$action)
{
switch($action)
{

case 'recreateList':
$newOrder = explode('_', $order);
for ($i=0; $i < count($newOrder); $i++)
{
$newOrder[$i] = 
$this->mysqlConnection->real_escape_string
($newOrder[$i]);
$result = 
$this->mysqlConnection->query('UPDATE chunks SET
order_no="' . $i . '" WHERE id="' . $newOrder[$i] . '"');
}
$recreatedList = 
$this->BuildChunksList();
return $recreatedList;
break;

I have tried several different versions of this, the one you see is
the one that worked previously before I added the other text, it is
also very similar to the tutorial.

Any further help would be greatly appreciated, I can email the files
if its easier to see that way and you have the time.

Regards

On Feb 23, 1:55 pm, Walter Lee Davis  wrote:
> sorry, this part should have been
>
> var foo = Sortable.create('chunksList',{}) //no need to tell it  
> tag:li -- it's the default
>
> Walter
>
> On Feb 23, 2009, at 8:33 AM, Walter Lee Davis wrote:
>
> > var foo = Sortable.serialize('chunksList',{}) //no need to tell it
> > tag:li -- it's the default
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Updating List - Issue with implementation

2009-02-23 Thread Walter Lee Davis

sorry, this part should have been

var foo = Sortable.create('chunksList',{}) //no need to tell it   
tag:li -- it's the default

Walter

On Feb 23, 2009, at 8:33 AM, Walter Lee Davis wrote:

> var foo = Sortable.serialize('chunksList',{}) //no need to tell it
> tag:li -- it's the default


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Updating List - Issue with implementation

2009-02-23 Thread Walter Lee Davis

This part is duplicating built-in functionality. Read up on  
Sortable.serialize (with a z) in the Wiki. It's very powerful and  
configurable.

var foo = Sortable.serialize('chunksList',{}) //no need to tell it  
tag:li -- it's the default

...time passes...

var fooOrder = foo.serialize();

The result will be an array where the index equals the list position,  
and the value equals the numerical id of the element. Elements must be  
ID'd in the following manner:

someString_1
someString_2
someString_3

...

one underscore per ID, followed by a number -- which does not need to  
be incremental, that's just an artifact of my example (but it must be  
locally unique, naturally).

If you sorted this into 2,3,1 order, you would get back [2,3,1] from  
the serialize method, post that to your server as 'order', and on your  
server side, you would use something like this:

//php
foreach($_POST['order'] as $k=>$v){
if($item = ActiveRecord::FindById('items',$v){
$v->position = $k+1;
$v->save();
}
}

and you're done.

Walter

On Feb 23, 2009, at 8:12 AM, vanq69 wrote:

> function serialise(listID)
> {
>   var length = document.getElementById(listID).childNodes.length;
>   var serialised = "";
>   for (i = 0; i < length; i++)
>   {
>   var li = document.getElementById(listID).childNodes[i];
>   var id = li.getAttribute("id");
>   // add number to serialised array
>   serialised += encodeURIComponent(id) + "_";
>   }
>   // returns array (with last "_" removed
>   return serialised.substring(0, serialised.length - 1);
> }


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Updating List - Issue with implementation

2009-02-23 Thread vanq69

Thanks for the help firstly :)

It does clarify, I know that "chunksList" needs to go in serialise
(content) - I think, just not sure how to access that in the script.

Here is the parts of the code, its possible I have made an error:

from the script:

function startup()
{
Sortable.create("chunksList", {tag:"li"});
}

function serialise(listID)
{
var length = document.getElementById(listID).childNodes.length;
var serialised = "";
for (i = 0; i < length; i++)
{
var li = document.getElementById(listID).childNodes[i];
var id = li.getAttribute("id");
// add number to serialised array
serialised += encodeURIComponent(id) + "_";
}
// returns array (with last "_" removed
return serialised.substring(0, serialised.length - 1);
}

--- and also the original code fragment.

then from chunkslist.class.php - chunks are the items of data going
into the list

case 'recreateList':
$reOrder = explode('_', $content);
for ($i=0; $i < count($reOrder); $i++)
{
$new_order[$i] = 
$this->mysqlConnection->real_escape_string($reOrder
[$i]);
$result = $this->mysqlConnection->query('UPDATE chunks SET
order_no="' . $i . '" WHERE id="' . $reOrder[$i] . '"');
}
$recreatedList = $this->BuildChunksList();
return $recreatedList;
break;

Thanks again for taking the time

On Feb 23, 12:58 pm, Walter Lee Davis  wrote:
> content.serialize() could be re-written as "variable you set to the  
> return of Sortable.create".serialize();
>
> var foo = Sortable.create('somDomElement',{});
>
> ...
>
> var sortOrder = foo.serialize();
>
> Does that help?
>
> Walter
>
> On Feb 23, 2009, at 7:00 AM, vanq69 wrote:
>
>
>
> > ANY help would be greatly appreciated, I think the rest of my code for
> > dealing with placing it into the database etc is correct as it worked
> > before so im fairly confident its just this line.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Updating List - Issue with implementation

2009-02-23 Thread Walter Lee Davis

content.serialize() could be re-written as "variable you set to the  
return of Sortable.create".serialize();

var foo = Sortable.create('somDomElement',{});

...

var sortOrder = foo.serialize();

Does that help?

Walter


On Feb 23, 2009, at 7:00 AM, vanq69 wrote:

>
> ANY help would be greatly appreciated, I think the rest of my code for
> dealing with placing it into the database etc is correct as it worked
> before so im fairly confident its just this line.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---