Jump to content

Build Table from Array of Data


BarryT06

Recommended Posts

Hi,

 

I am building a dashboard/report in work and I have it working at present (not fully) but I made the table structure static, in that if I wanted to make another one for another part of the business, I would need to redo the html table structure.

 

I want to have it create the table itself based on the information in the array.

 

I have included a sample of $data_array and also a screenshot of the current layout I have aswell as a sample of the HTML that is currently in use to generate the below table.

 

I understand its probably going to be a loop job, however Im getting confused as normally you loop for each row, but in this case I need to loop for each TD.

 

If there is any furthe info I can provide that would help, please let me know.

 

Apologies if I have not explained what I am looking for very well.

<table border="1" bordercolor="#000000" style="background-color:#FFFFFF" width="100%" cellpadding="3" cellspacing="0">
	<tr>
		<td colspan="3" rowspan="3" class="unused"></td>
		<td class = "center main_heading" colspan="33">UK Retail</td>
	</tr>
	<tr>
		
		<td class = "center channel_heading" colspan="<?php echo $chat_width;?>" ><a href="uk_retail_hh.php?c=Chat">Chat</a></td>
		<td class = "center channel_heading" colspan="<?php echo $phone_width;?>"><a href="uk_retail_hh.php?c=Phone">Phone</a></td>
		<td class = "center channel_heading" colspan="<?php echo $email_width;?>"><a href="uk_retail_hh.php?c=Email">Email</a></td>
	</tr>
	<tr>
		
		<td class = "center site_heading chat_ORK" >ORK</td>
		<td class = "center site_heading chat_EDI" >EDI</td>
		<td class = "center site_heading chat_VCC" >VCC</td>
		<td class = "center site_heading chat_HYD" >HYD</td>
		<td class = "center site_heading chat_BCD" >BCD</td>
		<td class = "center site_heading chat_SUT" >SUT</td>

		
		<td class = "center site_heading" >ORK</td>
		<td class = "center site_heading" >EDI</td>
		<td class = "center site_heading" >VCC</td>
		<td class = "center site_heading" >CBU</td>
		<td class = "center site_heading" >CPT</td>
		<td class = "center site_heading" >DAK</td>
		<td class = "center site_heading" >JAM</td>
		<td class = "center site_heading" >MNL</td>
		
		
		<td class = "center site_heading" >ORK</td>
		<td class = "center site_heading" >EDI</td>
		<td class = "center site_heading" >VCC</td>
		<td class = "center site_heading" >HYD</td>
		<td class = "center site_heading" >BCD</td>
		<td class = "center site_heading" >SUT</td>
		<td class = "center site_heading" >DAK</td>
	</tr>
	<tr>
		
		<td colspan="3">Offered</td>
		<td class = "center Chat_ORK"><?php echo $data_array['Chat']['ORK1']['Incoming'] ?></td>
		<td class = "center Chat_EDI"><?php echo $data_array['Chat']['EDI3']['Incoming'] ?></td>
		<td class = "center Chat_VCC"><?php echo $data_array['Chat']['UKVCC']['Incoming'] ?></td>
		<td class = "center Chat_HYD"><?php echo $data_array['Chat']['HYD']['Incoming'] ?></td>
		<td class = "center Chat_BCD"><?php echo $data_array['Chat']['BCD']['Incoming'] ?></td>
		<td class = "center Chat_SUT"><?php echo $data_array['Chat']['SUT']['Incoming'] ?></td>

		
		
		<td class = "center Phone_ORK"><?php echo $data_array['Phone']['ORK1']['Incoming'] ?></td>
		<td class = "center Phone_EDI"><?php echo $data_array['Phone']['EDI3']['Incoming'] ?></td>
		<td class = "center Phone_VCC"><?php echo $data_array['Phone']['UKVCC']['Incoming'] ?></td>
		<td class = "center Phone_CBU"><?php echo $data_array['Phone']['CBU1']['Incoming'] ?></td>
		<td class = "center Phone_CPT"><?php echo $data_array['Phone']['CPT']['Incoming'] ?></td>
		<td class = "center Phone_DAK"><?php echo $data_array['Phone']['DAK']['Incoming'] ?></td>
		<td class = "center Phone_JAM"><?php echo $data_array['Phone']['JAM']['Incoming'] ?></td>
		<td class = "center Phone_MNL"><?php echo $data_array['Phone']['MNL']['Incoming'] ?></td>
		
		<td class = "center Email_ORK"><?php echo $data_array['Email']['ORK1']['Incoming'] ?></td>
		<td class = "center Email_EDI"><?php echo $data_array['Email']['EDI3']['Incoming'] ?></td>
		<td class = "center Email_VCC"><?php echo $data_array['Email']['UKVCC']['Incoming'] ?></td>
		<td class = "center Email_HYD"><?php echo $data_array['Email']['HYD']['Incoming'] ?></td>
		<td class = "center Email_BCD"><?php echo $data_array['Email']['BCD']['Incoming'] ?></td>
		<td class = "center Email_SUT"><?php echo $data_array['Email']['SUT']['Incoming'] ?></td>
		<td class = "center Email_DAK"><?php echo $data_array['Email']['DAK']['Incoming'] ?></td>

	</tr>
	<tr>
		<td colspan="3">Handled</td>
		<td class = "center Chat_ORK"><?php echo $data_array['Chat']['ORK1']['Handled_incoming'] ?></td>
		<td class = "center Chat_EDI"><?php echo $data_array['Chat']['EDI3']['Handled_incoming'] ?></td>
		<td class = "center Chat_VCC"><?php echo $data_array['Chat']['UKVCC']['Handled_incoming'] ?></td>
		<td class = "center Chat_HYD"><?php echo $data_array['Chat']['HYD']['Handled_incoming'] ?></td>
		<td class = "center Chat_BCD"><?php echo $data_array['Chat']['BCD']['Handled_incoming'] ?></td>
		<td class = "center Chat_SUT"><?php echo $data_array['Chat']['SUT']['Handled_incoming'] ?></td>

		
		
		<td class = "center Phone_ORK"><?php echo $data_array['Phone']['ORK1']['Handled_incoming'] ?></td>
		<td class = "center Phone_EDI"><?php echo $data_array['Phone']['EDI3']['Handled_incoming'] ?></td>
		<td class = "center Phone_VCC"><?php echo $data_array['Phone']['UKVCC']['Handled_incoming'] ?></td>
		<td class = "center Phone_CBU"><?php echo $data_array['Phone']['CBU1']['Handled_incoming'] ?></td>
		<td class = "center Phone_CPT"><?php echo $data_array['Phone']['CPT']['Handled_incoming'] ?></td>
		<td class = "center Phone_DAK"><?php echo $data_array['Phone']['DAK']['Handled_incoming'] ?></td>
		<td class = "center Phone_JAM"><?php echo $data_array['Phone']['JAM']['Handled_incoming'] ?></td>
		<td class = "center Phone_MNL"><?php echo $data_array['Phone']['MNL']['Handled_incoming'] ?></td>
		
		
		<td class = "center Email_ORK"><?php echo $data_array['Email']['ORK1']['Handled_incoming'] ?></td>
		<td class = "center Email_EDI"><?php echo $data_array['Email']['EDI3']['Handled_incoming'] ?></td>
		<td class = "center Email_VCC"><?php echo $data_array['Email']['UKVCC']['Handled_incoming'] ?></td>
		<td class = "center Email_HYD"><?php echo $data_array['Email']['HYD']['Handled_incoming'] ?></td>
		<td class = "center Email_BCD"><?php echo $data_array['Email']['BCD']['Handled_incoming'] ?></td>
		<td class = "center Email_SUT"><?php echo $data_array['Email']['SUT']['Handled_incoming'] ?></td>
		<td class = "center Email_DAK"><?php echo $data_array['Email']['DAK']['Handled_incoming'] ?></td>

	</tr>

