Interactive
Interactive

Reputation: 1550

display multiple data only once

I'm creating a table based upon an imported .csv file.

This works fine however I want to spit out the company name once.

So this is the csv:

company,value  
company1, 231  
company1, 432  
company1, 876  

If I foreach the data into a table I want to display the companyname once.

echo'<table><tr><td>Companyname</td><td>Value</td>';
foreach ($data as $gegevens){
    $companyname = $gegevens['Companyname'];
    $value = $gegevens['Value'];
    echo '<tr><td>'.$companyname.'</td><td>'.$value.'</td></tr> ';

}   
echo'</table>';

This is what I want to have as output:
[Companyname] [Value]
[company 1 ]       [231]
[                   ]       [432]
[                   ]       [876]

Any thoughts on how I can accomplish this?

Upvotes: 3

Views: 1007

Answers (4)

domsson
domsson

Reputation: 4681

See OP's comment on another answer:

Haha almost..... This must be my fault for not explaining correct. There are (off course) multiple companies. So create a if statement or something?

Therefore, I would suggest something like this:

$data = array(
    array('Companyname' => 'FBI', 'Value' => '1'),
    array('Companyname' => 'CIA', 'Value' => '2'),
    array('Companyname' => 'CIA', 'Value' => '3'),
    array('Companyname' => 'CIA', 'Value' => '4'),
    array('Companyname' => 'NSA', 'Value' => '5'),
    array('Companyname' => 'NSA', 'Value' => '6'),
);

$last_company = ""; // What was the last company we printed?

echo'<table><tr><td>Companyname</td><td>Value</td>';
foreach ($data as $gegevens){
    $companyname = $gegevens['Companyname'];
    $value = $gegevens['Value'];
    if ($companyname == $last_company) {
        echo '<tr><td></td><td>'.$value.'</td></tr>';
    } else {
        echo '<tr><td>'.$companyname.'</td><td>'.$value.'</td></tr>';
        $last_company = $companyname;
    }
}
echo'</table>';

This generates the following output (when viewed with a browser):

Companyname Value
FBI         1
CIA         2
            3
            4
NSA         5
            6

Upvotes: 3

Anil Kumar
Anil Kumar

Reputation: 44

 <?php 
 $data = array(

    array('Companyname' => 'Company', 'Value' => '10'),
    array('Companyname' => 'Company1', 'Value' => '10'),
    array('Companyname' => 'Company1', 'Value' => '10'),
    array('Companyname' => 'Company2', 'Value' => '10'),
 ); 

 $newdata = array();

 foreach ($data as $gegevens){
    $newdata[$gegevens['Companyname']][] = $gegevens['Value']; 
 }

 echo'<table><tr><td>Companyname</td><td>Value</td>';
 foreach($newdata as $company => $values) {
    echo '<tr><td>'.$companyname.'</td><td>'.implode('<br>' ,$values).'</td></tr> ';
}
echo'</table>';
 ?>

Upvotes: 1

Mukesh
Mukesh

Reputation: 150

$rows = array();
foreach($data as $gegevens) {
    $companyname = $gegevens['Companyname'];
    $value = $gegevens['Value'];
    echo '<tr><td>'.$companyname.'</td><td>'.$value.'</td></tr> ';
    if(isset($rows[$companyname])) {
        $rows[$companyname] .= '<tr><td>&nbsp;</td><td>'.$value.'</td></tr> ';
    } else {
        $rows[$companyname] =  '<tr><td>'.$companyname.'</td><td>'.$value.'</td></tr> ';
    }
}
echo'<table><tr><td>Companyname</td><td>Value</td>';
echo implode("",$rows);

Upvotes: 0

Bibhudatta Sahoo
Bibhudatta Sahoo

Reputation: 4894

You can do like this using flag variable.

   $flag=True;
    echo'<table><tr><td>Companyname</td><td>Value</td>';
    foreach ($data as $gegevens){
        $companyname = $gegevens['Companyname'];
        $value = $gegevens['Value'];
    if($flag){
    $flag=false;
        echo '<tr><td>'.$companyname.'</td><td>'.$value.'</td></tr> ';
    }else{
        echo '<tr><td> </td><td>'.$value.'</td></tr> ';
    }
    }   
    echo'</table>';

I thing it will solve your issue.

Upvotes: 1

Related Questions