Is there any way something like this possible? - php

<?php
$division=$row['mark'];
$pass="Passed";
if($division>=80 && $pass==include "result.php")// Result.php has two value: one is `Pass` and the other is `Fail`.
{
echo "Letter";
}
elseif($division>=70 && $pass==include "result.php")
{
echo "First";
}
else
{
echo "Fail";
}
?>
What I want to output here is: if $division is equal to 80 and at the same time if $pass is equal to Passed, echo Letter. But if $division is less than 70, echo Fail; also $pass here equals to fail which is taken from result.php. I have been trying to output it with following code, but it does not work. It outputs FailFailFailFail when $division is less than 70.
Code for Result.php
<?php
$eng=40;
$mizo=40;
$hindi=40;
$maths=40;
$ss=40;
$science=40;
if ($eng>=40 && $mizo>=40 && $hindi>=40 && $maths>=40 && $ss>=40 && $science>=40)
{
echo "<font color=green>Passed</font>";
}
else
{
echo "<font color=red>Failed</font>";
}
?>

You are going about this all the wrong way. You can't compare the results of an include like that, not to mention they don't match properly anyway as you're comparing a single word string against a string with a whole bunch of HTML in it.
A better way to do it is to include results.php and store your answer in a variable. I have written an example below.
First off you need to change result.php to:
<?php
$eng=40;
$mizo=40;
$hindi=40;
$maths=40;
$ss=40;
$science=40;
if ($eng>=40 && $mizo>=40 && $hindi>=40 && $maths>=40 && $ss>=40 && $science>=40)
{
$test = "Passed";
}
else
{
$test = "Failed";
}
?>
Then you put the following in the first file:
<?php
$division=$row['mark'];
$pass="Passed";
include("result.php");// Result.php has two value: one is `Pass` and the other is `Fail`, store in $test.
if($division>=80 && $pass==$test)
{
echo "Letter";
}
elseif($division>=70 && $pass==$test)
{
echo "First";
}
else
{
echo "Fail";
}
?>

Something like this will do. For your result.php, use the following:
<?php
$eng= 40;
$mizo= 40;
$hindi= 40;
$maths= 40;
$ss= 40;
$science= 40;
// first group your variable into one array = $all
$all = array($eng, $mizo, $hindi, $maths, $ss, $science);
// second, just iterate over them till you find one value -40
for($i=0; $i < count($all); $i++){
if($all[$i] < 40) $sum = 1;
}
?>
For output:
<?php include "result.php";?>
<?php
$division=$row_['mark'];
$pass="Passed";
$test = (!empty($sum)) ? 'Failed' : 'Passed';
if($division>=80 && $pass==$test)
{
echo "Letter";
}
elseif($division>=70 && $pass==$test)
{
echo "First";
}
else
{
echo "Passed";
}
?>

You need to include the file first:
<?php
include "result.php"; //include the file
$division =$ row['mark'];
$pass = "Passed";
if($division == 80 && $pass == "Passed") {
echo "Letter";
}
elseif($division < 70) {
echo "Fail";
}
?>

Related

Why is this if condition not working fine?

$isPass should be "Go to hell", which should differ from 0, yet, the code proves otherwise. Why?
<?php
$marks = 35;
$passing_marks = 36;
$isPass = ($marks > $passing_marks) ? true : "Go to hell";
echo $isPass; // prints "Go to hell".
if($isPass == 0){
echo "Pass"; // prints "Pass", but why and how
}
?>
Just try this.
<?php
$marks = 35;
$passing_marks = 36;
if($marks > $passing_marks){
echo "Pass";
}
else {
echo "Go to hell";
}
?>
You don't need the other variable, just execute the check on one if-statement.

How limit user actions in higher/lower game

