Web Analytics Made Easy -
StatCounter Need help comparing ids within an array - CodingForum

Announcement

Collapse
No announcement yet.

Need help comparing ids within an array

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help comparing ids within an array

    I have a list of items with an id from 0-6 (7 total items). I want to turn these items into a drag and drop style game where students have to get them in the right order. If the list item id matches up to the array position then I want it to say "success" upon hitting the save button. This is what I have so far.


    index page
    Code:
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>jQuery UI Sortable - Default functionality</title>
      <link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
      <link rel="stylesheet" href="/resources/demos/style.css">
      <style>
      #sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
      #sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; }
      #sortable li span { position: absolute; margin-left: -1.3em; }
      </style>
      <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
      <script> $(function() {
       $( "#sortable" ).sortable();
    });
    
    function saveDisplayChanges()
    {
      var order =  $("ul#sortable").sortable("serialize");
    
    
     
    
    
      var a = "Correct";
      var b = "Try Again";
      $.post("update_displayorder.php?"+order,{Response1:a,Response2:b},function(theResponse){
      $("#categorysavemessage").html(theResponse);
    
    
    
    });
    }
    </script>
    
    <ul id="sortable">
    <li id="ID_4" class="ui-state-default">Figure out how you will get there</li>
    <li id="ID_5" class="ui-state-default">Go to the movies</li>
    <li id="ID_2" class="ui-state-default">Invite the friend to the movie</li>
    <li id="ID_6" class="ui-state-default">Eat some popcorn and enjoy the movie!</li>
    <li id="ID_3" class="ui-state-default">Find a time that works for you and your friend</li>
    <li id="ID_1" class="ui-state-default">Decide if you want to invite others</li>
    <li id="ID_0" class="ui-state-default">Decide on the movie you want to see. </li>
    
    </ul>
     <a href="javascript:void(0);" onClick="saveDisplayChanges();">Save</a>
     <span id="categorysavemessage"></span>

    update_displayorder.php page
    Code:
    <?php
    echo "<pre>";
    print_r($_REQUEST); 
    
    $newOrder = $_POST['id'];
    $counter = 1;
    foreach ($newOrder as $recordIDValue) {
       echo $counter .' '. $recordIDValue .'<br/>';
       $counter ++;
    echo $counter;
    }
    
    
    
    
    
    ?>
    Can I get some help in the comparison? I have tried several things with no luck.

  • #2
    Give your list elements a "value" attribute.

    In PHP, use an if statement that checks the array value vs the value that should be there,

    When you run it through a loop, check the inputs of the array the values are inserted into, against an array that is correct
    (Pseudo Code)
    Code:
    if(inputVal === correctVal)
      continue;
    else
      error;
      exit;
    That should be a start point for this. If you need more help, please let me know and we can see if we can figure this out.
    Riley
    Riley-Shannon.com - My Portfolio
    FraktalServices.com - My Company

    Comment


    • #3
      Originally posted by RShannon1321 View Post
      Give your list elements a "value" attribute.

      In PHP, use an if statement that checks the array value vs the value that should be there,

      When you run it through a loop, check the inputs of the array the values are inserted into, against an array that is correct
      (Pseudo Code)
      Code:
      if(inputVal === correctVal)
        continue;
      else
        error;
        exit;
      That should be a start point for this. If you need more help, please let me know and we can see if we can figure this out.


      I have tried

      Code:
      <?php
      echo "<pre>";
      print_r($_REQUEST); 
      
      $newOrder = $_POST['id'];
      $counter = 1;
      foreach ($newOrder as $recordIDValue) {
         echo $counter .' '. $recordIDValue .'<br/>';
         $counter ++;
      echo $counter;
      
      
      
      if ($newOrder == $order) {
          echo "Success";
      } else {
          echo "Try again";
      }
      
      
      }
      
      
      
      
      
      ?>

      Comment

      Working...
      X