0

I am getting data from my database. everything works but the foreach loop gets duplicate items. I am getting the team names as duplicates.

Picture:enter image description here

I know it's the fault of this code because it's inside a foreach loop but i don't know how to fix this:

foreach($arrayTotals as $team=>$values){
        foreach($values as $v){
            echo "<tr class=\"" . $v['kaupunki_id'] . "\"><td>".$numerointi .".";
            echo "<td>" . $team;
            $sum1=0;
            $sum2=0;
            $numerointi ++;
        }

My whole code:

$arrayTotals = array();
    foreach ($db->query("SELECT pisteet_1, pisteet_2, nimi, team_id, pisteet.kaupunki_id FROM pisteet INNER JOIN joukkueet ON joukkueet.id=pisteet.team_id ORDER BY team_id ASC") as $joukkuenimi) {

         $arrayTotals[$joukkuenimi['nimi']][] = array('pisteet_1'=>$joukkuenimi['pisteet_1'],'pisteet_2'=>$joukkuenimi['pisteet_2'],'kaupunki_id'=>$joukkuenimi['kaupunki_id']);

    }
//var_dump($arrayTotals);
$numerointi =1;
echo "<table class=\"zebra\">";
foreach($arrayTotals as $team=>$values){
    foreach($values as $v){
        echo "<tr class=\"" . $v['kaupunki_id'] . "\"><td>".$numerointi .".";
        echo "<td>" . $team;
        $sum1=0;
        $sum2=0;
        $numerointi ++;
    }
    foreach($values as $v){
            echo "<td class=\"pisteet\">" . $v['pisteet_1'] . "/" . $v['pisteet_2'] . "</td>";
            $sum1 +=$v['pisteet_1'];
            $sum2 +=$v['pisteet_2'];
    }
    echo '<td class="summa">'.$sum1.'/'.$sum2."</td>";
    echo "</tr>";
}
echo '</table>';

My array if i enable:

echo "<pre>";
print_r($arrayTotals);

Array
(
    [Itis/hki] => Array
        (
            [0] => Array
                (
                    [pisteet_1] => 6
                    [pisteet_2] => 10
                    [kaupunki_id] => 1
                )

            [1] => Array
                (
                    [pisteet_1] => 3
                    [pisteet_2] => 10
                    [kaupunki_id] => 1
                )

        )

    [Harju/Jyväskylä] => Array
        (
            [0] => Array
                (
                    [pisteet_1] => 3
                    [pisteet_2] => 10
                    [kaupunki_id] => 5
                )

            [1] => Array
                (
                    [pisteet_1] => 4
                    [pisteet_2] => 10
                    [kaupunki_id] => 5
                )

        )

    [Jojot] => Array
        (
            [0] => Array
                (
                    [pisteet_1] => 6
                    [pisteet_2] => 10
                    [kaupunki_id] => 5
                )

            [1] => Array
                (
                    [pisteet_1] => 7
                    [pisteet_2] => 10
                    [kaupunki_id] => 5
                )

        )

)
7
  • Tried replacing inner join by left join? Without having any example data or the schemas it's not possible to help. Commented Aug 17, 2014 at 15:21
  • 1
    You have commented out //var_dump($arrayTotals);. What does var_dump($arrayTotals); give you? Commented Aug 17, 2014 at 15:33
  • @Sean I included the array result. Commented Aug 17, 2014 at 15:37
  • but they all are different. Commented Aug 17, 2014 at 15:39
  • @suchit yes but the foreach loop adds the name two times. but i don't know how to format my loops that it would work. Sorry if my question was a little unclear. Commented Aug 17, 2014 at 15:42

3 Answers 3

1

Your first foreach() is causing the issue, as it is looping through both subarrays before printing the second foreach loop. Try removing it, and just reference the first $v['kaupunki_id'] using $values[0]['kaupunki_id'] -

$numerointi =1;
echo "<table class=\"zebra\">";
foreach($arrayTotals as $team=>$values){
    echo "<tr class=\"" . $values[0]['kaupunki_id'] . "\"><td>".$numerointi .".";
    echo "<td>" . $team;
    $sum1=0;
    $sum2=0;
    $numerointi ++;

    foreach($values as $v){
            echo "<td class=\"pisteet\">" . $v['pisteet_1'] . "/" . $v['pisteet_2'] . "</td>";
            $sum1 +=$v['pisteet_1'];
            $sum2 +=$v['pisteet_2'];
    }
    echo '<td class="summa">'.$sum1.'/'.$sum2."</td>";
    echo "</tr>";
}
echo '</table>';
Sign up to request clarification or add additional context in comments.

Comments

1

try like this:

$db->query("SELECT pisteet_1, pisteet_2, nimi, team_id, pisteet.kaupunki_id FROM pisteet INNER JOIN joukkueet ON joukkueet.id=pisteet.team_id where pisteet_1 is not null or pisteet_1 !='' ORDER BY team_id ASC") 

Note: in where clause you can add your desired condition which you thind will de good to get the appropriate result or add more than one condition which should be true to get the result. this will ommite the result with blank or null values.

2 Comments

i am still getting the same result :/
it will be better if you add the column names at the top of your question by editing. and use distinct with select and see.
0

Try if the code below will help you to remove duplicates.

function find_dup($p1, $p2){
   $n=0;
foreach($p1 as $key => $val){
if($val==$p2) $n++;
return $nb;
}

$ar = array(10,20,10,30,40,10);
echo find_dup($ar,10) // will output 3

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.