Jump to content

[SOLVED] Re: Novice Help needed in php Arrays


edster1980

Recommended Posts

Hi all i am currently studying a course in programing/scripting and could use a hand with something i have to work on I have a very simple array and within one line i inserted another array and need to check that both values are sent from the form but it seems to pass with just one ill cut and past the small parts of code that i think will help see the problem and i apologies as it will probably be like asking most people here to Tye there shoes but I'm desperate!! OK ill show my array and just the if statement relating to my problem please let me no if this is not enough and i can supply anyone willing to help with more info. Thanks HEAPS in advance i will pass the site on to some more of the class if successfull.

$correctanswer = array();
//put the correct answers into an array

$correctanswer[1] = "javascript";
$correctanswer[2] = array
(
"number2"=>array
  (
  "2a",
  "2c"

  )

);
$correctanswer[3] = "3";
$correctanswer[4] = "2";
$correctanswer[5] = "1";
$correctanswer[6] = "2";
$correctanswer[7] = "2";
$correctanswer[8] = "2";
$correctanswer[9] = "2";
$correctanswer[10] = "2";

and the if statement

$message .=  "<tr><td>2</td><td>";

if($q2 == $number2[2] . $number2[1])
{
$message .=  "Correct";
$mark = $mark + 1;  //add a mark for correct answer

}
else
{
$message .=  "Wrong Answer";
}
$message .=  "</td></tr>";

apologies for my post if its messy again I'm new i was thinking there must be a way to say that $q2 must be equal to or == those 2 but not these two checked again any help is much appreciated i have a live copy of this on a home server to see it working let me no. Thanks Again

Edster1980.

 

Update: Sorry again there is a made up test with a check box question and it has 2 correct answers as stored in number2 array and of course 2 incorrect answers but with the above code if i answer with just one either the 2a or 2c on the HTML form it passes as OK when i would like it to say it is wrong. Let me know if this is still very unclear as it will be me not anyone else. Thanks for the super fast response.

Link to comment
Share on other sites

2a and 2c are in the following (example):

 

echo $correctanswer[2][0]; // 2a

echo $correctanswer[2][1]; // 2c

 

When you do

 

if($q2 == $number2[2] . $number2[1])

 

you are comparing $q2 to nothing. 

 

actually, my bad.  they are in

 

echo $correctanswer[2]['number2'][0]; // 2a

echo $correctanswer[2]['number2'][1]; // 2c

 

Link to comment
Share on other sites

hi thanks for response i think I'm being a newb at explaining myself q2 comes from a form that is posted from a HTML page and i am trying to validate the answers given in a check box question named q2 with 4 values being 2a 2b 2c 2d there are only two correct values these are 2a and 2c which was my reason for the if $q2 statement I'm sorry if this is just confusing the matter i can zip and attach the files if needed the problem that i am running into again is that if i answer with just one of the correct answers it is said to be correct when it should be similar to the javascript if statement of

of course i have declared everything and forms[0] is f i no this is not php but it is what i think i am looking for!

   if (f.q2a.checked && f.q2c.checked && !f.q2b.checked && !f.q2d.checked)
   {
      correct=correct+1;
   }
   else
   {
      alert("Question 2 answer is wrong");
   }

Link to comment
Share on other sites

that is mostly correct appart from the $q2 comes from the html form i made Yes it has as you said Crayon Violent four values 2a-2d i only passed the 2 correct ones into the array number2. i will attach a copy of the files including the php that i am having problems with i hope this helps and isnt seen as rude.

 

[attachment deleted by admin]

Link to comment
Share on other sites

if I were you I would seperate the questions into different pages. I would create a code something like this:

if (isset($_GET['id] && is_numeric($_GET['id'])) {
  $i = $_GET['id'];
echo $question[$i];
if ($answer[$i] == $guess[$i]) {
//correct
} else {
// fail
}
} else {
echo 'page not found';
}

of course you would need to change it up a little (I gave myself a minute to finish)

 

Link to comment
Share on other sites

hi everyone thanks to all the replyed but none of that code seems to fit in with the code i attached in the file on a post above. I am still trying to get the answer elseware but no luck i understand now that checkboxes sent from a html form must have values on the form but the php is still only checking that one is checked not 2 out of the 4. I think 90% is better then nothing. thanks again

 

Mr. Confused.

Link to comment
Share on other sites

Example:

 

Pick the correct 2-answer combo:<br />
<form action = '' method = 'post'>
   <input type = 'checkbox' name = 'question[]' value = 'a'> a <br />
   <input type = 'checkbox' name = 'question[]' value = 'b'> b <br />
   <input type = 'checkbox' name = 'question[]' value = 'c'> c <br />
   <input type = 'checkbox' name = 'question[]' value = 'd'> d <br />
   <input type = 'submit' value = 'submit'>
</form>

<?php
if ($_POST) {
   $answer = array('a','c');
   if ($_POST['question'] == $answer) {
      echo "<br/>correct";
   } // end if
} // end if
?>

Link to comment
Share on other sites

Success i now need to work out how to mark this post as SOLVED again thanks to all that responded the help has been invalubale that last post worked a treat i had also worked another way around the problem but in a very similar way to the above post anyone reading this looking for help be it simple or advanced this is the place to post!! thanks again all this topic for me is now Closed! thanks again.

 

Mr Thinks he's a genius now cause he can use a simple array NOT!! :)

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.