0

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
4
  • Can you include the output? Commented Sep 7, 2014 at 9:42
  • 1
    Of course, I get: 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 Commented Sep 7, 2014 at 10:17
  • echo "serija".$serija['serija']." ".$serija['ponovitve']." ponovitev<br>"; this is not compatible with the out put you provided! where is ponovitev word in the output? Commented Sep 7, 2014 at 10:46
  • What is echoed after "serija".$serija['serija']." in particular row is not important, so I just pointed out the problem. You're right in every row there is also "N ponovitve" after "serija N". Commented Sep 7, 2014 at 10:51

2 Answers 2

1

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.

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

Comments

1

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

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.