Why is my if condition skipping characters? - php

I have made a game flames, where example given a name is Patrick and the other name is Abcdefg, the loop will crash out one character from one name that is the same with a character in the other name. When my condition is if srtlen($name1)==strlen($name2), why is my loop not crashing out letter a in the name Patrick and abcdefg? It only crashes out c.
My problem is in if($r==$e) part.
The desired outcome from the name Patrick and abcdefg is status=10; because A and C are crashed out.
<html>
<head>
<title>Flames</title>
</head>
<body>
<center><form method="post" style="margin-top:60px;">
<h2>Flames</h2>
<input type="text" name="name1" placeholder="First name"/><br>
<input type="text" name="name2" placeholder="Second name"/><br><br>
<input type="submit" name="submit" value="submit"/>
</form>
<?php
if(isset($_POST['submit'])){
$name1=$_POST['name1'];
$name2=$_POST['name2'];
//this is if the names have space on it.
if(strstr($name1,' ')&&strstr($name2,' ')||strstr($name1,' ')||strstr($name2,' ')){
$exploded1=explode(' ',$name1);
$exploded2=explode(' ',$name2);
$joined1=implode("",$exploded1);
$joined2=implode("",$exploded2);
$e=strlen($joined2);
$r=strlen($joined1);
}
else{
$r=strlen($name1);
$e=strlen($name2);
}
$counter=0;
$same=0;
if($r>$e){
for($m=0; $m<=$e-1; $m++){
for($i=0; $i<=$r-1; $i++){
if($counter<$e){
if($joined1[$i]==$joined2[$m]){
$same++;
//$counter++;
$joined1[$i]=' ';
break;
}
}
}
}
}
elseif($e>$r){
for($m=0; $m<=$r-1; $m++){
for($i=0; $i<=$e-1; $i++){
if($counter<$r){
if($joined2[$i]==$joined1[$m]){
$same++;
//$counter++;
$joined2[$i]=' ';
break;
}
}
}
}
}
//this is where it did not check the a character which is most likely to be crashed out because it has a pair
if($r==$e){
for($m=0; $m<$r; $m++){
for($q=0; $q<$r; $q++){
if($name1[$q]==$name2[$m]){
echo $name1[$q].'<br>';
$same++;
//$counter++;
$name1[$q]=' ';
break;
}
}
}
}
$sum=$e+$r;
$mult=$same*2;
$status=$sum-$mult;
echo $joined1.'<br>';
echo $joined2.'<br>';
echo 'r'.$r; echo '<br>';
echo 'e'.$e; echo '<br>';
echo 'status'.$status; echo '<br>';
echo 'sum'.$sum;echo '<br>';
echo 'mult'.$mult; echo '<br>';
echo "$joined1 <br>";
echo "$joined2 <br>";
//this is to determine the flames status.
if($status==1||$status%6==1){
echo 'Friends';
}
if($status==2||$status%6==2){
echo 'Lovers';
}
if($status==3||$status%6==3){
echo 'Anger';
}
if($status==4||$status%6==4){
echo 'Marriage';
}
if($status==5||$status%6==5){
echo 'Enemy';
}
if($status==6||$status%6==0){
echo 'Soulmates';
}
}
?>
</center>
</body>
</html>

When you compare Patrick and Abcdefg , c will only match as capital A and small a are different ,so to solve this you can convert your names to lowercase and then compare them .i.e :
$name1=strtolower($_POST['name1']);
$name2=strtolower($_POST['name2']);
Output :
a<br>c<br>patrick<br>abcdefg<br>r7<br>e7<br>status10<br>sum14<br>mult4<br>patrick <br>abcdefg <br>Marriage

Related

Unable to change the innerHTML of a <p/> tag in PHP

