Jump to content

PHP Foreach Loop

l11h

Hi, 

 

I wanted to know if the following is possible? 

 

I know foreach loop is for array which goes through an array.

currently my code goes through each array element and displays an element 

 

e.g. If Array[0] = 1 then print out hello (this in it's very basic form)

 

The array is for a quiz, it is also used in a session which can get data from a database. 

 

But I want to do the following

 

to go through the array match any array with database where lesson = 1, and only for those that have lesson = 1 look for all arrays that = 1 add them up and divide by how many there are. 

then something very similar to the ones with lesson = 2. 

 

My current setup is limiting me to only 1 question per lesson, I don't want to be limited in this way.

 

I just need some tips on how I could do this, if you do want me to give more in depth detail of code then I can also do this

 

Thanks

Link to comment
Share on other sites

Link to post
Share on other sites

so... you want to compute the average of something?

 

can you please explain it a bit more accurately? for example, it would be nice to know what you need from your database, what arrays you have and what do they contain (if there are more than one)

Link to comment
Share on other sites

Link to post
Share on other sites

Could you possibly give us a little more information on the structure of your data/variables?

Is lesson a database table field? Is it a member of an object you have?

It's hard to fully understand what you want... perhaps a code snippet would help, too.

Link to comment
Share on other sites

Link to post
Share on other sites

Sorry for not enough information 

I'm making an online tutorial 

So lesson is a field in the database which is basically 1, 2, 3, 4, 5

The database has other information such as question number, question itself, answer choices and the correct answer

 

My current code is the following: 

 

                $testarray = $_SESSION['answer_array'];
the answer_array saves what the user got for each question e.g. Question 1 (being array[0]) is either 1 or 0 so 1 being correct 0 being incorrect
 
I then use this for some feedback which is:
 
             $result42 = mysql_query("SELECT * FROM answersbeginner1 LIMIT 1");
 
            while($row = mysql_fetch_array($result42))
                {
                    echo "<br />";                    
                    //echo "Feedback: ";  
                    if ($testarray[0] == '0')
                        {
                            echo "Feedback: You need to revise topic 1 again!";
                        }
                    else
                        {
                            echo "";
                        }
 
                    echo "<br>";
                }
 
 

So the above code for the query is basically useless, the testarray is what is important. 

So if the first question the answer is equal to 0 then the user will get they need to revise topic 1 again. 

 

What I want is to go through a loop for the testarray, and does a match with the lesson field. 

So i'm thinking it's foreach but not sure

 

But what I want is something like the following

 

$lesson1 = $row['lesson'] == '1';

 

now i get stuck here 

 

So how do i get where it just looks for lesson 1, loops through and get echos out a result? 

 

So for example it finds that 2 / 5 match where $testarray = 1. 

Saves that to an array and then later I can do another calculation.

 

Sorry i'm not good at explaining stuff lol.

Link to comment
Share on other sites

Link to post
Share on other sites

how is the answersbeginner1 table done? what columns does it contain?

 

and try to explain it in plain words, without talking about code and arrays and stuff

do you want to count the number of correct given answers?

do you just want to check if the user answered correctly to a single certain question? do you want to check all the questions at once?

Link to comment
Share on other sites

Link to post
Share on other sites

So the table answersbeginner1 has the following columns: 

ID | Question_ID | Answer | Correct | Lesson

example data will be: 

1 | 2 | True | 1 | 1 

2 | 2 | False | 0 | 1

3 | 3 | True | 0 | 1

4 | 3 | False | 1 | 1

 

I've got the code for counting correct given answers

I've got the code for the user answered correctly to a single question

 

As you can see from the database example above Lesson 1 has 2 questions being 2 and 3. This can be how ever many questions I want, no limit to amount of question per lesson. 

 

So I want to count the number of questions the user got right for Lesson 1 and find an average. 

The user answers are saved in an array. 

 

So I want where only lesson 1 is looked at, the array is looped and outputs the amount of answers correct and how many questions in total. 

 

My current code basically is 1 question per lesson. So if user gets question 1 right they know topic otherwise they don't which isn't how it works in real life lol. 

 

I'm finding it hard to get my head around this lol, so it is hard to explain. 

Link to comment
Share on other sites

Link to post
Share on other sites

alright, i think i understood what you're doing

 

 

 

i see a problem with how you're storing the results: for every answer you store both the possible answers, and then you say in an additional field whether that is the correct one or not

other than being a solution that uses more disk/memory space than needed, it's actually even harder to work with it

 

so this is how you store the data

ID | Question_ID | Answer | Correct | Lesson1  |     2       | True   |    1    |   12  |     2       | False  |    0    |   13  |     3       | True   |    0    |   14  |     3       | False  |    1    |   1

but you could just have one line per question, storing the value of the correct answer. this way, the question_id could even be the primary key for the answer, so you don't need the dedicated ID either

Question_ID | Answer | Lesson    2       | True   |   1    3       | False  |   1

now, as you said, the user answers are saved in an array

what you have to do is just query the database, retrieve the correct answers and compare them to the user's answers

you will need a counter, that will count up when the answer is correct, this way you will know how many correct answers the user has given

the total number of answers given is just the size of the user answer's array, that you can retrieve using the count() function

 

 

 

P.S.

when your code is done and you think that it works well enough, you may want to use mysqli instead of mysql functions, because those are deprecated

Link to comment
Share on other sites

Link to post
Share on other sites

I thought I'd make my first post on here since it's PHP!

 

You should get into using MySQLi (improved MySQL) or PDO. PDO is PHP Data Objects. A lot of the basic mysql functions such as mysql_connect(""); are being depreciated as said above. This means in the latest versions (5.5.0+) they will no longer be accepted.

 

You will need to start using things like:     $sql = new PDO("", username, password);  etc to connect to the database.

Link to comment
Share on other sites

Link to post
Share on other sites

Right thanks for the help

and yeah I know the rest of the application is using MySQLi, dont know why i'm using MySQL here lol, guess cause I had the code from a previous version which I copied and pasted lol

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×