PHP SQL where statement with loop variable
<p>I am having problems with the retrieval of data from my database using a where clause with a for loop variable. this codes currently can retrieve the data from the database but however the values retrieved from the database stockpiles. at the number 1 in the for loop its still fine. but when it goes to the next one. the data gathered is the combination of 1 and 2. when it finally hit the end of the loop all of the data is displayed.
Which part of the codes must be edited to change the display of data to non-cumulative</p>

<pre><code>&lt;?php

$sectorcount = $row_SectorCount['COUNT(*)'];
//number of rows in database
$spendingname= array();
$spendingpercent= array();
$spendingid= array();

?&gt;
// Add some data to the details pie chart
options_detail_1.series.push({ name: 'Tax Spending Detail', data: [] });

$(document).ready(function() {
chart_main = new Highcharts.Chart(options_main);
chart_detail_1 = new Highcharts.Chart(options_detail_1);
})

function push_pie_detail(sectorid) {
switch(sectorid)
{
&lt;?php 

 for ($i=1; $i&lt;=$sectorcount; $i++)
    {
    echo    "case 'sector".$i."':" ,
            "setTimeout", "(" , '"chart_detail_1.series[0].remove(false)", 1000);' ,
            "chart_detail_1.addSeries(options_detail_1, false);" , 
            "chart_detail_1.series[1].setData( [";

            mysql_select_db($database_conn2, $conn2);
            $query_Spending = "SELECT CONCAT(spending.SectorID,     spending.ExpenditureID) AS 'SpendingID',
            expenditure.ExpenditureName, spending.SpendingPercent, spending.SectorID
            FROM spending
            INNER JOIN expenditure ON spending.ExpenditureID = expenditure.ExpenditureID
            WHERE spending.SectorID = '.$i.'";
            $Spending = mysql_query($query_Spending, $conn2) or die(mysql_error());
            $totalRows_Spending = mysql_num_rows($Spending);
            while($row_Spending = mysql_fetch_assoc($Spending))
            {
            $spendingname[] = $row_Spending['ExpenditureName'];
            $spendingpercent[] = $row_Spending['SpendingPercent'];
            $spendingid[]= $row_Spending['SpendingID'];
            }
            mysql_free_result($Spending);

            $a = 0;

            foreach ( $spendingid as $sgi =&gt; $sgid)
            {
            if ($a &gt; 0) echo ', '; // add the comma

            echo    "{
                name: '$spendingname[$sgi]',
                y: ". $spendingpercent[$sgi] * $tax .",
                id: ' $sgid',
                    }";

            $a++;   
            }

            echo    "], false);" ,
                    "chart_detail_1.redraw();",
                    "break;";
            }       


            ?&gt;
            default:
            break;
        }
</code></pre>

<p>}</p>
