George Reith
George Reith

Reputation: 13476

Looping through post data and updating corresponding database records

I am trying to loop through some data I post through jQuery. Here is my PHP script:

<?php
include '../dbconnect.php';
$map = $_POST['map'];
$position = 0;
foreach ($map as $ID)
{
  if ($_POST['type'] == "sub") {
    $query = "UPDATE Subcategories SET `Position` = '$position' WHERE CategoryID = '$ID';";
  } else {
    $query = "UPDATE Categories SET `Position` = '$position' WHERE CategoryID = '$ID';";
  }

  $result = mysql_query($query) or die(mysql_error());
  $position ++;
}
?>

and the data it is recieving as $map is sent in this format:

ID[]=27&ID[]=28&ID[]=33&ID[]=19

Obviously my foreach is wrong, but how would I go about getting it so that I retain $maps order and each numerical value becomes the variable $ID?

Upvotes: 0

Views: 280

Answers (5)

George Reith
George Reith

Reputation: 13476

Thanks for trying guys, but in the end I couldn't get any suggestions to work but I did find a solution:

Update category position:

<?php
include '../dbconnect.php';
foreach ($_POST['ID'] as $position => $ID){
$query = "UPDATE Subcategories SET `Position` = '$position' WHERE CategoryID = '$ID';";
$result = mysql_query($query) or die(mysql_error());
}
?>

and the new Jquery post request courtesy of Starx (telling me to remove variable name), as a result I can access the query string as above:

$.ajax({
              type: 'POST',
              url: 'updatecategoryposition.php',
              data: map
            });

Upvotes: 0

Starx
Starx

Reputation: 79031

Ok, Since your $map contains, ID[]=27&ID[]=28&ID[]=33&ID[]=19 string value. Do something like this

$str = str_replace($map, "ID[]=","");
$mArr = explode("&", $str);
foreach($mArr as $key) {
   //now your $key will have 27, 28, 33, 19 as the loop progresses
}

P.S. Based on the OP's comment of $map having a string output of that

Upvotes: 2

pb149
pb149

Reputation: 2298

I'm not entirely sure, but is this what you meant? It works, if I understand your problem correctly.

<?php
include '../dbconnect.php';
$position = 0;

$pattern = '/ID\\[\\]=(\d+)&*/';
$subject = $_POST['map'];

 preg_match_all($pattern, $subject, $matches);

foreach ($matches as $k => $ID) {
  // etc

Test Case

Try this test, in a separate script. The output from $matches[1] are what you would feed to the foreach loop if you used this script. Does this retrieve what you want?

<?php
$pattern = '/ID\\[\\]=(\d+)&*/';
$subject = 'ID[]=27&ID[]=28&ID[]=33&ID[]=19';
preg_match_all($pattern, $subject, $matches);
echo '<pre>'.print_r($matches,1).'</pre>';
?>

Outputs:

Array
(
[0] => Array
    (
        [0] => ID[]=27&
        [1] => ID[]=28&
        [2] => ID[]=33&
        [3] => ID[]=19
    )

[1] => Array
    (
        [0] => 27
        [1] => 28
        [2] => 33
        [3] => 19
    )

)

Upvotes: 0

Darcara
Darcara

Reputation: 1618

You can POST it without the map, just

$.ajax({ type: 'POST', url: 'updatecategoryposition.php', data: map + "&type=sub" });

I'm assuming that map contains 'ID[]=27&ID[]=28&ID[]=33&ID[]=19'

As an alternative you can save the map in a different format so you end up with map being just a list of IDs ($map="27,28,33,19"), use the original jquery post and finally use

$map = explode(',' $_POST['map']);

Upvotes: 0

Alan Hollis
Alan Hollis

Reputation: 1322

Can you show us what print_r($map); displays.

If it's just a string in the format you've described, I'd suggest changing the format of map to something easier to loop through like json. For example

{"map":[27,28,33,19]}

Then you can use

<?php
$mapObject = json_decode($_POST['map']);

$mapArray = $mapObject->map;
foreach($mapArray as $id)
{
    echo $id;
}
?>

Upvotes: 0

Related Questions