0

Question: How can I UPDATE my MySQL-database with the new value from a JQuery sortable list?

This is my code this far:

<?php

echo '
<form action="" method="post"> 
<ul id="sortable">';

$array = str_split("ABCDEFGHI", 3);

for($i = 0; $i < count($array); $i++) {
  echo '
    <li class="ui-state-default">
    <span class="ui-icon ui-icon-arrowthick-2-n-s"></span>' . $array[$i] . '<input type="hidden" name="pos[]" value="'.$array[$i].'">
    </li>';
}

echo '
</ul><br>

<input type="submit" name="submit" value="Show order">
</form>

</body>
</html>';

if(isset($_POST['submit'])) {
    foreach($_POST['pos'] as $value){
        echo $value;
    }

}

?>

Information:

$value holds the new value from JQuery sortable list that I want to UPDATE my MySQL-database with. But can't be used outside foreach. So how can I do it? I know I can make a variable that holds the SQL-query (in foreach) like this:

$query ="UPDATE test SET order='" . $value . "' WHERE id='" . $random_function . "'";

But how do I use the variable $query to perform the actual SQL-query?

$_POST['pos'] array-structure:

Array ( [0] => ABC [1] => DEF [2] => GHI ) Array ( [0] => ABC [1] => DEF [2] => GHI ) Array ( [0] => ABC [1] => DEF [2] => GHI )
8
  • what is your $_POST['pos'] structure? Commented Mar 14, 2013 at 16:25
  • How do you mean? I think it creates an array with the value. Commented Mar 15, 2013 at 7:19
  • that i know it's an array. what is the array structure there? can you add that to the question. use print_r(). Commented Mar 15, 2013 at 7:40
  • Look at the question again. Is that what you're looking for? Commented Mar 15, 2013 at 7:55
  • why you are not using mysql_query inside the foreach loop Commented Mar 15, 2013 at 8:00

2 Answers 2

1

Try this .

$str = '';
if(isset($_POST['submit'])) {
  foreach($_POST['pos'] as $value){
    $str = $str.$value;
  }
 }

$query ="UPDATE test SET order='" . $str . "' WHERE id='" . $random_function . "'";
mysql_query($query);
Sign up to request clarification or add additional context in comments.

8 Comments

end($value) replace with end($array). Edited in the answer.
Still just update the database with the last JQuery sortable value. I don't get any error messages. I rearranged ABCDEFGHI to GHIDEFABC and it just updated with ABC in the database. But if I type echo $value; in the foreach loop it displays all values. :S
you need to update this in the database. GHIDEFABC or ABCDEFGHI. am i currect?
Is it so then instead $array[] = $value; use $str = $str.$value; and also replace end($array) with $str.
Yes! I've got a JQuery sortable list. Variable $value give me a value from the list (ABCDEFGHI). Like list1=ABC, list2=DEF and list3=GHI. With the JQuery sortable list I can rearrange this three values to whatever I like. I want to update the database with the new value in $value. Do you understand? :)
|
1

I'm doing it like this:

in JavaScript

var ids = [];
rows.each(function (index, value)
{
    ids.push($(value).attr("data-item-id"));
});
$.post('/url/to/save/order/, {orderMap:JSON.stringify(ids)}, function() {}, 'json');

in PHP:

$orderMap = json_decode(stripslashes($this->getPost("orderMap")));
$statements = array();
foreach ($orderMap as $itemId => $position) {
    $statements[] = sprintf("UPDATE `{$tableName}` SET `{$fieldName}`=%d WHERE `{$fieldIdName}`=%d", $position, $itemId);
}
$this->db->executeMultiQuery(implode(";", $statements));

2 Comments

Okey, thanks! :) But how do I use it in my code? How do I use the line $this->db->executeMultiQuery(implode(";", $statements)); in my code? :S Is there any way to just use a regular mysql_query() to insert the value?
I use class mysqli ($connection is new mysqli()): if ($connection->multi_query($query)) { $results = array(); $i = 0; do { if ($result = $connection->store_result()) { while ($row = $result->fetch_array(MYSQLI_ASSOC)) { $results[$i][] = $row; } $result->free(); } $i++; } while ($connection->next_result()); return $results; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.