Gregor Simončič
Gregor Simončič

Reputation: 73

php: Too many outputs with nested while loops

I have a table up_vadba in wich I have some records that needs to be printed out. I created three nested while loops (see below), which returns main category, subcategory and subcategory results.

Everything is OK, except the last subcategory results might output too many results if previous subcategory results had more results than the last - I hope you understand what I'm explaining? For the first main category I have two subcategories and in each subcategories are two or more subcategory results. This code outputs as many subcategory results as they are in previous subcategory, instead of the right number of results.

I'm bumping in the wall with my head for a few days now to solve this problem and it seems I just can't figure it out. I also used the mysqli_fetch_array function, used counter and nothing helped.

Could any of you took a look what is wrong with the code? Thanks guys, would save me a wall :D

$sql="SELECT * FROM up_vadba WHERE upid='$usid' GROUP BY vadba"; //it gets main category
$vadbe=mysqli_query($cxn,$sql) or die ("Nisem uspel izvesti poizvedbe vadb");
while($vadba=mysqli_fetch_assoc($vadbe)) //as long as there is main category
{
  echo "Vadba ".$vadba['vadba']."<br>";
  $sqlvaje="SELECT * FROM up_vadba WHERE upid='$usid' GROUP BY imeVaje"; //it gets subcategory
  $vaje=mysqli_query($cxn,$sqlvaje) or die ("Nisem uspel izvesti poizvedb vaj");
  while($vaja=mysqli_fetch_assoc($vaje)) //as long as there is subcategory
  {
    echo $vaja['imeVaje']."<br>";
    $sqlserije="SELECT * FROM up_vadba WHERE upid='$usid' GROUP BY serija"; //it gets subcategory results 
    $serije=mysqli_query($cxn,$sqlserije) or die ("Nisem uspel izvesti poizvedb serij");
    while($serija=mysqli_fetch_assoc($serije)) //as long as there are subcategory results
        {
        echo "serija".$serija['serija']." ".$serija['ponovitve']." ponovitev<br>";
    }
  }
}

I got this output:

Vadba 1
- počep
    serija 1
    serija 2
    serija 3
- izpadni korak
    serija 1
    serija 2
    serija 3

Instead of:

Vadba 1
- počep
    serija 1
    serija 2
    serija 3
- izpadni korak
    serija 1
    serija 2

Upvotes: 0

Views: 101

Answers (2)

Gregor Simončič
Gregor Simončič

Reputation: 73

Yes, yes it worked, it worked...I just had to add another condition in my last statement. I will vote up, when I get 15 rep points. Thanke you very much. How I couldn't remember to do that??? And here is what I added:

...echo $vaja['imeVaje']."<br>";
$subvaja=$vaja['imeVaje'];
$sqlserije="SELECT * FROM up_vadba WHERE upid='$usid' AND imeVaje='$subvaja' GROUP BY serija"; //as long as there are subcategory results...

Thanks again guys. You saved my wall and my head from concushion :D

Upvotes: 1

Arijoon
Arijoon

Reputation: 2300

Look at your last query:

$sqlserije="SELECT * FROM up_vadba WHERE upid='$usid' GROUP BY serija";

you are not changing $usid therefore you are getting the exact same subcategory list everytime! If you edit your data in the sql database to something other than sequential numbers it will be a lot easier to spot. I believe you need to make sure $usid is compatible with the current $vaja['id'] (which is assume is your subcategory).

By current I mean the object in the secondary while loop.

Upvotes: 1

Related Questions