Okay, here is what I am trying to do. Someone will take a quiz. The results will then be calculated by adding the value (from 1-5) of various questions together (example: question 1 has a value of 5, question 37 has a value of 4, question 73 has a value of 3). I have 18 different categories that they will be placed in based on the calculated values. I only want it to show the top 5 categories based on the calculated values. Their test results will not be stored in a database so I can't use the limit function in MySQL. I can sort the array properly but it shows all 18 categories.

So how can I limit the number of records to the top 5?

    Well, if you're able to sort it to say the top 5 will always contain the top 5 scores (which is what you want to show), then it could be as simple as:

    for($a = 0; $a < 5; $a++)
      echo $myArray[$a] . '<br>';
    

    Here you just loop through the first 5 array elements.

      Thanks, but this is an associative array with key/values so I can't increment it that way.

        Ah... You hadn't specified that in your post...

        I'm thinking you'll have to loop through your entire array and find the top 5 values and their keys and create either a multidimensional array or just store them in 5 seperate variables. Then display them. This approach would void any sorting you are already doing to get the top 5 since you'll have to loop through each one anyways...

          Sorry I thought that I had mentioned it was an associative array in my post but I had only thought it. Yeah, I am thinking that a multi-dimensional array is going to be the only way to do it or go ahead and stick it in a database and retrieve the values from there using MySQL's Limit.

          Thanks for the help.

            If there data is already in the database, then go ahead and use the LIMIT approach. But if you have to go insert the values JUST to use LIMIT, then I'd have to say its not worth it (doesn't make sense to take on that much script overhead).

            Depending on what you need done, a class might be a good fit.

              Actually I will have the same problem if I use a database as I do now as I have found out. It is in a class so I will play with it to see if I can get it to work properly using a multi-dimensional array.

                Write a Reply...