UK9KWG1.png


Array

(
    [Phone] => Array
        (
            [CBU1] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 0
                    [site] => CBU1
                    [phone_skill] => Primary
                    [OU] => UK
                    [Incoming] => 1005
                    [Handled_incoming] => 902
                    [Aband_6] => 39
                    [Aband_210] => 39
                    [Aband_30] => 32
                    [Ans_60sec] => 810
                    [Ans_210sec] => 966
                    [Ans_30sec] => 683
                    [SL_60] => 0.8448
                    [SL_210] => 1.0000
                    [SL_30] => 0.7114
                )
 
            [DAK] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 0
                    [site] => DAK
                    [phone_skill] => Primary
                    [OU] => UK
                    [Incoming] => 913
                    [Handled_incoming] => 876
                    [Aband_6] => 18
                    [Aband_210] => 18
                    [Aband_30] => 14
                    [Ans_60sec] => 807
                    [Ans_210sec] => 894
                    [Ans_30sec] => 730
                    [SL_60] => 0.9036
                    [SL_210] => 0.9989
                    [SL_30] => 0.8149
                )
 
            [JAM] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 0
                    [site] => JAM
                    [phone_skill] => Primary
                    [OU] => UK
                    [Incoming] => 1171
                    [Handled_incoming] => 1377
                    [Aband_6] => 15
                    [Aband_210] => 15
                    [Aband_30] => 12
                    [Ans_60sec] => 1147
                    [Ans_210sec] => 1155
                    [Ans_30sec] => 1123
                    [SL_60] => 0.9923
                    [SL_210] => 0.9991
                    [SL_30] => 0.9693
                )
 
            [MNL] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 530
                    [site] => MNL
                    [phone_skill] => Primary
                    [OU] => UK
                    [Incoming] => 1918
                    [Handled_incoming] => 1764
                    [Aband_6] => 24
                    [Aband_210] => 27
                    [Aband_30] => 16
                    [Ans_60sec] => 1711
                    [Ans_210sec] => 1891
                    [Ans_30sec] => 1459
                    [SL_60] => 0.9046
                    [SL_210] => 1.0000
                    [SL_30] => 0.7690
                )
 
            [ORK1] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 700
                    [site] => ORK1
                    [phone_skill] => Primary
                    [OU] => UK
                    [Incoming] => 133
                    [Handled_incoming] => 128
                    [Aband_6] => 3
                    [Aband_210] => 3
                    [Aband_30] => 2
                    [Ans_60sec] => 96
                    [Ans_210sec] => 130
                    [Ans_30sec] => 86
                    [SL_60] => 0.7444
                    [SL_210] => 1.0000
                    [SL_30] => 0.6617
                )
 
            [UKVCC] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 700
                    [site] => UKVCC
                    [phone_skill] => CsPromotions
                    [OU] => UK
                    [Incoming] => 1264
                    [Handled_incoming] => 1191
                    [Aband_6] => 28
                    [Aband_210] => 30
                    [Aband_30] => 27
                    [Ans_60sec] => 1133
                    [Ans_210sec] => 1234
                    [Ans_30sec] => 1047
                    [SL_60] => 0.9185
                    [SL_210] => 1.0000
                    [SL_30] => 0.8497
                )
 
            [CPT] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 800
                    [site] => CPT
                    [phone_skill] => Primary
                    [OU] => UK
                    [Incoming] => 624
                    [Handled_incoming] => 624
                    [Aband_6] => 16
                    [Aband_210] => 16
                    [Aband_30] => 13
                    [Ans_60sec] => 562
                    [Ans_210sec] => 608
                    [Ans_30sec] => 535
                    [SL_60] => 0.9263
                    [SL_210] => 1.0000
                    [SL_30] => 0.8782
                )
 
            [EDI3] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 900
                    [site] => EDI3
                    [phone_skill] => Primary
                    [OU] => UK
                    [Incoming] => 779
                    [Handled_incoming] => 775
                    [Aband_6] => 21
                    [Aband_210] => 22
                    [Aband_30] => 19
                    [Ans_60sec] => 724
                    [Ans_210sec] => 757
                    [Ans_30sec] => 708
                    [SL_60] => 0.9564
                    [SL_210] => 1.0000
                    [SL_30] => 0.9332
                )
 
        )
 
    [Chat] => Array
        (
            [BCD] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 0
                    [site] => BCD
                    [phone_skill] => PrimaryChat
                    [OU] => UK
                    [Incoming] => 809
                    [Handled_incoming] => 777
                    [Aband_6] => 10
                    [Aband_210] => 11
                    [Aband_30] => 8
                    [Ans_60sec] => 765
                    [Ans_210sec] => 798
                    [Ans_30sec] => 687
                    [SL_60] => 0.9580
                    [SL_210] => 1.0000
                    [SL_30] => 0.8591
                )
 
            [HYD] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 0
                    [site] => HYD
                    [phone_skill] => PrimaryChat
                    [OU] => UK
                    [Incoming] => 862
                    [Handled_incoming] => 954
                    [Aband_6] => 8
                    [Aband_210] => 10
                    [Aband_30] => 8
                    [Ans_60sec] => 818
                    [Ans_210sec] => 852
                    [Ans_30sec] => 765
                    [SL_60] => 0.9582
                    [SL_210] => 1.0000
                    [SL_30] => 0.8968
                )
 
            [ORK1] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 0
                    [site] => ORK1
                    [phone_skill] => PrimaryChat
                    [OU] => UK
                    [Incoming] => 314
                    [Handled_incoming] => 317
                    [Aband_6] => 3
                    [Aband_210] => 3
                    [Aband_30] => 3
                    [Ans_60sec] => 311
                    [Ans_210sec] => 311
                    [Ans_30sec] => 290
                    [SL_60] => 1.0000
                    [SL_210] => 1.0000
                    [SL_30] => 0.9331
                )
 
            [SUT] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 0
                    [site] => SUT
                    [phone_skill] => PrimaryChat
                    [OU] => UK
                    [Incoming] => 1369
                    [Handled_incoming] => 1234
                    [Aband_6] => 21
                    [Aband_210] => 22
                    [Aband_30] => 19
                    [Ans_60sec] => 1299
                    [Ans_210sec] => 1347
                    [Ans_30sec] => 1181
                    [SL_60] => 0.9642
                    [SL_210] => 1.0000
                    [SL_30] => 0.8766
                )
 
            [UKVCC] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 0
                    [site] => UKVCC
                    [phone_skill] => PrimaryChat
                    [OU] => UK
                    [Incoming] => 300
                    [Handled_incoming] => 300
                    [Aband_6] => 2
                    [Aband_210] => 2
                    [Aband_30] => 2
                    [Ans_60sec] => 298
                    [Ans_210sec] => 298
                    [Ans_30sec] => 263
                    [SL_60] => 1.0000
                    [SL_210] => 1.0000
                    [SL_30] => 0.8833
                )
 
            [EDI3] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 900
                    [site] => EDI3
                    [phone_skill] => PrimaryChat
                    [OU] => UK
                    [Incoming] => 273
                    [Handled_incoming] => 296
                    [Aband_6] => 2
                    [Aband_210] => 2
                    [Aband_30] => 2
                    [Ans_60sec] => 269
                    [Ans_210sec] => 271
                    [Ans_30sec] => 251
                    [SL_60] => 0.9927
                    [SL_210] => 1.0000
                    [SL_30] => 0.9267
                )
 
        )
 
    [Email] => Array
        (
            [BCD] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 0
                    [site] => BCD
                    [phone_skill] => RetailEmail
                    [OU] => UK
                    [Incoming] => 1309
                    [Handled_incoming] => 1293
                    [Aband_6] => 0
                    [Aband_210] => 0
                    [Aband_30] => 0
                    [Ans_60sec] => 0
                    [Ans_210sec] => 0
                    [Ans_30sec] => 0
                    [SL_60] => 0.0000
                    [SL_210] => 0.0000
                    [SL_30] => 0.0000
                )
 
            [CPT] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 0
                    [site] => CPT
                    [phone_skill] => CsPromotionsEmail
                    [OU] => UK
                    [Incoming] => 15
                    [Handled_incoming] => 49
                    [Aband_6] => 0
                    [Aband_210] => 0
                    [Aband_30] => 0
                    [Ans_60sec] => 0
                    [Ans_210sec] => 0
                    [Ans_30sec] => 0
                    [SL_60] => 0.0000
                    [SL_210] => 0.0000
                    [SL_30] => 0.0000
                )
 
            [DAK] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 0
                    [site] => DAK
                    [phone_skill] => Email
                    [OU] => UK
                    [Incoming] => 1081
                    [Handled_incoming] => 1129
                    [Aband_6] => 0
                    [Aband_210] => 0
                    [Aband_30] => 0
                    [Ans_60sec] => 0
                    [Ans_210sec] => 0
                    [Ans_30sec] => 0
                    [SL_60] => 0.0000
                    [SL_210] => 0.0000
                    [SL_30] => 0.0000
                )
 
            [EDI3] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 0
                    [site] => EDI3
                    [phone_skill] => Email
                    [OU] => UK
                    [Incoming] => 709
                    [Handled_incoming] => 754
                    [Aband_6] => 0
                    [Aband_210] => 0
                    [Aband_30] => 0
                    [Ans_60sec] => 0
                    [Ans_210sec] => 0
                    [Ans_30sec] => 0
                    [SL_60] => 0.0000
                    [SL_210] => 0.0000
                    [SL_30] => 0.0000
                )
 
            [HYD] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 0
                    [site] => HYD
                    [phone_skill] => RetailEmail
                    [OU] => UK
                    [Incoming] => 2012
                    [Handled_incoming] => 1766
                    [Aband_6] => 0
                    [Aband_210] => 0
                    [Aband_30] => 0
                    [Ans_60sec] => 0
                    [Ans_210sec] => 0
                    [Ans_30sec] => 0
                    [SL_60] => 0.0000
                    [SL_210] => 0.0000
                    [SL_30] => 0.0000
                )
 
            [ORK1] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 0
                    [site] => ORK1
                    [phone_skill] => Email
                    [OU] => UK
                    [Incoming] => 1320
                    [Handled_incoming] => 336
                    [Aband_6] => 0
                    [Aband_210] => 0
                    [Aband_30] => 0
                    [Ans_60sec] => 0
                    [Ans_210sec] => 0
                    [Ans_30sec] => 0
                    [SL_60] => 0.0000
                    [SL_210] => 0.0000
                    [SL_30] => 0.0000
                )
 
            [SUT] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 0
                    [site] => SUT
                    [phone_skill] => CarriersEmail
                    [OU] => UK
                    [Incoming] => 3785
                    [Handled_incoming] => 3704
                    [Aband_6] => 0
                    [Aband_210] => 0
                    [Aband_30] => 0
                    [Ans_60sec] => 0
                    [Ans_210sec] => 0
                    [Ans_30sec] => 0
                    [SL_60] => 0.0000
                    [SL_210] => 0.0000
                    [SL_30] => 0.0000
                )
 
            [UKVCC] => Array
                (
                    [statistic_date] => 2013-05-20
                    [time] => 0
                    [site] => UKVCC
                    [phone_skill] => CarriersEmail
                    [OU] => UK
                    [Incoming] => 746
                    [Handled_incoming] => 750
                    [Aband_6] => 0
                    [Aband_210] => 0
                    [Aband_30] => 0
                    [Ans_60sec] => 0
                    [Ans_210sec] => 0
                    [Ans_30sec] => 0
                    [SL_60] => 0.0000
                    [SL_210] => 0.0000
                    [SL_30] => 0.0000
                )
 
        )
 
)
Link to comment
Share on other sites

