skip to Main Content

I want to display fontawesome icon if the user level is equal to 1 or etc. How can i do that in this code. i got an error if i used if else statement inside. Thanks in advance!

 function show_product(){
        $.ajax({
            type  : 'ajax',
            url   : '<?php echo site_url('User/userData')?>',
            async : true,
            dataType : 'json',
            success : function(data){
                var html = '';
                var i;
                var q = '';
                for(i=0; i<data.length; i++){


                    html += '<tr>'+
                            '<td>'+data[i].create_at+'</td>'+
                            '<td>'+data[i].user_email+'</td>'+
                            '<td>'+data[i].user_name+'</td>'+
                            '<td>'+data[i].user_lastname+'</td>'+


                            '<td>'if (data[i].user_level == '1') {
                                ++
                            }'</td>'+



                            '<td>'+data[i].status+'</td>'+


                            '</tr>';
                }
                $('#show_data').html(html);
            }

        });
    }

2

Answers


  1. The problem is most likely because the code don’t know that you want run the if statement.

    '<td>'if (data[i].user_level == '1') as you can see there is nothing separating the string <td> and the statement if (data[i].user_level == '1')

    Try something like this:

    function show_product() {
      $.ajax({
        type: 'ajax',
        url: '<?php echo site_url('
        User / userData ')?>',
        async: true,
        dataType: 'json',
        success: function(data) {
          var html = '';
          var i;
          var q = '';
          for (i = 0; i < data.length; i++) {
            html += '<tr>' +
              '<td>' + data[i].create_at + '</td>' +
              '<td>' + data[i].user_email + '</td>' +
              '<td>' + data[i].user_name + '</td>' +
              '<td>' + data[i].user_lastname + '</td>' +
              '<td>';
    
            if (data[i].user_level == '1') {
              html += '<i class="fas fas-user"></i>'
            }
    
            html += '</td>' +
              '<td>' + data[i].status + '</td>' +
              '</tr>';
          }
          $('#show_data').html(html);
        }
      });
    }
    
    Login or Signup to reply.
  2. You can do using the ternary operator

    function show_product(){
        $.ajax({
            type  : 'ajax',
            url   : '<?php echo site_url('User/userData')?>',
            async : true,
            dataType : 'json',
            success : function(data){
                var html = '';
                var i;
                var q = '';
                for(i=0; i<data.length; i++){
    
    
                    html += '<tr>'+
                            '<td>'+data[i].create_at+'</td>'+
                            '<td>'+data[i].user_email+'</td>'+
                            '<td>'+data[i].user_name+'</td>'+
                            '<td>'+data[i].user_lastname+'</td>'+
    
    
                            '<td>'+ data[i].user_level==1?'<i class="fa"></i>':'<i class="fa"></i></td>'
                            +
                            '<td>'+data[i].status+'</td>'+
    
    
                            '</tr>';
                }
                $('#show_data').html(html);
            }
    
        });
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search