Hello, I am retrieving code from a database and my resulting array is as below.

Array (
[0] => Array ( [0] => False [1] => True ) 
[1] => Array ( [0] => True [1] => False ) 
[2] => Array ( [0] => False [1] => True ) 
[3] => Array ( [0] => True [1] => False ) 
[4] => Array ( [0] => False [1] => True ) 
[5] => Array ( [0] => True [1] => False ) 
[6] => Array ( [0] => True [1] => False ) 
[7] => Array ( [0] => False [1] => True ) 
[8] => Array ( [0] => False [1] => True ) 
[9] => Array ( [0] => True [1] => False ) 
[10] => Array ( [0] => False [1] => True ) 
[11] => Array ( [0] => True [1] => False ) 
[12] => Array ( [0] => True [1] => False ) 
[13] => Array ( [0] => False [1] => True ) 
[14] => Array ( [0] => True [1] => False )

Can someone please explain a script on how I can access the first answer of each array element. e.g.
[0],[0]
[1],[0]
[2],[0]
[3],[0]
[4],[0]

This is really bugging me, I need to compare each of the above elements with a form variable of true or false??

My code is below, please ask any questions if something is not clear?
Many thanks
James

$connection = mysql_connect("localhost","root","root") or die("couldn't connect");      
$select = mysql_select_db("login") or die ("cannot select database!");   

 $query = mysql_query("SELECT answer FROM questions");      

$numrows = mysql_num_rows($query); //count the number of rows {45}        
 if ($numrows!=0)       //if number of rows does not equal 0
 {
	$count = 0; //counter set to 0
while($row = mysql_fetch_assoc($query))    //while a row exists 	  
	{    		            
	$correctanswer[$count] =  explode('|',$row['answer']);    //   //get answer    row from database and place in $correctanswer to get correct answer
    	
		$count++;//increment the counter			
		//print_r($correctanswer[$count]);		
	}
	print_r($correctanswer); 	
	echo $correctanswer['1,0'];									
	
  }

Recommended Answers

All 4 Replies

this is idea i think it can help you

for($i=1;$i<=4;$i++)
{
if(Your condition ~ $array[$i][0]);
}

thanks a lot for that but I get a syntax error when using the ~ symbol. Any ideas? I've tried using = but then that i get answer correct printed multiple times.
p.s the above file is included using the require_once statement.

if (isset($_POST['submitted'])){ //if submitted 
 			
					
							for($i=0;$i<=50;$i++){
							if (isset($_REQUEST['question-' . $i])){ //if answer is set request each answer and question id
 							$testvar = $_REQUEST['question-' . $i]; //place each answer in testvar
 							require_once('quizdatabaseresults.php');
								for($i=1;$i<=4;$i++)
								{
									if($testvar ~ $correctanswer[$i][0]);
										{ 		echo "answer correct";}
										
								}
}}}

Try this:

foreach($correctanswer as $answers){
	if($answers[0] == TRUE){
		echo "Your answer is true.";
	}
}

no luck with that either Zero13, thanks for trying though. I get an invalid argument error for foreach with the code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.