Jump to content

[SOLVED] Sending different arrays to one foreach loop.


iwiniquit

Recommended Posts

I'm trying to update my database using an array and a foreach loop. It's basically just updating/resorting a list of numbers. The problem I'm having is that I'm trying to updating several lists using one javascript function and sending it to the same php file that has one foreach loop.

 

Here's the php I'm using

 

$project_id = $_POST['project_id'];
$i=1; 

foreach($_POST['client_list_1'] as $key=>$value) { 
mysql_query("UPDATE `projectClients` SET position='".$i."' WHERE project_id = $project_id AND client_id ='".$value."'\n"); 
$i++;
}

 

This works fine. The problem I'm having is that instead of client_list_1. I want to make it so I can change the 1 into the number that $project_id has. My logic would be to replace $_POST['client_list_1'] with $_POST['client_list_$project_id'] but that of course doesn't work. If that did work, it would be perfect. So that's what I'm trying to accomplish.

 

I'm not sure if this helps but the code that sends everything over to the php file is:

 

echo("
<script type=\"text/javascript\">
function updateOrder() {

           var project_id = $project_id;
        	
            var options = {
		method : 'post',

		parameters : Sortable.serialize('client_list_'+project_id)+'&project_id='+project_id,
		// Handle successful response
		onSuccess: function(t) {
		 alert(t.responseText);
		},
		// Handle 404
		on404: function(t) {
		 //alert('Error 404: location \"' + t.statusText + '\" was not found.');
		},
		// Handle other errors
		onFailure: function(t) {
		 //alert('Error ' + t.status + ' -- ' + t.statusText);
		}
		};

            new Ajax.Request('Includes/lib/clients/drag_update_client.php', options);
        }
Sortable.create(\"client_list_$project_id\", {constraint:\"vertical\", onUpdate : updateOrder});
</script>
");

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.