0

I have this MySQL table:

+------------+-------------+
| contact_id | _company_id |
+------------+-------------+
|          1 |           1 |
|          2 |           1 |
|          3 |           1 |
|          4 |           2 |
|          5 |           2 |
+------------+-------------+

Im trying to print a new <div> containing all rows with the same _company_id

So far I have this code:

$previous = '';

while ($result = $stmt->fetch()) {

    $current = $_company_id;

    //This should be executed every first iteration and every time $_company_id changes
    if ($current != $previous) { $html .= '<div id="company-' . $_company_id . '" class="tab-pane fade">'; }

    //Iterate all contact_id here with the same _company_id

    if ($current != $previous) { $html .= '</div>'; }

    $previous = $current;

}

My desired output is:

<div id="company-1" class="tab-pane fade">123</div>
<div id="company-2" class="tab-pane fade">45</div>

Right now the output is:

<div id="company-1" class="tab-pane fade">1</div>
23
<div id="company-2" class="tab-pane fade">4</div>
5

What should I change?

2 Answers 2

1
$previous = null;

while ($result = $stmt->fetch()) {

    $current = $_company_id;

    //This should be executed every first iteration and every time $_company_id changes
    if ($current !== $previous) {
      if($previous !== null)
        $html.='</div>';
      $html .= '<div id="company-' . $_company_id . '" class="tab-pane fade">';
    }

    //Iterate all contact_id here with the same _company_id

    $previous = $current;

}

if($previous!==null)
  $html.='</div>';
Sign up to request clarification or add additional context in comments.

2 Comments

I'm trying to understand why this part if($previous !== null) $html.='</div>'; does not output </div> at the very first iteration? At the first iteration the variable $previous is equal to null (right?). Your code works fine, but I can't figure out why! Would you please explain that part? Thank's
First we set $previous to null - it's say we not opened any div. When we have new company, we check $previous. If $previous is null, then we not opened any div and don't need close anything. If $previous not null, then we already opened div and must close him. In end of code we check again, because if we opened div his don't close in iteration. I hope it is clear, in spite of my bad english.
0

Can you post your SQL query and/or some more code in this example. Off the top of my head, you could do something like:

<?php
$dbh = new PDO("mysql:host=localhost;dbname=companies","root","root");
$sql = "select _company_id from table_name limit 0,30;";
$companies = $dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC);
foreach($companies as $company){
    $contacts_sql = "select contact_id from table_name where company_id = :company_id";
    $stmt = $dbh->prepare($sql);
    $stmt->bindParam(":company_id", $company['_company_id'], PDO::PARAM_INT);
    $stmt->execute();
    $contacts = $stmt->fetchAll(PDO::FETCH_ASSOC);
    echo "<div id='company-{$company['_company_id']}'>";
    foreach($contacts as $contact){
        echo $contact['contact_id'];        
    }
    echo "</div>".PHP_EOL;
}
?>

Could you not?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.