Here is my PHP function, and json i am reading
While reading json i am caculating rowspan , but could not calculate colspan dynamically, thats why table is not alliging columns properly.
Need a jquery function to auto add colspan to a column which is needed to be added. or in function i can caculate colspan.
$data = json_decode($json, true);
function generateHtmlTableFromJson($data, $step = 1, $rowspan = 0, $rowspan2 = 0, $colspan = 0, $level = 0, $previousLevel = 0)
{
$html = ''; // Initialize the HTML string
$a = 0;
foreach ($data as $item) {
// Initialize rowspan as 1 by default
if (is_array($item['node']['value'])) {
}
// $colspan = 4;
if ($step == 1) {
$colspan = 4;
$counted_span = countNodes($item['node']['value']);
// exit;
$html .= '<tr>';
$html .= '<td rowspan="' . $counted_span . '">' . $item['node']['key'] . '</td>'; // Accessing nested data
}
if (is_array($item['node']['value'])) {
foreach ($item['node']['value'] as $index => $nested_item) {
// echo " ". $nested_item['node']['key'];
$rowspan2 = "";
if ($index > 0) {
$html .= '<tr>'; // Start a new row for additional rows created by nested array
}
if (is_array($nested_item['node']['value'])) {
$rowspan2 = countNodes($nested_item['node']['value']); // Calculate rowspan based on the length of the nested array
//$colspan=0;
$html .= '<td rowspan="' . $rowspan2 . '">' . $nested_item['node']['key'] .$step.'</td>';
} else {
//echo
$html .= '<td rowspan="' . $rowspan2 . '" colspan="' . $colspan . '">' . $nested_item['node']['key'] .'</td>';
}
if (isset($nested_item['node']['value']) && is_array($nested_item['node']['value'])) {
$step++;
++$level;
echo $previousLevel = $level;
if ($level > $previousLevel) { // Check if the current level is greater than the previous level
$colspan--;
}
$html .= generateHtmlTableFromJson([$nested_item], $step, $rowspan, $rowspan2, $colspan, $level,$previousLevel); // Recursively generate HTML table for nested array
//$html .= '</td>';
} else {
// Display normal values
// $html .= '<td>' . $nested_item['node']['value'] . '</td>';
$html .= '<td>' . $nested_item['type'] . '</td>';
$html .= '<td>' . $nested_item['minLength'] . '</td>';
$html .= '<td>' . $nested_item['desc'] . '</td>';
}
$html .= '</tr>';
}
} else {
// Display normal values
// $html .= '<td>' . $item['node']['value'] . '</td>';
$html .= '<td>' . $item['type'] . '</td>';
$html .= '<td>' . $item['minLength'] . '</td>';
$html .= '<td>' . $item['desc'] . '</td>';
$html .= '</tr>';
}
$a++;
}
//$finalColspan = 6;
// $html = str_replace('colspan="' . $colspan . '"', 'colspan="' . $finalColspan . '"', $html);
return $html;
}
function countArraysRecursive($array) {
$count = 0;
foreach ($array as $value) {
if (is_array($value)) {
$count++; // Increment count for the current array
$count += countArraysRecursive($value); // Recursively count arrays in child arrays
}
}
return $count;
}
// countTopArrays($json);
function countNodes($array) {
$count = 0;
foreach ($array as $key => $value) {
if ($key === "node") {
$count++;
}
if (is_array($value)) {
$count += countNodes($value);
}
}
return $count;
}
$html = '<table border="1">';
$html .= '<tr><th>Node</th><th colspan="4">Child Node</th><th>Type</th><th>Length</th><th>Description</th></tr>';
$html .= generateHtmlTableFromJson($data);
$html .= '</table>';
echo $html;
function calculateColspan($valueArray) {
$colspan = 0;
foreach ($valueArray as $item) {
if (isset($item['value']) && is_array($item['value'])) {
$colspan += calculateColspan($item['value']);
} else {
$colspan++;
}
}
return $colspan;
}
$json = '[
{
"node":{
"key":"header",
"value":[
{
"node":{
"key":"msg_id",
"value":"236001"
},
"type":"string",
"minLength":1,
"maxLength":12,
"desc":"Message ID, this field should be unique id for each Api call. This will be generated from client side. If the same message ID is used the system will decline the API call with Error Description “Duplicate Message ID” ",
"req":"required"
},
{
"node":{
"key":"msg_type",
"value":"TRANSACTION"
},
"type":"string",
"minLength":1,
"maxLength":12,
"desc":"Message Type – This can have either “TRANSACTION” or “ENQUIRY”. As for this API the value expected is “TRANSACTION” ",
"req":"required"
},
{
"node":{
"key":"msg_function",
"value":"REP_CLIENT_CREATE"
},
"type":"string",
"minLength":1,
"maxLength":50,
"desc":"Message functions: Should be “REP_CLIENT_CREATE” ",
"req":"required"
},
{
"node":{
"key":"src_application",
"value":"IVR"
},
"type":"string",
"minLength":1,
"maxLength":10,
"desc":"Source Application: This is a free Text and the client can populate the source system from where the API is Initiated. Example: IVR, IB, MB. No Validations of these are kept at Network Systems",
"req":"required"
},
{
"node":{
"key":"target_application",
"value":"WAY4"
},
"type":"string",
"minLength":1,
"maxLength":10,
"desc":"The target_application can hold any value from FI side, this can be used by FI to check the target system of the API call",
"req":"required"
},
{
"node":{
"key":"timestamp",
"value":"2020-07-20T06:49:02.366Z"
},
"type":"string",
"minLength":1,
"maxLength":15,
"desc":"Timestamp of the response - Format YYYY-MM-DDtHH:MM:SS.SSS+04:00",
"req":"required"
},
{
"node":{
"key":"bank_id",
"value":"NIC"
},
"type":"string",
"minLength":1,
"maxLength":4,
"desc":"Bank Id is Unique Id 4 digit code for each client and the same will be provided once the client setup is completed in our core system. For sandbox testing – Please use “NIC” ",
"req":"required"
}
]
},
"type":"object",
"minLength":null,
"maxLength":null,
"desc":"",
"req":"false"
},
{
"node":{
"key":"exception_details",
"value":[
{
"node":{
"key":"application_name",
"value":"TCC-ADP"
},
"type":"string",
"minLength":1,
"maxLength":20,
"desc":" Application Name",
"req":"required"
},
{
"node":{
"key":"date_time",
"value":"2023-02-01T16:39:47.314+04:00"
},
"type":"string",
"minLength":1,
"maxLength":30,
"desc":"Timestamp of the response Format ",
"req":"required"
},
{
"node":{
"key":"status",
"value":"S"
},
"type":"string",
"minLength":1,
"maxLength":1,
"desc":" Status of the request (S/F)",
"req":"required"
},
{
"node":{
"key":"error_code",
"value":"000"
},
"type":"string",
"minLength":1,
"maxLength":4,
"desc":" EAI Internal Error Code (Check error codes section for the complete list of error codes and error code descriptions)",
"req":"required"
},
{
"node":{
"key":"error_description",
"value":"Success"
},
"type":"string",
"minLength":1,
"maxLength":100,
"desc":" Error Description (Check error codes section for the complete list of error codes and error code descriptions)",
"req":"required"
},
{
"node":{
"key":"transaction_ref_id",
"value":"236001"
},
"type":"string",
"minLength":1,
"maxLength":20,
"desc":" The tracking_id sent in the request will be sent back in response in this field.",
"req":"false"
}
]
},
"type":"",
"minLength":0,
"maxLength":0,
"desc":" ",
"req":"false"
},
{
"node":{
"key":"body",
"value":[
{
"node":{
"key":"customer_id",
"value":"100000027"
},
"type":"string",
"minLength":1,
"maxLength":20,
"desc":"Customer ID: Customer Identification number nn This should be a unique number",
"req":"required"
},
{
"node":{
"key":"bank_code",
"value":"982"
},
"type":"string",
"minLength":1,
"maxLength":3,
"desc":"Bank code is Unique Id 3 digit number for each client and the same will be provided once the client setup is completed in our core system. For sandbox testing – “982” is used",
"req":"required"
},
{
"node":{
"key":"external_client_number",
"value":"100000027"
},
"type":"string",
"minLength":1,
"maxLength":20,
"desc":"additional identifier for the client that is generated by FI system",
"req":"false"
},
{
"node":{
"key":"card_name",
"value":"JAMES ROBERT"
},
"type":"string",
"minLength":1,
"maxLength":21,
"desc":"Card embossing name defined on client level (Upper case)",
"req":"required"
},
{
"node":{
"key":"personal_details",
"value":[
{
"node":{
"key":"gender",
"value":"M"
},
"type":"string",
"minLength":1,
"maxLength":6,
"desc":"Gender ex Male",
"req":"false"
},
{
"node":{
"key":"title",
"value":"Mr"
},
"type":"string",
"minLength":1,
"maxLength":4,
"desc":"Title ex Mr",
"req":"false"
},
{
"node":{
"key":"first_name",
"value":"James"
},
"type":"string",
"minLength":1,
"maxLength":255,
"desc":"First Name ex James",
"req":"required"
},
{
"node":{
"key":"last_name",
"value":"Robert"
},
"type":"string",
"minLength":1,
"maxLength":255,
"desc":"Last Name ex Robert",
"req":"required"
},
{
"node":{
"key":"middle_name",
"value":"Ivich"
},
"type":"string",
"minLength":1,
"maxLength":255,
"desc":"Last Name ex Ivich",
"req":"false"
},
{
"node":{
"key":"citizenship",
"value":"ARE"
},
"type":"string",
"minLength":1,
"maxLength":3,
"desc":"Citizenship",
"req":"required"
},
{
"node":{
"key":"marital_status",
"value":"M"
},
"type":"string",
"minLength":1,
"maxLength":18,
"desc":"Marital Status",
"req":"false"
},
{
"node":{
"key":"date_of_birth",
"value":"1968-08-25"
},
"type":"string",
"minLength":1,
"maxLength":20,
"desc":"Date of birth",
"req":"required"
},
{
"node":{
"key":"place_of_birth",
"value":"Dubai"
},
"type":"string",
"minLength":1,
"maxLength":255,
"desc":"Place of birth",
"req":"false"
},
{
"node":{
"key":"language",
"value":"ENG"
},
"type":"string",
"minLength":1,
"maxLength":3,
"desc":"Language ISO code",
"req":"false"
},
{
"node":{
"key":"security_name",
"value":"Tima"
},
"type":"string",
"minLength":1,
"maxLength":255,
"desc":"Security Name",
"req":"false"
}
]
},
"type":"object",
"minLength":null,
"maxLength":null,
"desc":"",
"req":"false"
},
{
"node":{
"key":"contact_details",
"value":[
{
"node":{
"key":"home_phone",
"value":"919702310992"
},
"type":"string",
"minLength":1,
"maxLength":32,
"desc":"Home Phone Number",
"req":"false"
},
{
"node":{
"key":"work_phone",
"value":"919702310992"
},
"type":"string",
"minLength":1,
"maxLength":32,
"desc":"Work Phone Number",
"req":"false"
},
{
"node":{
"key":"mobile_phone",
"value":"919702310992"
},
"type":"string",
"minLength":1,
"maxLength":32,
"desc":"Mobile Number",
"req":"false"
},
{
"node":{
"key":"email",
"value":"[email protected]"
},
"type":"string",
"minLength":1,
"maxLength":255,
"desc":"Email address",
"req":"false"
}
]
},
"type":"object",
"minLength":null,
"maxLength":null,
"desc":"",
"req":"false"
},
{
"node":{
"key":"addresses",
"value":[
{
"node":{
"key":"address_type",
"value":"PERMANENT"
},
"type":"string",
"minLength":1,
"maxLength":20,
"desc":"PERMANENT/PRESENT/WORK",
"req":"required"
},
{
"node":{
"key":"address_line_1",
"value":"Al Mahata Towers"
},
"type":"string",
"minLength":1,
"maxLength":255,
"desc":"Building Name",
"req":"required"
},
{
"node":{
"key":"address_line_2",
"value":"LandLord"
},
"type":"string",
"minLength":1,
"maxLength":255,
"desc":"LandLord/Tenant",
"req":"false"
},
{
"node":{
"key":"address_line_3",
"value":"House no - 1105"
},
"type":"string",
"minLength":1,
"maxLength":255,
"desc":"House Name or House Number",
"req":"false"
},
{
"node":{
"key":"address_line_4",
"value":"Al Qasmia"
},
"type":"string",
"minLength":1,
"maxLength":255,
"desc":"Street Name",
"req":"false"
},
{
"node":{
"key":"email",
"value":"[email protected]"
},
"type":"string",
"minLength":1,
"maxLength":255,
"desc":"Email Id",
"req":"false"
},
{
"node":{
"key":"phone",
"value":"919702310992"
},
"type":"string",
"minLength":1,
"maxLength":32,
"desc":"Phone number",
"req":"false"
},
{
"node":{
"key":"city",
"value":"Dubai"
},
"type":"string",
"minLength":1,
"maxLength":255,
"desc":"City name",
"req":"required"
},
{
"node":{
"key":"country",
"value":"ARE"
},
"type":"string",
"minLength":1,
"maxLength":255,
"desc":"Country Code Ex SAU",
"req":"required"
},
{
"node":{
"key":"zip",
"value":"24537"
},
"type":"string",
"minLength":1,
"maxLength":32,
"desc":"Zip Code",
"req":"false"
},
{
"node":{
"key":"state",
"value":"Dubai"
},
"type":"string",
"minLength":1,
"maxLength":32,
"desc":"State",
"req":"false"
}
]
},
"type":"array",
"minLength":null,
"maxLength":null,
"desc":"",
"req":"false"
},
{
"node":{
"key":"identity_proof_document",
"value":[
{
"node":{
"key":"number",
"value":"R589856"
},
"type":"string",
"minLength":1,
"maxLength":20,
"desc":"ID Number",
"req":"false"
},
{
"node":{
"key":"type",
"value":"Passport"
},
"type":"string",
"minLength":1,
"maxLength":20,
"desc":"passport, national_id, etc",
"req":"false"
},
{
"node":{
"key":"expiry_date",
"value":"2025-05-20"
},
"type":"string",
"minLength":1,
"maxLength":10,
"desc":"YYYY-MM-DD",
"req":"false"
}
]
},
"type":"array",
"minLength":null,
"maxLength":null,
"desc":"",
"req":"false"
},
{
"node":{
"key":"supplementary_documents",
"value":[
{
"node":{
"key":"number",
"value":"R589856"
},
"type":"string",
"minLength":1,
"maxLength":20,
"desc":"ID Number",
"req":"false"
},
{
"node":{
"key":"type",
"value":"Passport"
},
"type":"string",
"minLength":1,
"maxLength":20,
"desc":"passport, national_id, etc",
"req":"false"
},
{
"node":{
"key":"expiry_date",
"value":"2025-05-20"
},
"type":"string",
"minLength":1,
"maxLength":10,
"desc":"YYYY-MM-DD",
"req":"false"
}
]
},
"type":"array",
"minLength":null,
"maxLength":null,
"desc":"",
"req":"false"
},
{
"node":{
"key":"custom_fields",
"value":[
{
"node":{
"key":"key",
"value":"CN-DATE_OPEN"
},
"type":"string",
"minLength":1,
"maxLength":20,
"desc":"Client type Tag",
"req":"false"
},
{
"node":{
"key":"value",
"value":"2024-01-15"
},
"type":"string",
"minLength":1,
"maxLength":128,
"desc":"Tag value",
"req":"false"
}
]
},
"type":"array",
"minLength":null,
"maxLength":null,
"desc":"",
"req":"false"
}
]
},
"type":"object",
"minLength":null,
"maxLength":null,
"desc":"",
"req":"false"
}
]
';