Jump to content

Comparing arrays from db


mfindlay

Recommended Posts

Sorry this might have been double posted??

 

Hi,

 

noobie php coder

 

2 questions...

1. I am displaying results from a database in a webpage using php, the results show in the form that I wish but there are also a lot of error messages displayed with regard to the array_shift function and foreach function, code below..any ideas?

If I split into 2 while loops there are no errors.

while ($rec1 = mysql_fetch_row($answers) OR $rec2 = mysql_fetch_row($submissions))
{
echo ("\t<tr>\n");
echo ("\t\t<th>Correct Answers</th>\n");

array_shift ($rec1);
foreach ($rec1 as $ans)
	echo ("\t\t<td>" . $ans . "</td>\n");

echo ("\t</tr>\n");

echo ("\t<tr>\n");
echo ("\t\t<th>Student submission</th>\n");

array_shift ($rec2);
foreach ($rec2 as $sub)
	echo ("\t\t<td>" . $sub . "</td>\n");
echo ("\t</tr>\n");
}

 

2. I am wanting to compare the 2 arrays to work out a score, so each person has a set of correct answers but might have submitted several times. So I want to check the 2 arrays for the persons id and then compare the two sets of results, I can go through each value and compare but there must be a better way of doing it. eg

if ($rec1[0] == $rec2[0])
     $score = 1;
else
    $score = 0;
--->
if ($rec1[n] == $rec2[n])....

Would sorting the arrays by the person id and then using array_dif be an appropriate way to do this? any help or pointers to info would be much appreciated.

 

Cheers

Link to comment
https://forums.phpfreaks.com/topic/40680-comparing-arrays-from-db/
Share on other sites

question 1 - I take it that you are only pulling 1 row from the database for each $answers or $submissions - if you are pullingmore than 1 row for each then

 while ($rec1 = mysql_fetch_assoc($answers) OR $rec2 = mysql_fetch_assoc($submissions))

 

question 2 - have both $rec1 and $rec 2 have exactly the same amount of entries in each array?

if so then a for loop will work

 

for ($i = 0; $i < count($rec1); $i++)
{
if ($rec1[$i] == $rec2[$i])
{
     $score = 1;
}
else
{
    $score = 0;
}
}

 

Hi,

 

I still get the error message if I use mysql_fetch_assoc

What should be displayed for example is (although there are more fields and how they are displayed can change):

Answers

s01

yes

32

Result

s01

yes

29

Result

s01

no

32

Answers

s02

no

14

Result

s01

yes

13

 

So I can display the arrays on my webpage using:

while ($rec1 = mysql_fetch_row($answers))
{
echo ("\t<tr>\n");
echo ("\t\t<th>Correct Answers</th>\n");

array_shift ($rec1);
foreach ($rec1 as $ans)
	echo ("\t\t<td>" . $ans . "</td>\n");

echo ("\t</tr>\n");

for ($i = 0; $i < count($rec1); $i++)
{
	if ($rec1[$i] == $rec2[$i])
		$score = 1;
	else
    		$score = 0;
}
}

echo ("\t<tr>\n");
echo ("\t<td colspan=\"8\"> </td>");
echo ("\t</tr>\n");

while ($rec2 = mysql_fetch_row($submissions))
{
echo ("\t<tr>\n");
echo ("\t\t<th>Student submission</th>\n");

array_shift ($rec2);
foreach ($rec2 as $sub)
	echo ("\t\t<td>" . $sub . "<br />" . $score . "</td>\n");

}

 

and it displays correctly as example above (or as near as!!), but when I use previous code:

 

while ($rec1 = mysql_fetch_row($answers) OR $rec2 = mysql_fetch_row($submissions))

 

i get error messages regarding the roreach loops and the array_shift functions but the table displays correctly below messages eg

 

"Warning: array_shift() [function.array-shift]: The argument should be an array in D:\Program Files\xampp\htdocs\assessment\admin\get_results.php on line 34

 

Warning: Invalid argument supplied for foreach() in D:\Program Files\xampp\htdocs\assessment\admin\get_results.php on line 35"

 

So there will be more values in the submission array - or at least I think so - is each row classed as a new array or is it like a 2D array?

Anyway hope this clarifies what I am trying to do.

 

Cheers.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.