I am learning PHP and trying to change the innerHtml of a <p/> tag using PHP.
The problem is that I don't know why one of my PHP variables is working while the other is not.
The problem I am running into is within the last if statement; the $check variable is not being printed, however when replaced it with another variable $pin, it worked.
Code
</header>
<head>
<h1> MD5 cracker</h1>
</head>
<body>
<p>This application takes an MD5 hash of a four digit pin and check all 10,000 possible four digit PINs to determine the PIN.</p><br/>
<p id='deb'>Debug Output:<br/></p>
<p id='pin'>PIN: Not found </p>
<form method='POST'>
<input type='text' name='md5' value="">
<input type='submit' value='Crack MD5'>
</form>
<?php
$count=0;
$pin_list=array();
$pin_digits=array(0,1,2,3,4,5,6,7,8,9);
if (isset($_POST['md5'])) {
$md5= $_POST['md5'];
foreach ($pin_digits as $d1){
foreach ($pin_digits as $d2){
foreach ($pin_digits as $d3){
foreach ($pin_digits as $d4){
$pin= $d1 . $d2.$d3.$d4;
$check=hash('md5',$pin);
if ( $check==$md5){
echo "<script>
document.getElementById('pin').innerHTML='PIN: '+$pin;
</script>";
break;
}
if ($count<15) {
echo "<script>
document.getElementById('deb').innerHTML=$check;
</script>";
$count+=1;
}
}
}
}
}
}
?>
</body>
Try this, you forget quotes on .innerHTML='$check';
<body>
<p>This application takes an MD5 hash of a four digit pin and check all 10,000 possible four digit PINs to determine the PIN.</p><br/>
<p id='deb'>Debug Output:<br/></p>
<p id='pin'>PIN: Not found </p>
<form method='POST'>
<input type='text' name='md5' value="">
<input type='submit' value='Crack MD5'>
</form>
<?php
$count=0;
$pin_list=array();
$pin_digits=array(0,1,2,3,4,5,6,7,8,9);
if (isset($_POST['md5'])) {
$md5= $_POST['md5'];
foreach ($pin_digits as $d1){
foreach ($pin_digits as $d2){
foreach ($pin_digits as $d3){
foreach ($pin_digits as $d4){
$pin= $d1 . $d2.$d3.$d4;
$check=hash('md5',$pin);
if ( $check==$md5){
echo "<script>
document.getElementById('pin').innerHTML='PIN: '+$pin;
</script>";
break;
}
if ($count<15) {
echo "<script>
document.getElementById('deb').innerHTML='$check';
</script>";
$count+=1;
}
}
}
}
}
}
?>
</body>
JS code should be like document.getElementById('str').innerHTML = 'string';, so update your php source then everything ok. Like
echo "<script>document.getElementById('pin').innerHTML='PIN: {$pin}';</script>";
echo "<script>document.getElementById('deb').innerHTML='{$check}';</script>";

IS_Numeric and Get

When I enter a value it should be numeric only. In my code the check seems to be false because I can enter several non numeric items.
Do you have an idea ?
<body>
<form method="GET" action="">
<input type="text" name="niveau">
<input type="submit" value="valider" >
</form>
<br />
<?php
if (empty($_GET['niveau']))
{
echo 'Enter niveau please : ' . "</br>";
}
else if(isset($_GET['niveau']))
{
echo "Niveau : " .$_GET['niveau'] . "</br>";
}
else if (is_numeric($_GET['niveau']))
{
echo "Niveau is numeric" ;
}
else
{
echo "$var_name1 is not numeric. <br>" ;
}
?>
</body>
The problem is your if/else if structure. The first else if checks whether or not the niveau parameter is set. If yes you print out the niveau level and the second else if condition won't be checked.
You could for example include the second else if condition within the first one, something like this:
if (empty($_GET['niveau']))
{
echo 'Enter niveau please : ' . "</br>";
}
else if(isset($_GET['niveau']) && is_numeric($_GET['niveau']))
{
echo "Niveau : " .$_GET['niveau'] . "</br>";
}
else
{
echo "$var_name1 not set or not numeric. <br>" ;
}

Get session array from different file