What will be variable between data sets? Is the left hand column always going to be the same? Will there always be chat, email & Phone? Do the three lette5r column headers going to change?

 

Also, do you care what order that the subheaders are displayed in (e.g. 'ORK', 'EDI', 'VCC', etc.)?

Edited by Psycho
Link to comment
Share on other sites

This should get you started. Define a format array to list the rows in the order you want them to appear. Then call the function passing a title, the array of data, and the rowFormat array.

//Array to define the rows to display with the title as the index and
//the value is the index in the data array to display on that row.
//If you need more data for the rows (e.g. to pass different formats
//for the row, then make the value of each element a sub-array
$rowFormat = array(
    'Offered' => 'Incoming',
    'Handled' => 'Handled_incoming'
);
 

Function to create the table

function outputTable($title, $dataArray, $rowFormat)
{
    $headers = '';
    $subheaders = '';
    $dataRows = array();

    foreach($dataArray as $header => $modeData)
    {
        $colspan = count($modeData);
        $headers .= "<td class=\"center channel_heading\" colspan=\"{$colspan}\"><a href=\"uk_retail_hh.php?c={$header}\">{$header}</a></td>\n";

        foreach($modeData as $subheader => $data)
        {
            $subClass = "{$header}_{$subheader}";
            $subheaders .= "<td class=\"center site_heading {$subClass}\">{$subheader}</td>\n";
            foreach($rowFormat as $label => $index)
            {
                if(!isset($dataRows[$index]))
                {
                    $dataRows[$index] = "<td colspan=\"1\">{$label}</td>";
                }
                $dataRows[$index] .= "<td class=\"center {$subClass}\">{$dataArray[$header][$subheader][$index]}</td>\n";
            }
        }
    }

    $output = '';
    $output .= "<table border=\"1\" bordercolor=\"#000000\" style=\"background-color:#FFFFFF\" width=\"100%\" cellpadding=\"3\" cellspacing=\"0\"\n";
    $output .= "  <tr>\n";
    $output .= "    <td colspan=\"1\" rowspan=\"3\" class=\"unused\"></td>\n";
    $output .= "    <td class=\"center main_heading\" colspan=\"33\">{$title}</td>\n";
    $output .= "  </tr>\n";
    $output .= "  <tr>\n";
    $output .= $headers;
    $output .= "  </tr>\n";
    $output .= "  <tr>\n";
    $output .= $subheaders;
    $output .= "  </tr>\n";
    foreach($dataRows as $row)
    {
        $output .= "  <tr>\n";
        $output .= $row;
        $output .= "  </tr>\n";
    }

    return $output;
}

Usage

echo outputTable("UK", $dataArray, $rowFormat);
Edited by Psycho
Link to comment
Share on other sites

@Psycho - Wow! Thanks!

 

This has definitely got me started.
 

Sorry I didnt respond to your original post, I posted this on my work PC and never thought of checking when I was home last night.

 

Its not 100% but its a serious jump start considering where I was with it yesterday!

 

Thank you very much!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.