Jump to content

No errors being reported but code won't work properly.


morocco-iceberg

Recommended Posts

I've been able to fix everything that causes errors from the code... anything that reports an error anyway, but my code still won't work. The first section is a function to mark the user input and see if they have selected the correct words in the correct order. This code is located on the page following the page on which the function sent() outputs the questions the user is required to answer.

 

I don't receive any error messages, but it does not add up the score, which is what I need it to do.

I'm really lost with this.

 

Function that marks the user input:

function mark($table){
$counter = 0;
while ($counter<2){
	$id = $_POST[$table."_q".$counter];
	$names = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t");
	foreach($names as $identifier => $letter){
		$sql = mysql_query("SELECT * FROM $table WHERE id = '{$id}'") or die($error);
		while($row = mysql_fetch_array($sql)){
			$$letter = $row["$letter"];
		}
		if (isset($_POST[$table.'_a'.$counter.'_0'])){
			${'a'.$letter} = mysql_real_escape_string($_POST[$table.'_a'.$counter.'_0']);
		}else{
			${'a'.$letter} = "Undefined";
		}
		${'c'.$letter} = strcmp(${'a'.$letter}, $$letter);
	}
	if($ca == 0 && $cb == 0 && $cc == 0 && $cd == 0 && $ce == 0 && $cf == 0 && $cg == 0 && $ch == 0 && $ci == 0 && $cj == 0 && $ck == 0 && $cl == 0 && $cm == 0 && $cn == 0 && $co == 0 && $cp == 0 && $cq == 0 && $cr == 0 && $cs == 0 && $ct == 0){
		global $score;
		$score++;
	}else{
		global $score;
		$score = $score;
	}
$counter++;
}
}

Function that outputs the dropdown boxes for the user input:

function sent($table){
$sql = mysql_query("SELECT * FROM $table ORDER BY RAND()") or die($error);
$counter = 0;
while($row = mysql_fetch_array($sql)){
	$id[$counter] = $row["id"];
	$a[$counter] = $row["a"];
	$b[$counter] = $row["b"];
	$c[$counter] = $row["c"];
	$d[$counter] = $row["d"];
	$e[$counter] = $row["e"];
	$f[$counter] = $row["f"];
	$g[$counter] = $row["g"];
	$h[$counter] = $row["h"];
	$i[$counter] = $row["i"];
	$j[$counter] = $row["j"];
	$k[$counter] = $row["k"];
	$l[$counter] = $row["l"];
	$m[$counter] = $row["m"];
	$n[$counter] = $row["n"];
	$o[$counter] = $row["o"];
	$p[$counter] = $row["p"];
	$q[$counter] = $row["q"];
	$r[$counter] = $row["r"];
	$s[$counter] = $row["s"];
	$t[$counter] = $row["t"];
	$counter++;
}
$counter = 0;
while($counter<2){
	echo "<input type=\"hidden\" name=\"".$table."_q".$counter."\" value=\"".$id[$counter]."\">";
	${$table.'_a'.$counter} = array("$a[$counter]", "$b[$counter]", "$c[$counter]", "$d[$counter]", "$e[$counter]", "$f[$counter]", "$g[$counter]", "$h[$counter]", "$i[$counter]", "$j[$counter]", "$k[$counter]", "$l[$counter]", "$m[$counter]", "$n[$counter]", "$o[$counter]", "$p[$counter]", "$q[$counter]", "$r[$counter]", "$s[$counter]", "$t[$counter]");	
	${$table.'_c'.$counter} = ${$table.'_a'.$counter};
	shuffle(${$table.'_a'.$counter});
	echo "<table border=\"1\" bordercolor=\"#000000\" rules=\"none\" frame=\"box\" cellpadding=\"5\"><tr>";
	$number = 0;
	while($number<20){
		if(!${$table.'_a'.$counter}[$number]){
			echo "";
		}else{
			echo "<td>".${$table.'_a'.$counter}[$number]."</td>";
		}
		$number++;
	}
	echo "</tr></table><br/><table><tr>";		
	foreach(${$table.'_c'.$counter} as $identifier => $column){
		if(empty($column)){
			break;
		}else{
			echo "<td><select name=\"".$table."_a".$counter."_".$identifier."\"><option value=\"0\"></option>";
			$number = 0;
			while($number<20){
				if(!${$table.'_a'.$counter}[$number]){
					echo "";
				}else{
					echo "<option value=\"".${$table.'_a'.$counter}[$number]."\">".${$table.'_a'.$counter}[$number]."</option>";
				}
				$number++;
			}
			echo "</select></td>";
		}
	}
echo "</tr></table><br/>";
$counter++;
}
}

Link to comment
Share on other sites

that is progress

 

you have double $ there and $letter is in double quotes where it's not necessary, thus its slower

I think it's also better to use foreach instead of while

while($row = mysql_fetch_array($sql)){
			$$letter = $row["$letter"];
		}

Link to comment
Share on other sites

I changed that line of code to what you recommended again, but this time it didn't cause any errors. I've just woken up so I'm guessing it was a typo that caused them.

 

I've taken out the double quotes around the $row["$letters"], didn't realise I hadn't before so cheers for picking that up.

 

The $$letters are variable variables, so those should be fine as they are.

 

The score still isn't adding up though.

 

And I'm not exactly sure how to change that specific while loop into a foreach loop...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.