Jump to content

displaying data out of CSV saved in an array in a formatted way


Recommended Posts

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 

 

image.thumb.png.587b98461f7d417931c8f6d7fe741939.png

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.

 

 

 

 

 

image.png

Edited by AliG
wrong data and more text
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

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>

 

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

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

image.png.0aca46c51a07b848647f14712c62e890.png

etc.

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);

 

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?

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.

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; 
   }

 

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.