Well, I have created a simple higher/lower game script. Its working fine, but I want to limit the user actions to 3. This means that the user will have to guess the number with 3 moves. On third action I will execute query and save the user to mysql. How I can limit the actions/moves?
<?php
session_start();
function Start_Again() {
$number = rand(1,100);
$_SESSION['higherlower'] = $number;
echo "Select a Number below.";
Display_Form();
}
function Display_Form() {
echo "<table>";
for ($num=1;$num < 101;$num++) {
if (!preg_match("/(.*?)0/", $num)) { echo "<td><a href=\"?number=".$num."\">".$num."</td>"; }
else { echo "<td><a href=\"?number=".$num."\">".$num."</td></tr><tr>"; }
}
echo "</table>";
}
if (isset($_GET['number'])) {
$User_Number = $_GET['number'];
$Actual_Number = $_SESSION['higherlower'];
$count = 0;
if ($User_Number < $Actual_Number) { echo "Higher"; $count + 1; Display_Form(); }
elseif ($User_Number > $Actual_Number) { echo "Lower"; $count + 1; Display_Form(); }
elseif ($User_Number == $Actual_Number) { echo "Bingo, Correct Guess!<br>"; Start_Again(); }
echo $count;
}elseif (!isset($_POST['higherlower'])) { Start_Again(); }
?>
When you initialize the game, simply store the amount of tries in your session.
$_SESSION['tries'] = 3;
Then, when the user picks a number, lower the tries and check if it's 0.
$_SESSION['tries']--;
if ($_SESSION['tries'] <= 0) {
die("Enough! You've been clicking numbers all afternoon.");
}
Full Implementation
<?php
/**
* Higher-lower game
*/
if (!isset($_SESSION)) {
session_start();
}
function Start_Again() {
$number = rand(1,100);
$_SESSION['higherlower'] = $number;
$_SESSION['tries'] = 3;
echo "Select a Number below.";
Display_Form();
}
function Display_Form() {
echo "<table>";
$chunks = array_chunc(range(1, 100), 10);
foreach ($chunks as $chunk) {
echo "<tr>";
foreach ($chunk as $num) {
echo "<td><a href=\"?number=".$num."\">".$num."</td>";
}
echo "</tr>";
}
echo "</table>";
}
if (isset($_GET['number'])) {
$User_Number = $_GET['number'];
$Actual_Number = $_SESSION['higherlower'];
if ($_SESSION['tries'] <= 0) {
die("Oops! You're bad at this!");
}
if ($User_Number < $Actual_Number) { echo "Higher"; $count + 1; Display_Form(); }
elseif ($User_Number > $Actual_Number) { echo "Lower"; $count + 1; Display_Form(); }
elseif ($User_Number == $Actual_Number) { echo "Bingo, Correct Guess!<br>"; Start_Again(); }
$_SESSION['tries']--;
echo $_SESSION['tries'] . 'chances left';
} elseif (!isset($_POST['higherlower'])) {
Start_Again();
}
You are already starting a session so you can store the value in a session variable i.e. $_SESSION['count'];
session_start();
if( !isset( $_SESSION['count] ) ) $_SESSION['count'] = 1;
Later on in the code you will need to update the counter $_SESSION['count']++;
And check whether the person has used up all the guesses
if( $_SESSION['count'] > 3 ) { ..... }

How to execute php if condition once time only

How to execute php if condition once time only and didn't check it again, i put if condition in for loop and i want to check it one time only:
<?php
for($x=0;$x<3;$x++){
if($x == $x){
echo "";
}
else{
echo "hidden";
}
}
I need to execute first if one time only in for loop
I need to execute first if one time only in for loop. Well that's not how you do it, you need to compare with a variable and upon success just use break to come out of the loop. Something like:
<?php
$y = 0;
for($x=0; $x<3; $x++){
if($x === $y){
echo "X is Equal to Y";
break;
}
else {echo "hidden";}
}
echo "<br>". "You're outside the loop!";
?>
Set a flag, if flag is true don't execute if condition.
<?php
$flag = 0;
for($x=0;$x<3;$x++) {
if($x == $x && $flag == 0){
echo "";
$flag = 1;
}
else{
echo "hidden";
}
}

Best way to check 3 variable values equals to zero inside if condition - PHP

I am checking 3 variables are equals to zero inside if condition , currently i am doing some thing like this
if($diff_colour_code==0 && $diff_upholstery_code==0 && $big_diff==0 )
is there any better way to do this
I am thinking a way like
if($diff_colour_code==$diff_upholstery_code==$big_diff==0 )
Please help , Thanks in advance .
This should work for you:
You can give the function as many arguments as you need!
<?php
$diff_colour_code = 0;
$big_diff = 0;
$diff_upholstery_code = 0;
function zeroCheck() {
foreach(func_get_args() as $arg)
if($arg == 0)
continue;
else
return false;
return true;
}
if (zeroCheck($diff_colour_code, $big_diff, $diff_upholstery_code))
echo "all are 0!";
?>
you can use ! as :
if(!$diff_colour_code && !$diff_upholstery_code && !$big_diff )
You could do something like this:
$var1 = 0;
$var2 = 0;
$var3 = 0;
$array = compact("var1", "var2", "var3");
$countValues = array_count_values($array);
if($countValues[0] == count($array)){
echo "yes";
}else{
echo "no";
}
or this
if(($var1 == 0 && $var1 == $var2 && $var2 == $var3)){
echo "yes";
}else{
echo "no";
}

PHP code how to clean user input from $_POST

I have this PHP code:
<?php
$score11 = $_POST['passmarks12'];
if($_POST['passmarks12'] > 100){
$grade11 = "";
}
elseif ($_POST['passmarks12'] < 45){
$grade11 = "Fail";
}
$strg = " $grade11";
echo $strg;
?>
The code is always printing "Fail" regardless of what is sent in.
I want it so that if it passes in blank or invalid input it fails.
And how should I properly cleanse the input?
Try this:
<?php
//$_POST['passmarks12'] = '';
if(empty($_POST['passmarks12']) || $_POST['passmarks12'] > 100)
{
$grade11 = "";
}
else if ($_POST['passmarks12'] < 45){
$grade11 = "Fail";
} else{
$grade11 = "Pass";
}
$strg = " $grade11" ;
echo $strg;
?>
Points:
Check if $_POST['key'] exists using isset.
Check if $_POST['key'] has a valid data type, string.
It may come as array.
Check if $_POST['key'] has a valid numeric format.
Compare $_POST['key'](String) and 45(Integer), using intval.
Example:
<?php
switch (true) {
case !isset($_POST['passmarks12']):
case !is_string($score = $_POST['passmarks12']):
case !is_numeric($score):
$result = 'Error (Invalid parameter)';
break;
case (intval($score) < 45):
$result = 'Fail (Less than 45)';
break;
default:
$result = 'Success (No less than 45)';
}
echo $result;
I think you want
elseif ($_POST['passmarks12'] < 45 && !empty($_POST['passmarks12']))
This PHP code will make sure $_POST is not empty before comparing it to 100.
<?php
$score11 = $_POST['passmarks12'];
if(empty($_POST['passmarks12']) || $_POST['passmarks12'] > 100)
$grade11 = "";
elseif ($_POST['passmarks12'] < 45)
$grade11 = "Fail";
$strg = " $grade11" ;
echo $strg;
?>

Categories