跳过json对象(PHP会话)

时间:2018-04-16 16:57:53

标签: javascript php json ajax

我有一组用户。可以跳过每个用户.click按钮。但是我希望它跳过登录的用户。会话是用PHP启动的,但我通过ajax和javascript向用户显示。但是

       if(sSession = sKey){
            aPeople.slice(this.jPerson);
        }

未正确跳过用户。你能帮忙吗?

以下是代码:

$("#btnSkip").click(function() {
$.getJSON('include/users.txt', function(aPeople){

        var getPerson = function(id) {
            var jPerson = aPeople[id]
            var sID = jPerson.id
            var sName = jPerson.name
            var sImage = jPerson.picture                
            var sSession = $('#mySession').text()
            var sKey = jPerson.key

            //this if statement doesn't work
            if(sSession == sKey){
                console.log(sSession)
                console.log(sKey)
                console.log(personIndex)
                console.log(jPerson)
                aPeople.splice(jPerson);
            }

            $('#sName').text(sName)
            $('#sImg').attr('src', sImage)

            //TO START COUNT AGAIN

            if(aPeople.length -1 == personIndex){
                personIndex = 0
            }else{
                personIndex = personIndex + 1
            }    
        }
            getPerson(personIndex);

            $( '#sName' ).fadeIn( 'slow' )
            $( '#sImg' ).fadeIn( 'slow' )
            })
        })

1 个答案:

答案 0 :(得分:1)

所以,我觉得你想要:

  1. 检索用户对象数组。
  2. 导航检索到的列表并按下按钮显示每个用户。
  3. 从不显示登录用户。
  4. 这是一个建议的解决方案:

    1. 我使用jQuery grep函数过滤掉登录用户。
    2. 我简化了你的导航逻辑。
    3. 我不确定你是想在每次点击时检索用户JSON,所以我也改变了。
    4. 
      
        function setupUserNavigator(users, loggedInUserKey) {
          var idx = 0;
      
          // filter out the logged in user
          var filtered = $.grep(users, function(user) {
            return !(user.key === loggedInUserKey);
          });
      
          function current() {
            return filtered[idx];
          }
      
          function next() {
            idx += 1;
          }
      
          function more() {
            return idx < filtered.length - 1;
          }
      
          return { current, next, more };
        }
      
        function displayUser(user) {
          $('#sName').text(user.name);
          $('#sImg').attr('src', user.picture);
        }
      
        function usersResponseHandler(users) {
          var loggedInUserKey = $('#mySession').text();
          var userNavigator = setupUserNavigator(users, loggedInUserKey);
      
          // display the first user immediately
          displayUser(userNavigator.current());
      
          // display each subsequent user on a 'next' button click
          $('#next').click(function() {
            userNavigator.next();
            displayUser(userNavigator.current());
      
            if(!userNavigator.more()) {
              $(this).prop('disabled', true);
            }
          });
        }
        
        // $.getJSON('include/users.txt', usersResponseHandler);
      
        // use test data for the snippet and mock response handler call
        var data = [
          { id: '1', key: '1234', name: 'Joe', picture: 'img/joe.png' },
          { id: '2', key: '5678', name: 'John', picture: 'img/john.png' },
          { id: '3', key: '9012', name: 'Sarah', picture: 'img/sarah.png' },
          { id: '4', key: '0987', name: 'Tim', picture: 'img/tim.png' },
          { id: '5', key: '6543', name: 'Lily', picture: 'img/lily.png' }
        ];
        
        usersResponseHandler(data);
        
        
      &#13;
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      
      Logged In User Key:
      <div id="mySession">9012</div><br />
      
      Name:
      <div id="sName"></div>
      Picture:
      <div><img id="sImg" src="" /></div><br />
      <button id="next">Next</button>
      &#13;
      &#13;
      &#13;