1

there is something wrong with my code. Loops are doing too much reps. For example, query in phpmyadmin is returning 4 results, and my code is returning 5. Another example.Print of $arr[0] should return

Wybory
Wybory
Burmistrz
Rada

but it returns

Wybory
Wybory
Wybory
Burmistrz
Rada
Rada Rada

What is wrong? It is adding too much elements I've got 14 elements in "komitet" at start, but it created 15 elements array.

$WYNIK = '
            <h2 class="site-title">Kandydaci</h2>
            <div id="kandydaci_lista" class="row">
                <ul class="rejon">';
    $zapytanie='SELECT * FROM komitety';
        $wynik=mysql_query($zapytanie) or die('błąd bazy');
        $komitety=array();

        while($komitety[]=mysql_fetch_assoc($wynik));


        foreach($komitety as $komitet){
            $WYNIK.='<li><span id="komitet_'.$komitet['id'].'">'.$komitet['nazwa'].'<img src="'.$ADR.'img/arrow_bot.png"></span>';

                $zapytanie='
                    SELECT kandydat_do
                    FROM kandydaci
                    WHERE komitet="'.$komitet['id'].'"
                    GROUP BY kandydat_do
                ';
                //echo"<pre>";
                $wynik=mysql_query($zapytanie) or die('błąd bazy');

                $kandydaci_do=array();
                array_pop($kandydaci_do);
                print_r($kandydaci_do);
                while($kandydaci_do[]=mysql_fetch_assoc($wynik));
                   //print_r($kandydaci_do);


                $WYNIK.='<ul class="kandydat_do">';
                    foreach($kandydaci_do as $kandydat_do){

                                        $WYNIK.='<li><span>'.$kandydat_do['kandydat_do'].'</span>';

                                        $zapytanie2='
                                            SELECT k.okreg, ok.granice_okregu from kandydaci k
                                            LEFT JOIN okregi as ok ON k.komitet=ok.teryt
                                            where k.komitet= "'.$komitet['id'].'" and k.okreg=ok.nr_okregu
                                            group by k.okreg
                                        ';
                                        $wynik2=mysql_query($zapytanie2);
                                        $okregi=array();

                                        while($okregi[]=mysql_fetch_assoc($wynik2));

                                            foreach($okregi as $okreg){

                                                if($okreg['okreg']==0)
                                                {
                                                    $zapytanie='
                                                        SELECT k.*, ko.nazwa as komitet
                                                        FROM kandydaci as k
                                                        LEFT JOIN komitety as ko ON ko.id=k.komitet
                                                        WHERE k.komitet="'.$komitet['id'].'" AND k.kandydat_do="'.$kandydat_do['kandydat_do'].'" AND k.status=1
                                                        group by k.id
                                                    ';
                                                }
                                                else{
                                                    $zapytanie='
                                                        SELECT k.*, ko.nazwa as komitet
                                                        FROM kandydaci as k
                                                        LEFT JOIN komitety as ko ON ko.id=k.komitet
                                                        WHERE k.komitet="'.$komitet['id'].'" AND k.kandydat_do="'.$kandydat_do['kandydat_do'].'" AND k.status=1 AND k.okreg='.$okreg['okreg'].'
                                                        group by k.id
                                                    ';
                                                }
                                                //echo $zapytanie.'<br />';
                                                $wynik=mysql_query($zapytanie);
                                                $kandydaci=array();
                                                while($kandydaci[]=mysql_fetch_assoc($wynik));
                                                $arr = explode(' ',trim($kandydat_do['kandydat_do']));
                                                //echo $arr[0];
                                                if($okreg['okreg']==0 || $arr[0]=='Burmistrz' || $arr[0]=='Wójt'){

                                                }
                                                else{
                                                    $WYNIK.='
                                                    <ul class="okreg"><span>Nr okręgu: '.$okreg['okreg'].'</span>
                                                    <br>
                                                    <span class="granice">'.$okreg['granice_okregu'].'</span>';
                                                }

                                                    foreach($kandydaci as $kandydat){

                                                        if($kandydat['imie']==''){

                                                        }
                                                        else{
                                                            $WYNIK.='
                                                            <li class="kandydat '.($kandydat['platny']?'platny':'').'">
                                                                <a href="kandydaci,'. $kandydat['id'] .'.html">
                                                                <span>'.$kandydat['imie'].' '.$kandydat['nazwisko'].'</span>
                                                                </a>
                                                            </li>'; 
                                                        }

                                                    }
                                                    if($okreg['okreg']==0 || $arr[0]=='Burmistrz' || $arr[0]=='Wójt'){

                                                    }
                                                    else{
                                                        $WYNIK.='</ul>';
                                                    }
                                            }
                                        $WYNIK.='</li>';

                                    }

                $WYNIK.='</ul>';
            $WYNIK.='</li>'; 
        }
        $WYNIK.='</ul>';

        $WYNIK.='</div>'; 
        $SRODEK.=$WYNIK;
9
  • 2
    Why are you using the long-deprecated mysql_ code library? It was discontinued many years ago and removed entirely in PHP7. No new code should be written using this library. It leaves you vulnerable to SQL injection attacks (due to the lack of parameterised query support) and potentially other unpatched vulnerabilities. Switch to using mysqli or PDO as soon as possible, and then learn how to write parameterised queries to protect your data from malicious input. See bobby-tables.com for a simple explanation of the risks and some sample PHP code to write queries safely. Commented Sep 26, 2018 at 9:15
  • 1
    Would you please code in english? It's quite unease to read Commented Sep 26, 2018 at 9:16
  • 1
    I don't know what is wrong, but I know why it goes wrong: you are mixing data and output. First put your data together in an aray, then format your output based on that array. In that case it's far easier to trap any errors. Commented Sep 26, 2018 at 9:17
  • which array has too many elements? It's not really clear. Commented Sep 26, 2018 at 9:19
  • Im using mysql insted of mysqli beacuse i have to add module in really old project, and I dont have time to modify whole code. I know it's very wrong... Every array have at least 1 additional element, I just want to know why, mayby i forgot something there. Starting with first query, it should return 14 elements and it does in phpmyadmin. When im putting query result in array Im receiving 15 elements. Commented Sep 26, 2018 at 9:53

1 Answer 1

1

You need to change your array-building code to look like this (same for all the places where you do this task):

$komitety=array(); 
while ($row = mysql_fetch_assoc($wynik)) { 
  $komitety[] = $row; 
} 

The reason for this is that the last time mysql_fetch_assoc() runs it returns false (due to there being no more rows) and it adds that false result as an item in your array.

This is because it adds to the array before it checks the condition in the while loop, whereas you want the opposite - you want it to check the condition before adding to the array.

Sign up to request clarification or add additional context in comments.

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.