3

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?

Interactive
  • 1,474
  • 5
  • 25
  • 57
  • Could you show us what the desired output is supposed to look like? – domsson Jun 29 '17 at 10:51
  • @domdom Please see my update – Interactive Jun 29 '17 at 10:54
  • Then use a boolean flag that you trigger after the first loop. See [Bibhudatta's answer](https://stackoverflow.com/a/44823171/3316645). Although I'd advice to name it something more meaningful than just `$flag`. Also you most likely want to check out [`rowspan`](https://stackoverflow.com/questions/8706468/how-to-make-a-td-on-more-than-one-row). – domsson Jun 29 '17 at 10:55
  • Also, here is [another solution](https://stackoverflow.com/questions/831501/display-text-once-within-while-loop-on-the-first-loop) that is using the loop's counter instead of `if` - which is probably not what you want, seeing that you don't have a counter, but still... – domsson Jun 29 '17 at 11:01
  • And: check [this answer](https://stackoverflow.com/a/22482149/3316645) on this possible duplicate: [How do I execute a specific code block for only the first iteration of a loop?](https://stackoverflow.com/questions/22481925/how-do-i-execute-a-specific-code-block-for-only-the-first-iteration-of-a-loop) – domsson Jun 29 '17 at 11:05
  • @Interactive You want to have new `` for each row regardless of company? Do you not want line breaks between each value to keep things in the same cell? Do you want the `company1` cell to span multiple rows? It seems sloppy to me to write new rows each time. – mickmackusa Jun 29 '17 at 11:16

4 Answers4

3

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
domsson
  • 4,553
  • 2
  • 22
  • 40
  • @pAsh absolutely correct. However, seeing how this is tabular data, I would assume it is either sorted or printing the company name again would even be desired (otherwise, how could you tell what company the value belongs to? You would think it belongs to the last one you printed). Worst case, we can always sort the array first. *In other words*: what you describe is a feature rather than a bug. – domsson Jun 29 '17 at 12:14
1

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.

Bibhudatta Sahoo
  • 4,808
  • 2
  • 27
  • 51
  • I would've used a ternary to make it less verbose, but that's a question of style. However, in the name of best practices, I would recommend to name the flag something more meaningful than just `flag`. PS: s/thing/think – domsson Jun 29 '17 at 10:58
  • I see where you are going with this but it doesn't work. I only shows the else values. So it's like the first loop is ignored! Set the first `$flag` to true – Interactive Jun 29 '17 at 11:00
  • Just please check it once again i modified it. – Bibhudatta Sahoo Jun 29 '17 at 11:02
  • I just missed $flag=True; first time. – Bibhudatta Sahoo Jun 29 '17 at 11:03
  • 1
    Haha almost..... This must be my fault for not explaining correct. There are (off course) multiple companies. So create a if statement or something? – Interactive Jun 29 '17 at 11:03
  • Interactive, you really should edit your question to clarify that (multiple companies), as this calls for different answers as your original question! – domsson Jun 29 '17 at 11:25
1
 <?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>';
 ?>
Anil Kumar
  • 44
  • 5
0
$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);
Mukesh
  • 150
  • 1
  • 10