I can't access array from other file and still don't figure it out, whether my data already stored into array or not. Should i put $_SESSION into the function?
Lat3_3a.php
<form id="form1" name="form1" method="post" action="Lat3_3b.php">
Insert number: <input type="number" name="num" id="num" />
<input type="submit" name="button" id="button" value="OK" />
</form>
Lat3_3b.php
<?php
session_start();
$_SESSION["num"] = $_POST["num"];
if (empty($_SESSION["num"]))
echo "Please, insert number";
else {
$val=$_POST['num'];
echo " Factorial " .$val. " ! = " .factorial($val)."<br/>";
echo "<a href='Lat3_3c.php'>Link</a>";
}
function factorial($val){
if($val<=1){
$result=1;
return $result;
}elseif($val>1){
for($i=1; $i<=$val; $i++){
$result=$val * factorial($val-1);
}
return $result;
}
$data=array($val,$result,"12345", "Travis");
$_SESSION["var"]=$_POST["data"];
}
?>
Lat3_3c.php
<?php
session_start();
if(empty($_SESSION["var"]))
echo "Variable not found";
else
echo "Data : ". $_SESSION["var"];
?>
Ok looking a bit more in to your code, you need to learn more about the session, post and functions. For that a look at the PHP manual site.
Site http://php.net
For your code to work you need to use something like this:
<?php
session_start();
$_SESSION["num"] = $_POST["num"];
$_SESSION["var"]=array();
if (empty($_SESSION["num"])){
echo "Please, insert number";
}
else {
$val=$_POST['num'];
$function_result=factorial($val);
$data=array($val,$function_result,"12345","Travis");
$_SESSION["var"]=$data;
echo " Factorial " .$val. " ! = " .$function_result."<br/>";
echo "<a href='Lat3_3c.php'>Link</a>";
}
function factorial($val){
if($val<=1){
$result=1;
return $result;
}elseif($val>1){
for($i=1; $i<=$val; $i++){
$result=$val * factorial($val-1);
}
return $result;
}
}
?>
I'm not looking into your math.

add values with one another with input field submit in php calculator

I want a calculator with my PHP code that adds values with one after one with submit button.like when I input a number then submit it and show it on the page then and input other, it should add previous number.and then enter another then submit.like these, numbers are adding with one another after submitting.
<?php
session_start();
?>
<?php
error_reporting(0);
?>
<html>
<title>adding input single values</title>
<body>
<form method="post">
<input type="text" name='number' method="post"/>
<input type="submit" />
</form>
<?php
if(!isset($_POST['number']))
{
}
else
{
$sum += $_POST['number'];
echo ++$sum;
}
?>
</body>
</html>
here is the output
You could use a hidden field instead of storing in the session.
<input type="text" name='number' method="post"/>
<?php
if(!isset($_POST['number'])) {
echo "<input type='hidden' name='prev_number' value=0 />";
} else {
$sum = $_POST['number'] + $_POST['prev_number'];
echo "<input type='hidden' name='prev_number' value=" . $sum . " />";
echo $sum;
}
?>
<input type="submit" />
</form>
<?php
if(!isset($_POST['number'])) {
// ...
}
else {
$_SESSION['number'] = isset($_SESSION['number']) ? $_SESSION['number'] : '';
$_SESSION['number'] += $_POST['number'];
echo $_SESSION['number'];
}

how to echo exploded array line

Here's the code.
<!DOCTYPE html>
<html>
<head><title>Numbers</title></head>
<body>
<form action="index.php" method="get">
<b>Numbers</b>
<br>
<textarea rows="12" cols="25" name="result" value="result"></textarea>
<br>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
<?php
$result=$_GET["result"];
if (empty($_GET['result']))
{
echo '<p><font size="3" color="red">Field is Empty*</font></p>';
}
elseif (isset($_GET['result']))
{
$result=(explode("\n", $result));
}
{
echo count ($result);
echo "<br />";
echo array_sum($result);
}
?>
Ok so I figured out how to get most of my assignment's tasks and the last 1 that I am stuck with is using similar codes such as filter_var to print out non-numerical values that are submitted. Ex. a b c * & ! #
PRINTING any invalid inputs that aren't numbers. Ex. Letters, symbols.
Any suggestions?
you can use the below code on your $result array & get the desired results:
$oddArray = array();
$evenArray = array();
$skippedArray = array();
foreach($result as $value)
{
if(is_numeric($value))
{
if($value%2 == 0)
{
$evenArray[] = $value;
}
else
{
$oddArray[] = $value;
}
}
else
{
$skippedArray[] = $value;
}
}
echo "Sum of odd values entered: ".array_sum($oddArray);
echo "Sum of even values entered: ".array_sum($evenArray);
echo "Skipped invalid values entered: ";
print_r($skippedArray);
I hope above will help you.

Categories