Page 1 of 1

accordion ordering to database

Posted: Thu Dec 19, 2013 9:37 pm
by KillGorack
Greets,

I have on a site the jquery accordion object, and with it you can order it, bring that order over in a form submission, and update that order so that you can do an "ORDER BY" in your sql and see that new order. But for the life of me I cannot get this code to work perfectly all the time, it seems random. I just can't get my head around it..

Code: Select all

   // ==================================================================
   // Updating order...
   // ==================================================================
      if(isset($_POST['mainsubmit'])){
        if($_POST['order'] <> ""){
          $pieces = explode(',',$_POST['order']);
          foreach($pieces as $piece){
            $sql = "SELECT field_order, ID FROM fields Where field_app = '".$targetapp."' and field_order = '".$piece."' LIMIT 1";
            $result = mysql_query($sql, $link) or die(mysql_error());
            $row = mysql_fetch_assoc($result);
            $orderarray[] = array($row['field_order'], $piece, $row['ID']);
            mysql_close($result);
          }
          $cntr = 1;
          foreach($orderarray as $order){
            mysql_query("UPDATE fields SET field_order = ".$cntr." WHERE ID = '".$order[2]."'");
            $cntr = $cntr + 1;
          }
        }
        unset($orderarray);
      }
   // ==================================================================
For whatever reason , this however DOES work in ASP code, I tried to copy all functions as best I could..

Code: Select all

   ' ==================================================================
   ' Database work, field order...
   ' ==================================================================
      if Request.Form("mainsubmit") = "confirmed" Then
         if Request.Form("order") <> "" Then
            ' =========================================================
            ' Re-order fields, start with getting a record count...
            ' =========================================================
               SQL = "Select * From fields where fieldapp = '" & Targetapp & "'"
               Rs.Open SQL, Conn, 3, 3
               SortOrder = Rs.RecordCount
               Rs.Close
            ' =========================================================
             ' Build Array 
            ' =========================================================
               BreakItUp = Split(Request.Form("order"), ",")
               LinkNumber = 0
                 For X = 1 to SortOrder
                   SQL = "Select * From fields where fieldapp = '" & Targetapp & "' and fieldorder = " & BreakItUp(LinkNumber)
                   Rs.Open SQL, Conn, 3, 3
                     Redim Preserve SortFieldArray(2, LinkNumber)
                     SortFieldArray(0, LinkNumber) = Rs.Fields("fieldorder")
                     SortFieldArray(1, LinkNumber) = BreakItUp(LinkNumber)
                     SortFieldArray(2, LinkNumber) = Rs.Fields("ID")
                     LinkNumber = LinkNumber + 1
                   Rs.Close
                 Next
            ' =========================================================
             ' Update order 
            ' =========================================================
                For X = 0 to LinkNumber - 1
                  SQL = "Select * From fields Where ID = " & SortFieldArray(2, X)
                  Rs.Open SQL, Conn, 3, 3
                  Rs.Fields("fieldorder") = X + 1
                  Rs.UpDate
                  Rs.Close
                Next  
            ' =========================================================
            ' And always!
            ' =========================================================
               erase SortFieldArray
            ' =========================================================
         end if
      end if
   ' ==================================================================

Re: accordion ordering to database

Posted: Fri Dec 20, 2013 12:29 pm
by KillGorack
I found "an" answer after I slept.. usually makes things more clear. I placed the unique identifier in the form submission so the $_POST['order'] was = to something like "20,21,22,23,24,25" and used that as a key to create the array on, then update the order.. works pretty well now..

Code: Select all

   // ==================================================================
   // Updating order. as user specifies...
   // ==================================================================
      if(isset($_POST['mainsubmit'])){
        if($_POST['order'] <> ""){
          $pieces = explode(',',$_POST['order']);
          foreach($pieces as $piece){
            $sql = "SELECT field_order, ID FROM fields Where ID = '".$piece."' LIMIT 1";
            $result = mysql_query($sql, $link) or die(mysql_error());
            $row = mysql_fetch_assoc($result);
            $orderarray[] = array($row['field_order'], $piece, $row['ID']);
            mysql_close($result);
          }
          $cntr = 1;
          foreach($orderarray as $order){
            mysql_query("UPDATE fields SET field_order = ".$cntr." WHERE ID = '".$order[2]."'");
            $cntr = $cntr + 1;
          }
        }
        unset($orderarray);
      }
   // ==================================================================