Working on a quiz website where when users submits their answers $perQuestionScore equals true when the answer is correct but false when it is wrong. I'm trying to find the instances where $perQuestionScore equals true in other to total the score but it doesn't seem to work.
My code looks like below
<?php
$perQuestionScore = 0;
if (isset($_POST['grader'])) {
if(isset($_POST[$chosen]))
{
$choice= $_POST[$chosen];
if (strpos($choice, $correctOne) !== false) {
$perQuestionScore++;
echo $_POST[$chosen] . "" . " is the correct answer";
} elseif (strpos($choice, $correctOne) == false) { echo $_POST[$chosen] . "" . " is the Wrong answer";
} else {
echo "You did not choose an answer"; {
}
}
}
}
}
echo "<input id=grader' type='submit' name='grader' value='Grade Quiz'>" . "</form>";
echo $perQuestionScore * 10;
}
$conn->close();
?>
I suggest simplifying it to this:
<?php
$perQuestionScore = 0; //initialize var
if ($choice == $correct) {
$perQuestionScore++; //add 1 if correct
}
echo $perQuestionScore * 10; //i guess you want it times 10
Related
i'm sorry if my english is not perfect but i will explain the situation with my best, basically my goal is to disable/hide the radio button if the previous loop value of status is pending, below is my code
$allPaid = true
while($row2 = mysqli_fetch_assoc($sql2Q))
{
$bill = $row2['billPay_id'];
$amount = $row2['billPay_amount'];
$interest = $row2['billPay_interest'];
$payAmount = $row2['billPay_amountPaid'];
$payInterest = $row2['billPay_interestPaid'];
$makeUp = $row2['billPay_makeUp'];
$billStatus = $row2['billPay_status'];
$date = $row2['billPay_collectOn'];
$date = date("d-m-Y",strtotime($date));
$PayDate = $row2['billPay_payOn'];
if($PayDate == "0000-00-00 00:00:00")
{
$PayDate = "";
}
else
{
$PayDate = date("Y-m-d",strtotime($PayDate));
}
if($billStatus == 0)
{
$status = "Pending";
}
else
{
$status = "Paid";
}
echo "
<tr>
<form>
<td>$no</td>
<td>$date</td>
<td>$PayDate</td>
<td>$amount</td>
<td>$payAmount</td>
<td>$payInterest</td>
<td>$makeUp</td>
<td>$status</td>
";
if(($status == "Pending")&&($allPaid == true))
{
echo "<td><input type='checkbox' name='colDate' value='$date' onclick='payData()'></input></td>";
$allPaid = false;
}
else
{
echo "<td></td>";
$allPaid = true;
}
echo " </form>
</tr> ";
$date = date("y-m-d",strtotime($date));
$no++;
}
what i am doing right now is making a payment system where customer need to pay the bill before able to pay the next bill, so before paying the current bill the next bill radio button will not appear or hidden, in this system the radio button is act as selector,is there any way to achieved that? any help will be very helpful, thank you
UPDATED
i have update the code based on Anggara suggestion, but the output is a little bit off
If your page showing only one bill for one person, you can add a variable indicating the person has paid all previous bills.
$allPaid = true; // declare here
while($row2 = mysqli_fetch_assoc($sql2Q))
{
$bill = $row2['billPay_id'];
$amount = $row2['billPay_amount'];
$interest = $row2['billPay_interest'];
$payAmount = $row2['billPay_amountPaid'];
$payInterest = $row2['billPay_interestPaid'];
$makeUp = $row2['billPay_makeUp'];
$billStatus = $row2['billPay_status'];
$date = $row2['billPay_collectOn'];
$date = date("d-m-Y",strtotime($date));
$PayDate = $row2['billPay_payOn'];
if($PayDate == "0000-00-00 00:00:00")
{
$PayDate = "";
}
else
{
$PayDate = date("Y-m-d",strtotime($PayDate));
}
if($billStatus == 0)
{
$status = "Pending";
$allPaid = false; // there is a bill unpaid
}
else
{
$status = "Paid";
}
echo "
<tr>
<form>
<td>$no</td>
<td>$date</td>
<td>$PayDate</td>
<td>$amount</td>
<td>$payAmount</td>
<td>$payInterest</td>
<td>$makeUp</td>
<td>$status</td>
";
if($status == "Pending" && $allPaid) // show only if previous bills are paid
{
echo "<td><input type='checkbox' name='colDate' value='$date' onclick='payData()'></input></td>";
}
else
{
echo "<td></td>";
}
echo " </form>
</tr> ";
$date = date("y-m-d",strtotime($date));
$no++;
}
If i understand correctly, this should do what you want
replace
if($status == "Pending")
{
echo "<td><input type='checkbox' name='colDate' value='$date' onclick='payData()'></input></td>";
}
else
{
echo "<td></td>";
}
with
echo "<td><input type='checkbox' name='colDate' value='$date' onclick='payData()' ". (($status == 'Pending')? 'disabled':'') ."></input></td>";
if you want to hide it you can replace disabled with hidden
So I have this foreach loop that checks if $testsubject is equal to the results from the array.
But I want it to check all the results first and if one is true than go further and check the date and else just echo that the voucher is not corect.
the purpose of the code is that the user puts in a voucher code which for now is $testsubject than I check if the voucher exists in the system if that is true I check if it is not expired with the date function and then I cut the discount for the price $testamount.
image of the echo's https://imagebin.ca/v/3xQuiAClVsAG
index.php
function display()
{
$arrContextOptions = [
"ssl" => [
"verify_peer" => false,
"verify_peer_name" => false,
],
];
$getVoucherList = "https://www.planyo.com/rest/?method=list_vouchers&api_key=yourkey&resource_id=110556";
$cleanVoucherList = preg_replace("/ /", "%20", $getVoucherList);
$voucherlist = file_get_contents("$cleanVoucherList", false, stream_context_create($arrContextOptions));
$voucherList = json_decode($voucherlist, true);
$testsubject = "TESTVOUCHER";
$testamount = "5,00";
foreach ($voucherList['data']['results'] as $testVoucher => $testVoucherArr) {
if ($testsubject == $testVoucherArr['code']) {
echo $testsubject . " is not equal to " . $testVoucherArr['code'] . "<br>";
echo $testVoucherArr['rental_end_date'] . "<br>";
echo $testVoucherArr['discount_value'] . "<br>";
if (date("Y-m-d") <= $testVoucherArr['rental_end_date']) {
echo "this code can be used <br>";
echo $testamount - $testVoucherArr['discount_value'] . "<br>";
} else {
echo "this code cannot be used";
}
;
} else {
echo $testsubject . " is not equal to " .
$testVoucherArr['code'] . "<br>";
}
}
}
if (isset($_POST['submit'])) {
display();
}
Would this work for you? If the code is valid, then you enter a function which checks the date. After the function has ended, the foreach loop will end by using "break;"
function testVoucherDate($voucher)
{
if (date("Y-m-d") <= $testVoucherArr['rental_end_date']) {
echo "this code can be used <br>";
echo $testamount - $testVoucherArr['discount_value'] . "<br>";
} else {
echo "this code cannot be used";
};
}
foreach ($voucherList['data']['results'] as $testVoucher => $testVoucherArr) {
if ($testsubject == $testVoucherArr['code']) {
echo $testsubject . " is not equal to " . $testVoucherArr['code'] . "<br>";
echo $testVoucherArr['rental_end_date'] . "<br>";
echo $testVoucherArr['discount_value'] . "<br>";
testVoucherDate($testVoucherArr);
break;
} else {
echo $testsubject . " is not equal to " .
$testVoucherArr['code'] . "<br>";
}
}
EDIT: I've put the function above the loop, so no errors of undefined functions will occur
First set a flag to true, then loop setting the flag to false if there is an error. Then test the flag:
$flag = true; // SET A FLAG
foreach($a as $b){
if($b !== 'Hello')$flag = false; // IF contidtion not met, set flag to true
}
if($flag === false){ // TEST IF flag result
echo 'Dear oh dear';die;
}
foreach(....){ // GO ON if flag === true
....
}
I'm trying to use a multiple foreach and if statements to give me a list of list of people that have not been matched. I have the below code, I am able to get this to successfully give me a list of people it does match.
What I want to do is it echo each the ID from the $tenant_id foreach that have not been found in the $value2 foreach, am I doing something wrong? It will only output nothing?
foreach($array_93 as $value) {
$tenant_id = $value['id'];
$limit = 0;
foreach($obj->response->entries as $value2) {
if($limit==1) break;
if ($value2->{100} == 'true' && $value2->{114} == $tenant_id)
{echo $value['id']; // This should echo ID's that have not been found.}
$limit++;
}
}
};
UPDATE >>
After continuing to try and get this working I have got to this point, I am able to to use this to show which ID's are all 'n' as per screenshot after. The first one is all n's so this has not matched, how can I now make just the ones with all n's ID show?
foreach($array_93 as $value) {
echo '<b>'.$value['id'].'</b>';
echo '<br />';
foreach($obj->response->entries as $value2) {
if (strpos($value2->{114}, $value['id']) === false)
{
echo '<i>n</i>';
} else {
echo '<b>Y</b>';
}
}
echo '<br />';
};
Use a flag with Y-found state:
foreach($array_93 as $value) {
$Yfound = false;
foreach($obj->response->entries as $value2) {
if (strpos($value2->{114}, $value['id']) !== false) {
$Yfound = true;
}
}
if(!$Yfound) {
echo $value['id'] . ' has n`s only<br>';
}
}
Hi you should not echo right away :
foreach($array_93 as $value) {
//echo '<b>'.$value['id'].'</b>';
//echo '<br />';
//don't print yet
$output = ""; //this to store your n and Y strings.
$n = 0; //Here you store the number of times Y appears
foreach($obj->response->entries as $value2) {
if (strpos($value2->{114}, $value['id']) === false)
{
$output .= '<i>n</i>';//concatenating
} else {
$output .= '<b>Y</b>';
$n++;
}
}
//then test if there is a y and echo output.
if($n == 0){
echo '<b>'.$value['id'].'</b>';
echo '<br />';
echo $output;
echo '<br />';
}
};
I am trying to make a textbased game, but I can't seem to figure out of why this code down is not working.
It has a mysqli connection in the core file.
It has values for the chances in the database.
Rank is set in the database.
I don't get any errors, only the "Success" and "Failure" messages not showing up.
Code:
<?php
include_once "core.php";
function checkRandom($chance){
return rand(1, 100) <= (int)$chance;
}
$userid = '1';
$getAllQuery = $data->query("SELECT * FROM players WHERE id = '$userid'") or die($data->error);
while ($getall = $getAllQuery->fetch_assoc()) {
$rank = $getall['rank'];
$chance1 = $getall['crime_chance1'];
$chance2 = $getall['crime_chance2'];
$chance3 = $getall['crime_chance3'];
$chance4 = $getall['crime_chance4'];
$chance5 = $getall['crime_chance5'];
$chance6 = $getall['crime_chance6'];
}
if (isset($_POST['crime'])) {
$choice = $_POST['crime'];
$pass = 0;
$fail = 0;
if ($choice == 1 && $rank <= 1) {
echo "3esd";
if (checkRandom($chance1)) {
echo "Success";
} else {
echo "Failure";
}
} else {
if ($choice == 2 && $rank <= 2) {
if (checkRandom($chance2)) {
echo "Success";
} else {
echo "Failure";
}
}
}
}
echo "<form method='POST' action='#'>";
if ($rank >= 1) {
echo "<label><input type='radio' name='crime' value='1'>Crime 1 " . $chance1 . "% chance</label><br />";
if ($rank >= 2) {
echo "<label><input type='radio' name='crime' value='2'>Crime 2 " . $chance2 . "% chance</label><br />";
if ($rank >= 3) {
echo "<label><input type='radio' name='crime' value='3'>Crime 3 100% chance</label><br />";
}
}
}
echo "<input type='submit'>
</form>";
?>
I would really appreciate some help. :D
This code seems to work fine except under the following cases:
1) Nothing is handling Rank 3+ as the IF statements are <= 1 or <= 2 and 3 is > then 1 and 3.
2) If you select the first option, and you are not rank 1, the code won't return anything, because the first IF statement is limited to the selected choice being 1 AND the Rank being <= 1.
3) If you select the second option, and you are not rank 2, the code won't return anything, because the second IF statement is limited to the selected choice being 1 AND the Rank being <= 2.
see the following addition
}ELSE{ # condition not handled
ECHO "WE ARE HERE";
if ($choice == 1 && $rank <= 1) {
echo "3esd";
if (checkRandom($chance1)) {
echo "Success";
} else {
echo "Failure";
}
} else {
if ($choice == 2 && $rank <= 2) {
if (checkRandom($chance2)) {
echo "Success";
} else {
echo "Failure";
}
}ELSE{ # condition not handled
ECHO "WE ARE HERE";
}
}
I asked a question yesterday which was solved and I can now output WHILE loops which 2 results on a line, rather than 1 on a line. This is the code I've got at the moment:
if(mysql_num_rows($result2) > 0) {
$count=0;
$day = 1;
while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
echo "<b>";
if ($day=='1') { echo "Sunday - ";}
else if ($day=='3') { echo "Monday - "; }
else if ($day=='5') { echo "Tuesday - "; }
else if ($day=='7') { echo "Wednesday - "; }
else if ($day=='9') { echo "Thursday - "; }
else if ($day=='11') { echo "Friday - "; }
else if ($day=='13') { echo "Saturday - "; }
else { echo "";}
if (($row["open"] == 0) && ($row["close"] == 0))
{
echo "closed"
}
else
{
echo "</b>" . $row["open"] . "-" . $row["close"] . " ";
if ($count % 2 !=0 ){ echo "<br/><br/>";}
}
$count++;
$day++;
}
} else {
echo "Error";
}
With this code, when the branch is closed, then it returns the word 'closed' twice, where I need it to appear only once. Is this possible?
Just test on modulo the value within $day :
in place of :
if (($row["open"] == 0) && ($row["close"] == 0)) {
echo "closed";
}
do :
if ($row["open"] == 0 && $row["close"] == 0) {
if ($day % 2 == 1) {
echo "closed";
}
}
It may not be the most elegant solution but I would probably have a variable $closed = false, which gets set to true if the branch is closed. Then just test for $closed, if it's false, output "closed".
Use a for loop:
for($i=0`;`$i`<`count(row)`;`$i++)
{
}