Jump to content

Array and Jquery AJAX


Recommended Posts

hi guys i have a problem and here it is i am makeing a website with draggable boxs lik this

$(function () {	$(".draggable").draggable({		containment: '#holder',		grid: [ 20, 20 ],		handle:'.box-header',		cursor: 'move',		stop : function(event,ui) {         		var dragposition = ui.position;         		var dragboxid = $(this).attr('id');			$.ajax({            			type: 'POST',            			dataType: 'json',            			url: My_Site+'/themes/mytheme-2/ajax.php',            			timeout: 5000,            			data: {            				task:"Moved",            				dragposition:dragposition,            				user_id:User_ID,            				dragboxid:dragboxid            			}        		});         	}    	});});

as you can see when you finish moving the box it will log the position with AJAX here is the "ajax.php" file

if($_POST['task'] == 'Moved'){$dragboxid 		= $_POST['dragboxid'];$dragposition_left	= $_POST['dragposition'][left];$dragposition_top	= $_POST['dragposition'][top];$uid			= $_POST['user_id'];$exist	= mysql_query("SELECT var1 FROM `jcow_mytheme` WHERE uid >= '$_POST[user_id]' LIMIT 1");if(mysql_num_rows($exist) == 0){		$var1[$dragboxid] = array("pos_left"=>$dragposition_left,"pos_top"=>$dragposition_top);		$var1e = serialize($var1);				mysql_query("INSERT INTO `".$table_prefix."mytheme` (uid, var1) VALUES('$uid', '$var1e' ) ");		}else{		$row = mysql_fetch_array($exist);		$var1d = unserialize($row['var1']);		$var1d[$dragboxid] = array("pos_left"=>$dragposition_left,"pos_top"=>$dragposition_top);		$var = serialize($var1d);					mysql_query("UPDATE `".$table_prefix."mytheme` SET var1='$var' WHERE uid='$_POST[user_id]'");	}}

this all works GREAT, but only once, the second time it wipes the array and starts again i will post some of the array to show my problemfirst time

array(1) {  ["notifications"]=>  array(2) {    ["pos_left"]=>    string(1) "0"    ["pos_top"]=>    string(2) "20"  }}

second time

array(2) {  ["notifications"]=>  array(2) {    ["pos_left"]=>    string(1) "0"    ["pos_top"]=>    string(2) "20"  }  ["friends"]=>  array(2) {    ["pos_left"]=>    string(1) "0"    ["pos_top"]=>    string(2) "20"  }}

Forth time

array(4) {  ["notifications"]=>  array(2) {    ["pos_left"]=>    string(1) "0"    ["pos_top"]=>    string(2) "20"  }  ["friends"]=>  array(2) {    ["pos_left"]=>    string(1) "0"    ["pos_top"]=>    string(2) "20"  }  ["messages"]=>  array(2) {    ["pos_left"]=>    string(1) "0"    ["pos_top"]=>    string(2) "20"  }  ["search"]=>  array(2) {    ["pos_left"]=>    string(2) "20"    ["pos_top"]=>    string(2) "20"  }}

and the wierd one Fifth one

array(1) {  ["new_member"]=>  array(2) {    ["pos_left"]=>    string(3) "-20"    ["pos_top"]=>    string(2) "20"  }}

HELP PLEASE lolthanks guys

Link to post
Share on other sites

I don't see anything in the code you posted that would explain that, although I'm curious why you're looking for records where the user ID is greater than or equal. Maybe some other code is clearing out or deleting the database record. I would set up an error log and write debugging information to it to figure out what's going on with the records in the database and what the queries are returning.

Link to post
Share on other sites

ok so i have tried debugging and all results work just fine untill i change 4 items then it restarts the array, So how about if i change it so onunload it saves positions off the divs?? would this be a better option?

Link to post
Share on other sites

WAIT A MIN... i dont understand arrays fully but it it right that...first

array([color=#ff0000]1[/color]) {  ["notifications"]=>  array([color=#ff0000]2[/color]) {	["pos_left"]=>	string(1) "0"	["pos_top"]=>	string(2) "20"  }}

and

array([color=#ff0000]4[/color]) {  ["notifications"]=>  array([color=#ff0000]2[/color]) {	["pos_left"]=>	string(1) "0"	["pos_top"]=>	string(2) "20"  }  ["friends"]=>  array([color=#ff0000]2[/color]) {	["pos_left"]=>	string(1) "0"	["pos_top"]=>	string(2) "20"  }  ["messages"]=>  array([color=#ff0000]2[/color]) {	["pos_left"]=>	string(1) "0"	["pos_top"]=>	string(2) "20"  }  ["search"]=>  array([color=#ff0000]2[/color]) {	["pos_left"]=>	string(2) "20"	["pos_top"]=>	string(2) "20"  }}

the highlited RED numbers in the arrays is this correct??? [EDIT]--WHY color=#ff0000 INSTEAD OF RED??

Edited by Craig Hopson
Link to post
Share on other sites

The number in parentheses is the length of the array, the number of items that it contains. There's nothing in your code that resets the array once it reaches a certain length, at least not in the part of the code that you posted. The one thing that could be an issue is the greater than, it could initially select an array from a user with an ID greater than the given user, which might be blank, and end up updating the actual user's row with the array from the other user. That's the issue with using greater than in the select query. But the data in the database isn't going to change itself, something is changing it. If you have debugging code to print the serialized array each time before you update it, and also print the new serialized array before updating the database, and when you go to update it to add a fifth item the serialized array from the database is suddenly empty, something in your code changed that. The database isn't going to change itself, and I don't see in the code you posted where you would clear out an existing array from the database.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...