View Full Version : Problem iterating Array Properly.

03-07-2010, 10: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.


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

03-07-2010, 10: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! :)


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

and your code is open to SQL Injection.

03-07-2010, 11: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() {
handle : '.handle',
update : function () {
var order = $('#test-list').sortable('serialize');
var querystring = "?listItem=" + order;

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

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

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


should be sufficient therefore your querystring would be formulated as:

?nameOfElement=valueOfElement&nameOfElement2=valueOfElement2 etc etc

so atm your query string is:


then try

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

and then change your PHP ajax code to:


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

Might help you debug!