0

I've been struggling with this for a while now and can't seem to get a solution for it.

I have a sortable list, through using JQuery-ui. Whenever the positions are changed, there are colors behind them that need to change to the same positions aswell.

So, whenever the list is changed, I do a AJAX request to a PHP file which should handle the sorting. All the data, on how the colors are supposed to be sorted, is working fine. However, I need to update this into a table inside my database.

When I debug, by echoing the data returned (console.log) from the AJAX request, I get the right way how they are supposed to be sorted. But, as soon as I add myqsli_query($the_query), it gives me back the same query twice. Which then ends up messing up the sorting and everything goes downhill from that part.

Piece of code that the AJAX request goes to:

        foreach ($status_colors as $values) {
            //rearranges the colors with the new positions
            for ($i = 0; $i < count($positions); $i++) {
                $new_array[$positions[$i]] = $values[$i];
            }
            //sorting by key starting with 0
            ksort($new_array);

            //since the colors are saved in the table like this;
            //red,green,yellow,purple,etc. I have to implode
            $new_status_color = implode(',', $new_array);
             //return the right order of colors.
            echo $new_status_color;

            //Setting up the query with the new status_color the id here is just a example, however its actually going through about 20 records to update
            $update_query = 'UPDATE cars SET status_color="'. $new_status_color .'" WHERE id=1';
            //If i echo out $update_query, sort the list a couple of times
            //I get the result that I want, but as soon as I use the following it breaks
            //This breaks it, on the first sort it goes fine. But when I sort again, I get the same query back, as the first one.
            mysqli_query($link, $update_query);
        }

A example on the problem: Lets say I have 5 items in a list, that is sortable.

  1. Item 1 - Green
  2. Item 2 - Yellow
  3. Item 3 - Red
  4. Item 4 - Blue
  5. Item 5 - Black

Lets say I change Item 5, to be above Item 1, which would create this list:

  1. Item 5 - Black
  2. Item 1 - Green
  3. Item 2 - Yellow
  4. Item 3 - Red
  5. Item 4 - Blue

Now with mysql_query this works fine for the first time, however when I change it back to the first list (item 1 - 5), it still returns the SQL from the previous list.

I hope that cleared it up a bit, if not you can ask and ill try to make it more understandable.

Also liked to add, I have tryed disabling caching, in PHP and aswell in the JQuery AJAX, this did not help either.

9
  • Where does $positions come from? You're using the same $positions array for each element of $status_colors.
    – Barmar
    Aug 26, 2012 at 5:35
  • This is just a part of the file, $positions is coming from the a table in the database. So is $status_colors. Then I reorder the $status_colors by the $postitions, so the right color is with the right position. Even though that doesn't seem te be the problem to me, since when print_r the $new_array I get the order as I expect it to be. But as soon as I use mysqli_query or even mysql_query it starts messing up by giving me the same query twice.
    – Ax_Npi
    Aug 26, 2012 at 17:13
  • Are you checking for errors, maybe the update isn't taking place for some reason?
    – Barmar
    Aug 26, 2012 at 17:23
  • Yes, I have checked for all errors. None of them seem to give me one, and they execute properly. Also thought it might be the cache, but that's not it either. Setting up the query, as in creating the SQL (before using the mysql(i)_query() function), it works fine. I get the expected results, but as soon as I use mysql(i)_query() it messes up and returns me the previous data.
    – Ax_Npi
    Aug 26, 2012 at 17:33
  • I have a strong feeling it's something silly like a typo. Since the code you posted looks like a simplified version, not the actual code, we're not going to be able to find it.
    – Barmar
    Aug 26, 2012 at 17:41

0

Your Answer

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

Browse other questions tagged or ask your own question.