...

View Full Version : Problem iterating Array Properly.



macleodjb
03-07-2010, 09:22 PM
Hi guys,

I'm having a problem with an array. I am trying to implement the jquery sortable plugin. I downloaded the source code from here.

http://www.wil-linssen.com/musings/entry/extending-the-jquery-sortable-with-ajax-mysql/

The problem i am having is that when the array is in javascript it shows all rows in the array. For example if we have 4 total rows. On the javascript side it will show 4 rows and then when i use the ajax code shown below it only shows 3 rows. I am thinking it's a problem with the php, but i could be wrong. Any opinions.

Here's the php code.

foreach ($_GET['listItem'] as $position => $item){
$sql[] = "UPDATE `table` SET `position` = $position WHERE `id` = $item";
}

print_r ($sql);

Thanks in advance

JordanW
03-07-2010, 09:52 PM
Without information on how the Ajax is formulated in Javascript, it's hard to tell what's up:

Try print_r($_GET['listItem']) and print_r($_GET) to see what is inside those. If it only shows 3 options, then it's an issue with the Ajax/Javascript side of things... otherwise, post the results of the print_r's above, and see if that helps us to see what's going on! :)

~Jordan

Dormilich
03-07-2010, 09:54 PM
how do you pass those 4 values to the URL?

and your code is open to SQL Injection.

macleodjb
03-07-2010, 10:33 PM
This is the javascript that comes right out of the example.



<script type="text/javascript">
// When the document is ready set up our sortable with it's inherant function(s)
$(document).ready(function() {
$("#test-list").sortable({
handle : '.handle',
update : function () {
var order = $('#test-list').sortable('serialize');
alert(order);
var querystring = "?listItem=" + order;
$("#info").load("process-sortable.php"+querystring);
}
});
});
</script>


process-sortable.php only contains the code above in my previous post.

Dormilich
03-08-2010, 05:48 AM
what is the value of querystring?

JordanW
03-09-2010, 12:53 PM
First of all, I'm pretty sure Jquerys serialize function formulates the query string hence:



$("#info").load("process-sortable.php?"+order);


should be sufficient therefore your querystring would be formulated as:


?nameOfElement=valueOfElement&nameOfElement2=valueOfElement2 etc etc

so atm your query string is:


listItem=nameofElement=valueofElement&nameOfElement2=valueOfElement2

then try


$("#info").load("process-sortable.php"+querystring, function(response, status, xhr) {
alert(response);
}
});

and then change your PHP ajax code to:




print_r($_POST);




The ajax should then execute and on completion alert the contents of your post variable!

Might help you debug!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum