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>
");

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.