简体   繁体   中英

PHP & CSV - Echo Result only once

i am fairly new to PHP and tried several hours to get something going, sadly without a result. I hope you can point me into the right direction.

So what i got is a CSV file containing Articles. They are separated into diff columns and always the same structure, for example:

ArtNo, ArtName, ColorCode, Color, Size

When an article has different color codes in the CSV, the article is simply repeated with the same information except for the color code, see an example:

ABC237;Fingal Edition;48U;Nautical Blue;S - 5XL;
ABC237;Fingal Edition;540;Navy;S - 5XL;

My problem is, i want to display all the articles in a table, include an article image etc.. so far i got that working which is not a problem, but instead of showing the article twice for every different color code i want to create only one line per ArtNo (First CSV Line) but still read the second duplicate line to add the article color to the first one, like:

ABC237; Fingal Edition ;540;Nautical Blue, Navy;S - 5XL;

Is this even possible or am I going into a complete wrong direction here? My code looks like this

    <?php
    $csv = readCSV('filename.csv');
    
    foreach ($csv as $c) {
    
    $artNo = $c[0]; $artName = $c[1]; $colorCode = $c[2]; $color = $c[3]; $sizes = $c[4]; $catalogue = $c[5]; $GEP = $c[6]; $UVP = $c[7]; $flyerPrice = $c[8]; $artDesc = $c[9]; $size1 = $c[10]; $size2 = $c[11]; $size3 = $c[12]; $size4 = $c[13]; $size5 = $c[14]; $size6 = $c[15]; $size7 = $c[16]; $size8 = $c[17]; $picture = $c[0] . "-" . $c[2] . "-d.jpg";
        
    // Echo HTML Stuff
    }
?>

Read CSV Function

<?php
function readCSV($csvFile){
    $file_handle = fopen($csvFile, 'r');
        while (!feof($file_handle) ) 
        {
            $line_of_text[] = fgetcsv($file_handle, 0, ";");
        }
    fclose($file_handle);
    return $line_of_text;

}?>

I tried to get along with array_unique etc but couldn't find a proper solution.

Read all the data into an array, using the article number as the key....

 while (!feof($file_handle) ) {
        $values = fgetcsv($file_handle, 0, ";");
        $artno = array_shift($values);
        if (!isset($data[$artno])) $data[$artno]=array();
        $data[$artno][]=$values;
    }

And then output it:

 foreach ($data as $artno=>$v) {
     $first=each($v);
     print $artno . "; " . each($first);
     foreach ($v as $i) {
       $discard=array_shift($i);
       print implode(";", $i);
     }
     print "\n";
 }

(code not tested, YMMV)

You need to know exactly how many items belong to each ArtNo group. This means a loop to group, and another loop to display.

When grouping, I steal the ArtNo from the row of data and use it as the grouping key. The remaining data in the row will be an indexed subarray of that group/ArtNo.

I am going to show you some printf() and sprintf() syntax to keep things clean. printf() will display the first parameter's content and using any subsequent values to replace the placeholders in the string. In this case, the 2nd parameter is a conditional expression. On the first iteration of the group, ( $i = 0 ), we want to show the ArtNo as the first cell of the row and declare the number of rows that it should span. sprinf() is just like printf() except it produces a value ( s ilently). Upon any subsequent iterations of the group, $i will be greater than zero and therefore an empty string is passed as the value.

Next, I'm going to use implode() which is beautifully flexible when you don't know exactly how many columns your table will have (or if the number of columns may change during the lifetime of your project).

Tested Code:

$csv = <<<CSV
ABC237;Fingal Edition;48U;Nautical Blue;S - 5XL
ABC236;Fingal Edition;540;Navy;S - 5XL
ABC237;Fingal Edition;49U;Sea Foam;L - XL
ABC237;Fingal Edition;540;Navy;S - 5XL
CSV;

$lines = explode(PHP_EOL, $csv);
foreach ($lines as $line) {
    $row = str_getcsv($line, ';');
    $grouped[array_shift($row)][] = $row;
}

echo '<table>';
foreach ($grouped as $artNo => $group) {
    foreach ($group as $i => $values) {
        printf(
            '<tr>%s<td>%s</td></tr>',
            (!$i ? sprintf('<td rowspan="%s">%s</td>', count($group), $artNo) : ''),
            implode('</td><td>', $values)
        );
    }
}
echo '</table>';

Output:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM