vineet0306 Posted September 2, 2015 Share Posted September 2, 2015 (edited) I have been trying to get API results in a HTML table, but not able to do same. When i echo API response its coming perfectly but on rendering in HTML table it is created and , showing total results and even creating paging but results are not displayed i mean it is showing blank table. P.S. - I am trying to get results in datatables. CODE :- <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap 101 Template</title> <link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css"> <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/plug-ins/9dcbecd42ad/integration/bootstrap/3/dataTables.bootstrap.css"> <script type="text/javascript" language="javascript" src="//code.jquery.com/jquery-1.11.1.min.js"></script> <script type="text/javascript" language="javascript" src="//cdn.datatables.net/1.10.3/js/jquery.dataTables.min.js"></script> <script type="text/javascript" language="javascript" src="dataTables.bootstrap.js"></script> <script type="text/javascript" charset="utf-8"> $(document).ready(function() { $('#example').dataTable(); } ); </script> <!-- Bootstrap <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">--> <!-- Optional theme <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">--> <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> <!--[if lt IE 9]> <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <![endif]--> <style> /*.filterable { margin-top: 15px; } .filterable .panel-heading .pull-right { margin-top: -20px; } .filterable .filters input[disabled] { background-color: transparent; border: none; cursor: auto; box-shadow: none; padding: 0; height: auto; } .filterable .filters input[disabled]::-webkit-input-placeholder { color: #333; } .filterable .filters input[disabled]::-moz-placeholder { color: #333; } .filterable .filters input[disabled]:-ms-input-placeholder { color: #333; } */ </style> </head> <body> <div class="container"> <?php error_reporting(0); // Specify API URL define('HASOFFERS_API_URL', 'http://api.hasoffers.com/Apiv3/json'); // Specify method arguments $args = array( 'NetworkId' => 'abc', 'Target' => 'Report', 'Method' => 'getConversions', 'NetworkToken' => 'NETkHhcasadzxcFYRnvsrtertKvh', 'fields' => array( 'Offer.name', 'Stat.ad_id', 'Stat.affiliate_id', 'Stat.advertiser_info', 'Stat.affiliate_info1', 'Stat.status', 'Stat.session_ip', 'Stat.datetime', 'Stat.datetime_diff' ), 'filters' => array( 'Stat.affiliate_id' => array( 'conditional' => 'EQUAL_TO', 'values' => '2' ) ), 'data_start' => '2015-09-01', 'data_end' => '2015-09-01' ); // Initialize cURL $curlHandle = curl_init(); // Configure cURL request curl_setopt($curlHandle, CURLOPT_URL, HASOFFERS_API_URL . '?' . http_build_query($args)); // Make sure we can access the response when we execute the call curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); // Execute the API call $jsonEncodedApiResponse = curl_exec($curlHandle); // Ensure HTTP call was successful if($jsonEncodedApiResponse === false) { throw new \RuntimeException( 'API call failed with cURL error: ' . curl_error($curlHandle) ); } // Clean up the resource now that we're done with cURL curl_close($curlHandle); // Decode the response from a JSON string to a PHP associative array $apiResponse = json_decode($jsonEncodedApiResponse, true); // Make sure we got back a well-formed JSON string and that there were no // errors when decoding it $jsonErrorCode = json_last_error(); if($jsonErrorCode !== JSON_ERROR_NONE) { throw new \RuntimeException( 'API response not well-formed (json error code: ' . $jsonErrorCode . ')' ); } // Print out the response details if($apiResponse['response']['status'] === 1) { // No errors encountered echo 'API call successful'; echo PHP_EOL; echo 'Response Data: ' . print_r($apiResponse['response']['data'], true); echo PHP_EOL; } else { // An error occurred echo 'API call failed (' . $apiResponse['response']['errorMessage'] . ')'; echo PHP_EOL; echo 'Errors: ' . print_r($apiResponse['response']['errors'], true); echo PHP_EOL; } ?> </div> <div class="container"> <h3>Affiliates Details</h3> <p><span>Total Records:-<?php print_r($apiResponse['response']['data']['count']); ?></span></p> <hr> <table class="table table-striped table-bordered" id="example"> <thead> <tr class="filters"> <th>Affiliate Id</th> <th>Offer ID</th> <th>Status</th> <th>Session IP</th> <th>Transaction ID</th> <th>Adv info</th> <th>Aff Info</th> <th>Date</th> </tr> </thead> <tbody> <?php foreach($apiResponse['response']['data'] as $x =>$x_value) { // echo "Affiliate ID" . $x . ", Value=" ; //print_r($x_value); foreach($x_value as $x => $x_value) { $sss = $x_value['Stat.affiliate_id']; echo "<tr>"; echo "<td style='width:50px;'>".$x_value['Stat.affiliate_id']."</td>"; echo "<td>".$x_value['Offer.name']."</td>"; echo "<td>".$x_value['Stat.status']."</td>"; echo "<td>".$x_value['Stat.session_ip']."</td>"; echo "<td>".$x_value['Stat.ad_id']."</td>"; echo "<td>".$x_value['Stat.advertiser_info']."</td>"; echo "<td>".$x_value['Stat.affiliate_info1']."</td>"; echo "<td>".$x_value['Stat.datetime']."</td>"; echo "</tr>"; } } ?> </tbody> </table> </div> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>--> <!-- Include all compiled plugins (below), or include individual files as needed <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>--> </body> </html> Edited September 2, 2015 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 2, 2015 Share Posted September 2, 2015 Here your second foreach loop foreach($x_value as $x => $x_value) { The second $x_value will be overwriting the value of first $x_value. This will be causing problems looping the array of results. Try renaming the second $x_value to be $value you then need to use $value inside the second foreach loop, rather than $x_value if that does not fix the problem then can you show us the output of echo 'Response Data: ' . print_r($apiResponse['response']['data'], true); Quote Link to comment Share on other sites More sharing options...
vineet0306 Posted September 3, 2015 Author Share Posted September 3, 2015 (edited) Hi Ch0cu3r, Thanks for you reply. I have tried but results are same. I am able to print response from API but when i am trying to get the same in datatables nothing is coming up but i can see blank <tr> and <td>. Below is the running code :- <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap 101 Template</title> <link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css"> <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/plug-ins/9dcbecd42ad/integration/bootstrap/3/dataTables.bootstrap.css"> <script type="text/javascript" language="javascript" src="//code.jquery.com/jquery-1.11.1.min.js"></script> <script type="text/javascript" language="javascript" src="//cdn.datatables.net/1.10.3/js/jquery.dataTables.min.js"></script> <script type="text/javascript" language="javascript" src="dataTables.bootstrap.js"></script> <script type="text/javascript" charset="utf-8"> $(document).ready(function() { $('#example').dataTable(); } ); </script> <!-- Bootstrap <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">--> <!-- Optional theme <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">--> <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> <!--[if lt IE 9]> <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <![endif]--> <style> /*.filterable { margin-top: 15px; } .filterable .panel-heading .pull-right { margin-top: -20px; } .filterable .filters input[disabled] { background-color: transparent; border: none; cursor: auto; box-shadow: none; padding: 0; height: auto; } .filterable .filters input[disabled]::-webkit-input-placeholder { color: #333; } .filterable .filters input[disabled]::-moz-placeholder { color: #333; } .filterable .filters input[disabled]:-ms-input-placeholder { color: #333; } */ </style> </head> <body> <div class="container"> <?php error_reporting(0); // Specify API URL define('HASOFFERS_API_URL', 'http://api.hasoffers.com/Apiv3/json'); // Specify method arguments $args = array( 'NetworkId' => 'icubes', 'Target' => 'Report', 'Method' => 'getConversions', 'NetworkToken' => 'NETkHhcGxnnB4FYRnvJLNUI3LmHKvh', 'fields' => array( 'Offer.name', 'Stat.ad_id', 'Stat.affiliate_id', 'Stat.advertiser_info', 'Stat.affiliate_info1', 'Stat.status', 'Stat.session_ip', 'Stat.datetime', 'Stat.datetime_diff' ), 'filters' => array( 'Stat.affiliate_id' => array( 'conditional' => 'EQUAL_TO', 'values' => '2' ) ), 'data_start' => '2015-09-01', 'data_end' => '2015-09-01' ); // Initialize cURL $curlHandle = curl_init(); // Configure cURL request curl_setopt($curlHandle, CURLOPT_URL, HASOFFERS_API_URL . '?' . http_build_query($args)); // Make sure we can access the response when we execute the call curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); // Execute the API call $jsonEncodedApiResponse = curl_exec($curlHandle); // Ensure HTTP call was successful if($jsonEncodedApiResponse === false) { throw new \RuntimeException( 'API call failed with cURL error: ' . curl_error($curlHandle) ); } // Clean up the resource now that we're done with cURL curl_close($curlHandle); // Decode the response from a JSON string to a PHP associative array $apiResponse = json_decode($jsonEncodedApiResponse, true); // Make sure we got back a well-formed JSON string and that there were no // errors when decoding it $jsonErrorCode = json_last_error(); if($jsonErrorCode !== JSON_ERROR_NONE) { throw new \RuntimeException( 'API response not well-formed (json error code: ' . $jsonErrorCode . ')' ); } // Print out the response details if($apiResponse['response']['status'] === 1) { // No errors encountered echo 'API call successful'; echo PHP_EOL; //echo 'Response Data: ' . print_r($apiResponse['response']['data'], true); echo PHP_EOL; } else { // An error occurred echo 'API call failed (' . $apiResponse['response']['errorMessage'] . ')'; echo PHP_EOL; echo 'Errors: ' . print_r($apiResponse['response']['errors'], true); echo PHP_EOL; } ?> </div> <div class="container"> <h3>Affiliates Details</h3> <p><span>Total Records:-<?php print_r($apiResponse['response']['data']['count']); ?></span></p> <hr> <table class="table table-striped table-bordered" id="example"> <thead> <tr class="filters"> <th>Affiliate Id</th> <th>Offer ID</th> <th>Status</th> <th>Session IP</th> <th>Transaction ID</th> <th>Adv info</th> <th>Aff Info</th> <th>Date</th> </tr> </thead> <tbody> <?php foreach($apiResponse['response']['data'] as $x =>$x_value) { // echo "Affiliate ID" . $x . ", Value=" ; //print_r($x_value); foreach($x_value as $x => $value) { print_r($value); $sss = $value['Stat.affiliate_id']; echo "<tr>"; echo "<td style='width:50px;'>".$value['Stat.affiliate_id']."</td>"; echo "<td>".$value['Offer.name']."</td>"; echo "<td>".$value['Stat.status']."</td>"; echo "<td>".$value['Stat.session_ip']."</td>"; echo "<td>".$value['Stat.ad_id']."</td>"; echo "<td>".$value['Stat.advertiser_info']."</td>"; echo "<td>".$value['Stat.affiliate_info1']."</td>"; echo "<td>".$value['Stat.datetime']."</td>"; echo "</tr>"; } } ?> </tbody> </table> </div> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>--> <!-- Include all compiled plugins (below), or include individual files as needed <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>--> </body> </html> Edited September 3, 2015 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted September 3, 2015 Solution Share Posted September 3, 2015 (edited) Variables like $value['Stat.xxxx] and $value['Offer.yyyy] need to be written as $value['Stat]['xxxx'] and $value['Offer]['yyyy'] Edited September 3, 2015 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
vineet0306 Posted September 3, 2015 Author Share Posted September 3, 2015 Hi Ch0cu3r, Thanks man!!, it works like a charm. You are a life saver Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.