where to write else condition inside loop within another loop? [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
If the phone number matches , I am able to print "Yes". If it doesnot match , i need to print "No". But if i include else part , its executing only else part. Please guide me how and where to write else part.
<?php if($re['phone_v'] == "1"){
echo "Yes";
}else if($re['phone_v'] == "0"){
for($i=0;$i < count($crusers);$i++) {
if ($re['phone'] == $crusers[$i]['phone']) {
echo "Yes";
}
}
}
?>
I am checking condition inside table in view. small thing is making complicated.

This will help you.
$re['phone_v'] = 0;
$re['phone'] = 1;
$crusers[0]['phone'] = 1;
if($re['phone_v'] == "1"){
echo "Yes";
}else{
if($re['phone_v'] == "0")
{
for($i=0;$i < count($crusers);$i++)
{
if ($re['phone'] == $crusers[$i]['phone'])
{
echo "Yes11";
}else{
echo "No";
}
}
}
}

Related

Replacing Numbers with text in php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
So my taslk is to program two numbers randomly (for example 0 and 1) and then replace the numbers with "Hello" and "bye". I've done the generating it rendomly part, but now I'm struggling with the replacing Part. It would be cool If the solution would be by using "if" and "else"
Here's what i've done so far,
thank you in advance
<?php
$zaehler = 0;
while($zaehler < 10)
{
echo mt_rand(0, 1);
if(0)
{
echo "bye" ;
}
else (1) ;
{
echo "hello" ;
}
$zaehler++;
}
?>
For your case, just use a comparison statement
e.g.
if($result==0) { // do something;} else {// do another thing; }
(the $result will only be either 0 or 1, so just use one if-then-else)
<?php
$zaehler = 0;
while($zaehler < 10){
$result=mt_rand(0, 1);
echo $result;
if($result==0) {
echo "bye" ;
} else {
echo "hello" ;
}
$zaehler++;
}
?>
So i just saw your post,
First, you need to assign mt_rand to a variable.
Afer your statement IF isnt correct, you need to evaluate something like this $i === 0
And about your else you need an else if, so you can take a look to this code ;)
$idx = 0;
while ($idx < 10) {
$random_int = mt_rand(0, 1);
if ($random_int === 0) {
echo "bye\r\n";
} else if ($random_int === 1) {
echo "hello\r\n";
}
$idx++;
}

Why this simple code for increment numbers doesn't work? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I want to make just simple incrementing with session but the session just stop every time on 2. Can you please help me what I should do?
if (!isset($_SESSION["current"]))
{
$_SESSION["current"] = 1;
}
$_SESSION["current"] = $_SESSION["current"] +1;
echo "SESSION: ".$_SESSION["current"]."<br>CURRENT: ";
I tryed this code and it doesnt work too:
<?php
if (isset($_POST["previous"]))
{
$_SESSION["current"] = $_SESSION["current"] - 1;
}
if (isset($_POST["next"]))
{
$_SESSION["current"] = $_SESSION["current"] + 1;
}
echo "SESSION: ".$_SESSION["current"]."<br>CURRENT: ";
?>
I believe you want to use this "current" value for pagination. If that is the case you are going about it a bit wrong. Remember the session will store the last value set until it is destroy or reset with another value. Here is my take
<?php
session_start(); // according to you, you don't need this so please remove
$current = 0; // lowest value for our pagination
if(isset($_SESSION['current'])){
$current = $_SESSION['current']; // session has an entry
}
if ((isset($_POST["previous"]) || isset($_POST['next'])))
{
if(isset($_POST['previous'])){
$current -= 1; // for previous
}else{
$current += 1; // for next
}
$current = ($current >= 0)?$current: 0; // reset to 0 when we get to negative
}else{
$current = 0; // we maybe back on the "start page"
}
$_SESSION['current'] = $current;
echo "SESSION: ".$_SESSION["current"]."<br>CURRENT: ";
That code should work, maybe your session is not active. try to issue this on top of your script:
if (session_status() == PHP_SESSION_NONE) {
session_start();
}`

Else If Not Work in PDO Script [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I wanted to thwart POST if the number of characters entered is less than 6 use this code:
<?php
if(isset($_POST['button']))
{
$id = $_SESSION['user_session'];
$u_password = $_POST['password'];
if($crud->updatePassword($id,$u_password))
{
$msg = $msgsucces;
}
else if($u_password < 6) {
$msg = "<div class='alert alert-warning'><strong>Failed!!</strong> Minimum length 6 character</div>";
return false;
}
else
{
$msg = $msgfailled;
}
}
echo $msg ;
?>
But apparently it did not work. What is wrong?
You can use your conditions as like that:
$msg = "";
if(strlen($u_password) < 6) {
$msg = "<div class='alert alert-warning'><strong>Failed!!</strong> Minimum length 6 character</div>";
}
else{
if($crud->updatePassword($id,$u_password))
{
$msg = "success message";
}
else {
$msg = "failure message";
}
}
echo $msg;
For checking length of an input you can use strlen() function.
Side note:
Note that I have remove return false from ist condition.
Also suggest you to always add error_reporting() in your file this will help you to save your time. (Only for local environment not for production).
with $u_password <6 you are not really checking length.
You need strlen function to get the length and compare to 6 so we use strlen function
Rule of thumb is you should have wrong cases before the write one.
The last else is executed if $crud->updatePassword($id,$u_password) fails ie false.
if(strlen($u_password) < 6)
{
$msg = "<div class='alert alert-warning'><strong>Failed!!</strong> Minimum length 6 character</div>";
return false;
}
else if($crud->updatePassword($id,$u_password))
{
$msg = $msgsucces;
}
else {
$msg =$msgfailled;
}
}
echo $msg ;
?>

Simple PHP issue that I cant seem to get to work [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Im working on a simple roll a dice program. So it has to generate a random number from 0-5 and when it selects 0-4 it echo's and when it selects 5 it echo's.
To it keeps echo the first echo 'you got 6'. I cant seem to get my mind around this.
<!DOCTYPE html>
<html>
<head>
<title>A loop of your own</title>
<link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
<?php
$trows = 0;
$dice = 0;
while ($dice == 0)
{
$dice = rand(0,5);
$trows++;
}
if ($dice = 5)
{
echo 'you got 6';
}
else
{
echo 'you got 1-5';
}
echo '<br><br>';
echo $trows;
?>
</body>
if ($dice = 5)
Is actually assigning the value of 5 to dice, rather than comparing the values, so the first part of the if statement will be entered, rather than the else.
if ($dice == 5)
is what you are looking for
Edit: From your comments I think this is what you are looking for
$trows = 0;
$dice = 0;
while ($dice != 5)
{
$dice = rand(0,5);
$trows++;
if ($dice == 5) {
echo 'you got 6';
} else {
echo 'you got 1-5';
}
echo '<br><br>';
echo $trows;
}
Your if statement is not testing for a condition, but rather performing an assignment and then evaluating the truth of that assignment (which will always be true in this case).
Your code should be:
if($dice == 5)
{
echo 'you got 1-5';
}
Notice the == instead of the single =
This will do the trick
while ($trows == 0)
{
$dice = rand(0,5);
if ($dice == 5)
{
echo 'you got 6';
}
else
{
echo 'you got 1-5';
$trows++;
}
}

why isnt this equal function working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
im trying to make something to change password.
nkode is the new password
gkode is the old password
if($_GET[rediger] == 'ja'){
$nkode = md5($_POST[nkode]);
$gkode = md5($_POST[gkode]);
if($nkode !== ''){
if($gkode !== ''){
$nukode = $udskrivprofil[Kodeord];
if($gkode == '$nukode'){
echo "success";
} else {
echo "fail";
}
}
}
echo "<br>$gkode <br> $nukode";
}
both $gkode and $nukode outputs the 100% same, yet i get the fail error... whats wrong?
if($gkode == '$nukode'){
Look at those quotes. You're comparing the contents of $gkode against a string which has the characters $, n, u, etc... in it...
Maybe you wanted
if($gkode == $nukode){
instead?
remove quotes from $nukode variable...
if($_GET[rediger] == 'ja'){
$nkode = md5($_POST['nkode']);
$gkode = md5($_POST['gkode']);
if($nkode !== ''){
if($gkode !== ''){
$nukode = $udskrivprofil[Kodeord];
if($gkode == $nukode){
echo "success";
} else {
echo "fail";
}
}
}
echo "<br>$gkode <br> $nukode";
}

Categories