0

helo i have tried hard in styling the json values that are printed randamoly in page ,here that are 4 categories,what i am doing with javascript is finding the lenght of the json array containing many objects ,then taking the type id ,as it is common in all array ,to determine whether they belong to 0,1,2,3 category ,then printing all the elements in the json array and so on ,here i would like to create table for each category and push all the corresponding id's to the table rows.

var txt = '<?php echo $response ?>';
//alert(txt);
//var jsonData = eval ("(" + txt + ")");
var jsonData = JSON.parse(txt);
for (var i = 0; i < jsonData.account_detail.length; i++) {
var counter = jsonData.account_detail[i];
//console.log(counter.counter_name);
//alert(counter.type);
document.write("<table border="0" width="500>">")
if(counter.type=="0")
    {

      document.write(counter.building_name);
      document.write(counter.org_name);
      document.write(counter.user_name);
      document.write(counter.name);
      document.write(counter.loc_name);
      document.write(counter.email_id);
      document.write(counter.password);

    }
if(counter.type=="1")
    {
        document.write(counter.user_name);
        document.write(counter.name);
        document.write(counter.password);
        document.write(counter.email_id);
    }
    if(counter.type=="2")
        {
            document.write(counter.building_name);
            document.write(counter.org_name);
            document.write(counter.user_name);
            document.write(counter.opr_code);
            document.write(counter.name);
            document.write(counter.loc_name);
            document.write(counter.email_id);
            document.write(counter.password);
        }
        if(counter.type=="3")
            {
               document.write(counter.building_name);
               document.write(counter.org_name);
               document.write(counter.machine_type);
               document.write(counter.activate_status);
               document.write(counter.machine_name);
               document.write(counter.entrance_exit_name);
               document.write(counter.entrance_or_exit);
               document.write(counter.loc_name);
               document.write(counter.activation_code);
            }
}
document.write("</table>")

1 Answers1

0

Try this:

 for (var i = 0; i < jsonData.account_detail.length; i++) {
var counter = jsonData.account_detail[i];
//console.log(counter.counter_name);
//alert(counter.type);
var tbl_str = '<table border="0" width="500><tr>';
    if(counter.type=="0")
    {
      tbl_str += "<td>"+counter.building_name+"</td>";
      tbl_str += "<td>"+counter.org_name+"</td>";
      tbl_str += "<td>"+counter.user_name+"</td>";
      tbl_str += "<td>"+counter.name+"</td>";
      tbl_str += "<td>"+counter.loc_name+"</td>";
      tbl_str += "<td>"+counter.email_id+"</td>";
      tbl_str += "<td>"+counter.password+"</td>";
    }
    else if(counter.type=="1")
    {
        tbl_str += "<td>"+counter.user_name+"</td>";
        tbl_str += "<td>"+counter.name+"</td>";
        tbl_str += "<td>"+counter.password+"</td>";
        tbl_str += "<td>"+counter.email_id+"</td>";
    }
    else if(counter.type=="2")
    {
            tbl_str += "<td>"+counter.building_name+"</td>";
            tbl_str += "<td>"+counter.org_name+"</td>";
            tbl_str += "<td>"+counter.user_name+"</td>";
            tbl_str += "<td>"+counter.opr_code+"</td>";
            tbl_str += "<td>"+counter.name+"</td>";
            tbl_str += "<td>"+counter.loc_name+"</td>";
            tbl_str += "<td>"+counter.email_id+"</td>";
            tbl_str += "<td>"+counter.password+"</td>";
     }
     else {
               tbl_str += "<td>"+counter.building_name+"</td>";
               tbl_str += "<td>"+counter.org_name+"</td>";
               tbl_str += "<td>"+counter.machine_type+"</td>";
               tbl_str += "<td>"+counter.activate_status+"</td>";
               tbl_str += "<td>"+counter.machine_name+"</td>";
               tbl_str += "<td>"+counter.entrance_exit_name+"</td>";
               tbl_str += "<td>"+counter.entrance_or_exit+"</td>";
               tbl_str += "<td>"+counter.loc_name+"</td>";
               tbl_str += "<td>"+counter.activation_code+"</td>";
            }    
tbl_str += "</tr></table>";    
document.write(tbl_str);    
}
Pankaj Dadure
  • 641
  • 5
  • 11