Barand Posted October 16, 2021 Share Posted October 16, 2021 One way to test it would be to work backwards from the JSON. Decode the JSON string and recreate a table from its data so you can visually compare the results with a display or print of the original. It would be too tricky to recreate something as bad as the original so go for something simpler, say If you want to test it in CLI mode, then, unless you want the extra fun of drawing grids on the screen, you could go for a simple column display, such as Quote Link to comment https://forums.phpfreaks.com/topic/313994-web-scraping-unstructured-html-table/page/3/#findComment-1591131 Share on other sites More sharing options...
RohanH Posted October 16, 2021 Author Share Posted October 16, 2021 54 minutes ago, Barand said: you could go for a simple column display how we do so using assertions or it will be simple try and catch (I mean to ask is how to we realize that the test failed)? Another question is to run the test do we create the test function (using the foreach loops and print the column value?) and then call the file like Quote php test.php ? Quote Link to comment https://forums.phpfreaks.com/topic/313994-web-scraping-unstructured-html-table/page/3/#findComment-1591132 Share on other sites More sharing options...
Barand Posted October 16, 2021 Share Posted October 16, 2021 52 minutes ago, RohanH said: (I mean to ask is how to we realize that the test failed)? It fails if the output values from the the new test script don't match the original table's values. For example, the output from my test script, built from the JSON data, looks like this (which can be easily compared with the original) ... You aren't compelled to use my method. Feel free to think up your own. Quote Link to comment https://forums.phpfreaks.com/topic/313994-web-scraping-unstructured-html-table/page/3/#findComment-1591133 Share on other sites More sharing options...
RohanH Posted October 16, 2021 Author Share Posted October 16, 2021 4 hours ago, Barand said: simple column display I want to display the column in the cli but I fail to do so! This is what I tried to print in cli. foreach($results_by_date as $name => $work) { foreach($work as $kdate => $date) { if($kdate == 'date'){ echo $date. "\n"; }else{ foreach($date as $kdata => $date_data){ foreach($date_data as $kdatakey => $data){ echo " " . $data. "\n"; } } } } } Quote Link to comment https://forums.phpfreaks.com/topic/313994-web-scraping-unstructured-html-table/page/3/#findComment-1591134 Share on other sites More sharing options...
Solution Barand Posted October 16, 2021 Solution Share Posted October 16, 2021 The data looks OK. It could do with some separation between the blocks of student data to make it easier to read. Perhaps... foreach($results_by_date as $name => $work) { foreach($work as $kdate => $date) { if($kdate == 'date'){ echo str_repeat('-', 20)."\n" ; echo $date. "\n\n"; }else{ foreach($date as $kdata => $date_data){ foreach($date_data as $kdatakey => $data){ echo " " . $data. "\n"; } echo " \n" ; } } } } FYI, this is my version for cli output // JSON from previous script saved in file $results = json_decode(file_get_contents('c:/inetpub/wwwroot/test/doc1355/rohan.json'), 1); $pad = str_repeat(' ', 8); $divs = $pad . str_repeat('-', 12) . PHP_EOL; $divd = str_repeat('-', 20) . PHP_EOL; echo '<pre>'; // test only - not required in CLI mode foreach ($results as $day) { echo $day['date'] . PHP_EOL . PHP_EOL; foreach ($day['students'] as $k => $sdata) { if ($k) echo $divs; foreach ($sdata as $val) { echo $pad . $val . PHP_EOL; } } echo $divd; } Quote Link to comment https://forums.phpfreaks.com/topic/313994-web-scraping-unstructured-html-table/page/3/#findComment-1591136 Share on other sites More sharing options...
RohanH Posted October 16, 2021 Author Share Posted October 16, 2021 Thanks for the help, also the explanation for each step which cleared certain concepts for me! 🙂 Quote Link to comment https://forums.phpfreaks.com/topic/313994-web-scraping-unstructured-html-table/page/3/#findComment-1591138 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.