d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Javascript Help
Add Reply New Topic New Poll
Member
Posts: 13,893
Joined: Sep 10 2009
Gold: 13.37
Apr 22 2013 09:30pm
I have this Javascript:
Code
function buildChatLine(data) {
     //console.log(data);
     var $node = $('<tr class="tabelinhoud" id="'+data.id+'">' +
       '<td><i>'+(new Date()).dateFormat('l, d/m/Y (H:i)', 'en', data.msg_time)+'</i></td>' +
       '<td><a href="clientdetails.php?game=<?php echo $game; ?>'+
     '&id='+data.client_id+'"></a>&nbsp;(' + data.msg_type + '&nbsp;-&nbsp; Unknown)</td>' +
       '<td class="msg"></td>'+
       '</tr>');
       $node.find('a').text(data.client_name);
       $node.find('td.msg').text(data.msg);
       if ( data.msg.charAt(0)=='!' || data.msg.charAt(0)=='@' ) {
         $('td.msg', $node).css({'color':'#FF8000', 'font-weight':'bold'});
       }
       return $node;
   }

It just grabs info from the database as it gets inserted and updates the page in real time. Now, I have this PHP code:
Code
<?php if($row_rs_chats['client_team'] == 3){
     echo '<font color="red">Allies</font>';
    }else if($row_rs_chats['client_team'] == 2){
     echo '<font color="green">Axis</font>';
    }else if($row_rs_chats['client_team'] == -1){
     echo '<font color="grey">Spectator</font>';
    }else{
     echo 'Unknown';
    }
    ?>


It should be pretty self explanatory, but if you're lost it will just display their team name instead of the number. For the Javascript part I have it set to just display "Unknown". What would I use for the Javascript to do what the PHP part is doing? Instead of having it always say "Unknown" as it updates I want it to display the actual team name.

I've always been sketchy with Javascript and haven't taken the time to properly learn it.

Thanks in advance!

This post was edited by Fakie on Apr 22 2013 09:33pm
Member
Posts: 2,478
Joined: Jan 4 2007
Gold: 7,545.00
Apr 29 2013 12:10pm
You basically do exactly what the PHP is doing.

(Now I have to guess because I have no idea what the data structure is like)

Create some variable that will hold the result


Code
var temp = 'unknown';
   if (data.client_team== 3) temp = 'Allies';
   else if (data.client_team== 2) temp = 'Axis';
   else if (data.client_team== -1) temp = 'Spectator';



Then replace the "unknown" with

Code
' + temp  + '


This post was edited by DirtyRasa on Apr 29 2013 12:11pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll