AliG Posted December 4, 2021 Share Posted December 4, 2021 (edited) HI I I have the following code , I wish to output my array in a nicely formatted way but I cannot get to do this. The array should look like this Data Row 1 Number 9000000 Name Zebra Type Noropor List number 159 Data Row 2 Number 9000000 Name Zebra Type Noropor List number 159 Data Row 3 Number 9000000 Name Zebra Type Noropor List number 159 This is my code <body> <center> <h1>DISPLAY DATA PRESENT IN CSV</h1> <?php function imp_open($pfad) { // Daten auslesen und in der Tabelle speichern $content = file($pfad); return $content; }; $array=imp_open('C:/xampp1/htdocs/S1/Medikamente.csv'); foreach($array as $key => $value) { if (!is_array($value)) { echo $key ." => ". $value ."\r\n" ; } else { echo $key ." => array( \r\n"; foreach ($value as $key2 => $value2) { echo "\t". $key2 ." => ". $value2 ."\r\n"; } echo ")"; } } /*echo "<table>"; foreach ($array as $key => $value) { echo $key, $value,"<br>"; } /*echo "</table>"; $y=explode(",", $value);*/ ?> </center> </body> </html> my code would bring the following output This is my data 9999900001,"zebra","Noropr","159" 9999900002,"coco1","Noropr","999998" 9999900003,"coco12","Noropr","78" 99999000099999,"coco1123","Noropr","33" 9999900005,"coco198","Noropr","79" 9999900006,"coco111","Noropr","66" 9999900007,"coco1456","NoroprNoropr","2999996" 9999900008,"coco1sss","Salbe","55" 9999900009,"coco90","Salbe","90" 9999900010,"coco111111","Tabletten","102" 9999900011,"coco178989","Noropr","999998" 9999900012,"coco18283838383","Noropr","59" 9999900013,"coco17874738774","Tabletten","899999" 99999000199999,"Tannosynt","Salbe","71" 9999900015,"Vomex A","Noropr","699999" 9999900016,"Vomex A","Noropr","35" Thanks for the asisstance. Edited December 4, 2021 by AliG wrong data and more text Quote Link to comment https://forums.phpfreaks.com/topic/314275-displaying-data-out-of-csv-saved-in-an-array-in-a-formatted-way/ Share on other sites More sharing options...
gw1500se Posted December 4, 2021 Share Posted December 4, 2021 Did you try this? $array=imp_open('C:/xampp1/htdocs/S1/Medikamente.csv'); echo "<pre>"; print_r($array); echo "</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/314275-displaying-data-out-of-csv-saved-in-an-array-in-a-formatted-way/#findComment-1592429 Share on other sites More sharing options...
AliG Posted December 4, 2021 Author Share Posted December 4, 2021 4 minutes ago, gw1500se said: Did you try this? $array=imp_open('C:/xampp1/htdocs/S1/Medikamente.csv'); echo "<pre>"; print_r($array); echo "</pre>"; Thanks , I did , this just displays the array with keys and index , I want the data to be presented in a rather formatted way as shown above or here like this Data Row 1 Number 9000000 Name Zebra Type Noropor List number 159 Data Row 2 Number 9000000 Name Zebra Type Noropor List number 159 Data Row 3 Number 9000000 Name Zebra Type Noropor List number 159 Quote Link to comment https://forums.phpfreaks.com/topic/314275-displaying-data-out-of-csv-saved-in-an-array-in-a-formatted-way/#findComment-1592430 Share on other sites More sharing options...
Barand Posted December 4, 2021 Share Posted December 4, 2021 try <?php $data = []; $data_list = ''; $heads = ['Number', 'Name', 'Type', 'List number']; $csv = fopen('Medikamente.csv', 'r'); while ($line = fgetcsv($csv)) { $data[] = array_combine($heads, $line); } fclose($csv); foreach ($data as $rec) { foreach ($rec as $k => $v) { $data_list .= "<label>$k</label> $v<br>"; } $data_list .= "<br>\n"; } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Test</title> <meta charset="utf-8"> <style type='text/css'> label { display: inline-block; width: 120px; background-color: #E0E0E0; color: black; padding: 8px; border: 1px solid white; } </style> </head> <body> <?= $data_list ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/314275-displaying-data-out-of-csv-saved-in-an-array-in-a-formatted-way/#findComment-1592431 Share on other sites More sharing options...
AliG Posted December 4, 2021 Author Share Posted December 4, 2021 2 hours ago, Barand said: try <?php $data = []; $data_list = ''; $heads = ['Number', 'Name', 'Type', 'List number']; $csv = fopen('Medikamente.csv', 'r'); while ($line = fgetcsv($csv)) { $data[] = array_combine($heads, $line); } fclose($csv); foreach ($data as $rec) { foreach ($rec as $k => $v) { $data_list .= "<label>$k</label> $v<br>"; } $data_list .= "<br>\n"; } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Test</title> <meta charset="utf-8"> <style type='text/css'> label { display: inline-block; width: 120px; background-color: #E0E0E0; color: black; padding: 8px; border: 1px solid white; } </style> </head> <body> <?= $data_list ?> </body> </html> Thanks, getting this error Fatal error: Uncaught ValueError: array_combine(): Argument #1 ($keys) and argument #2 ($values) must have the same number of elements in C:\xampp1\htdocs\S1\try.php:7 Stack trace: #0 C:\xampp1\htdocs\S1\try.php(7): array_combine(Array, Array) #1 {main} thrown in C:\xampp1\htdocs\S1\try.php on line 7 Quote Link to comment https://forums.phpfreaks.com/topic/314275-displaying-data-out-of-csv-saved-in-an-array-in-a-formatted-way/#findComment-1592432 Share on other sites More sharing options...
Barand Posted December 4, 2021 Share Posted December 4, 2021 Then your csv data is not as you posted, with 4 items of data in each row. I used the data you gave Medikamente.csv... 9999900001,"zebra","Noropr","159" 9999900002,"coco1","Noropr","999998" 9999900003,"coco12","Noropr","78" 99999000099999,"coco1123","Noropr","33" 9999900005,"coco198","Noropr","79" 9999900006,"coco111","Noropr","66" 9999900007,"coco1456","NoroprNoropr","2999996" 9999900008,"coco1sss","Salbe","55" 9999900009,"coco90","Salbe","90" 9999900010,"coco111111","Tabletten","102" 9999900011,"coco178989","Noropr","999998" 9999900012,"coco18283838383","Noropr","59" 9999900013,"coco17874738774","Tabletten","899999" 99999000199999,"Tannosynt","Salbe","71" 9999900015,"Vomex A","Noropr","699999" 9999900016,"Vomex A","Noropr","35" ... and my results were etc. Quote Link to comment https://forums.phpfreaks.com/topic/314275-displaying-data-out-of-csv-saved-in-an-array-in-a-formatted-way/#findComment-1592437 Share on other sites More sharing options...
AliG Posted December 5, 2021 Author Share Posted December 5, 2021 Thanks, you are absolutely right , my CSV had something probably corrupt or something, I recreated the Medikamente.csv test data and the code works like a charm. Just out or curosity wanted to ask , since i but my head alot to get this right , can it not work this way , integrating this function with file and then probbaly with explode ? Thanks a million again for the asisstance. function imp_open($pfad) { // Daten auslesen und in der Tabelle speichern $content = file($pfad); return $content; }; $array=imp_open('C:/xampp1/htdocs/S1/Medikamente.csv'); $data=explode(",", $array); Quote Link to comment https://forums.phpfreaks.com/topic/314275-displaying-data-out-of-csv-saved-in-an-array-in-a-formatted-way/#findComment-1592446 Share on other sites More sharing options...
Barand Posted December 5, 2021 Share Posted December 5, 2021 Yes, you could, but your explode above isn't giving the array that you need. You would still have to loop through your array lines and remove the newline from the end of each line explode each individual line trim off the quotes $data = []; $data_list = ''; $heads = ['Number', 'Name', 'Type', 'List number']; $array = imp_open('Medikamente.csv'); foreach ($array as $line) { $rec = explode(',', $line); $rec = array_map(function($v) { return trim($v, ' "'); }, $rec); $data[] = array_combine($heads, $rec); } foreach ($data as $rec) { foreach ($rec as $k => $v) { $data_list .= "<label>$k</label> $v<br>"; } $data_list .= "<br>\n"; } function imp_open($pfad) { // Daten auslesen und in der Tabelle speichern $content = file($pfad, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES); return $content; } When php provides a function specifically for handling csv data and does all that for you, why not use it? Quote Link to comment https://forums.phpfreaks.com/topic/314275-displaying-data-out-of-csv-saved-in-an-array-in-a-formatted-way/#findComment-1592447 Share on other sites More sharing options...
AliG Posted December 5, 2021 Author Share Posted December 5, 2021 24 minutes ago, Barand said: Yes, you could, but your explode above isn't giving the array that you need. You would still have to loop through your array lines and remove the newline from the end of each line explode each individual line trim off the quotes $data = []; $data_list = ''; $heads = ['Number', 'Name', 'Type', 'List number']; $array = imp_open('Medikamente.csv'); foreach ($array as $line) { $rec = explode(',', $line); $rec = array_map(function($v) { return trim($v, ' "'); }, $rec); $data[] = array_combine($heads, $rec); } foreach ($data as $rec) { foreach ($rec as $k => $v) { $data_list .= "<label>$k</label> $v<br>"; } $data_list .= "<br>\n"; } function imp_open($pfad) { // Daten auslesen und in der Tabelle speichern $content = file($pfad, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES); return $content; } When php provides a function specifically for handling csv data and does all that for you, why not use it? You are right ,i would personally prefer that to Uwe fopen. However a self peoclaimed coding Genius is pushing us to use File in a PHP Bootcamp at my end. Thats that , nonethless appreciate your asisstance, have great sunday. Quote Link to comment https://forums.phpfreaks.com/topic/314275-displaying-data-out-of-csv-saved-in-an-array-in-a-formatted-way/#findComment-1592449 Share on other sites More sharing options...
Barand Posted December 5, 2021 Share Posted December 5, 2021 There is a compromise solution using file() and that is to use str_getcsv() instead of explode(). $data = []; $data_list = ''; $heads = ['Number', 'Name', 'Type', 'List number']; $array = imp_open('Medikamente.csv'); foreach ($array as $line) { $rec = str_getcsv($line); $data[] = array_combine($heads, $rec); } foreach ($data as $rec) { foreach ($rec as $k => $v) { $data_list .= "<label>$k</label> $v<br>"; } $data_list .= "<br>\n"; } function imp_open($pfad) { // Daten auslesen und in der Tabelle speichern $content = file($pfad, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES); return $content; } Quote Link to comment https://forums.phpfreaks.com/topic/314275-displaying-data-out-of-csv-saved-in-an-array-in-a-formatted-way/#findComment-1592453 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.