While statement not displaying ECHO [duplicate] - php

This question already has answers here:
MySQL - count total number of rows in php
(11 answers)
Closed 4 years ago.
the code below is display the else statement, but when $getlogin == 0 it is not displaying
while ($getlogin = mysql_fetch_array($checklogin)) {
if ($getlogin == 0) {
echo "NOTHING TO DISPLAY";
}
else{
echo $getlogin['username'];
echo $getlogin['password'];
}
}

$checklogin = mysql_query($queryContents);
if(mysql_num_rows($checklogin)== 0){
echo "NOTHING TO DISPLAY";
}
else{
while($getlogin = mysql_fetch_array($checklogin)) {
var_dump($getlogin);
}
}
you can use php empty function also to check empty
like this
if (empty($checklogin)) {
echo 'NOTHING TO DISPLAY';
}else{
while($getlogin = mysql_fetch_array($checklogin)) {
var_dump($getlogin);
}
}

Related

Problem during creating Pyramid Star Patterns in PHP [duplicate]

This question already has answers here:
PHP Displaying a number of asterisks depending on the number given in the input field
(4 answers)
Closed 11 months ago.
Question :
My Code :
<html\>
Enter the number of rows:
<?php
if($_POST)
{
$row = $_POST['row'];
if(!is_numeric($row))
{
echo "Make sure you enter a NUMBER";
return;
}
else
{
for($row=0;$row<=1;$row++){
for($j=0;$j<=$row;$j++){ echo "#"; } echo ""; }}}?>
The problem is it's showing only two rows
I expected as shown in the photo
$row = 10;
for($i=0;$i<=$row;$i++){
for($j=0;$j<=$i;$j++){ echo "#"; } echo "<br>"; }
You are overriding the $row variable.
Also, you don't need the else since you are returning in case the if(!is_numeric) is true;
This should do it.
if(isset($_POST['row'])) {
$rows = $_POST['row'];
if(!is_numeric($rows))
{
echo "Make sure you enter a NUMBER";
return;
}
for($row=0;$row<=$rows;$row++){
for($j=0;$j<=$row;$j++){
echo "#";
}
echo "\n";
}
}
You can make use of the PHP function str_repeat to simplify the script.
This would only take 1 loop, so you don't get confused with the variable names.
$row = 10;
$i = 1;
while ($i <= $row)
{
echo str_repeat("#", $i); echo "\n";
$i ++;
}
Working Example here.

PHP check if a string contains a specific character [duplicate]

This question already has answers here:
How do I check if a string contains a specific word?
(36 answers)
Closed 1 year ago.
I have 2 strings $verify and $test. I want to check if $test contains elements from $verify at specific points. $test[4] = e and $test[9] = ]. How can I check if e and ] in $verify?
$verify = "aes7]";
$test = "09ske-2?;]3fs{";
if ($test[4] == $verify AND $test[9] == $verify) {
echo "Match";
} else {
echo "No Match";
}
Use strpos() https://www.php.net/manual/en/function.strpos.php
https://paiza.io/projects/n10TMV4Ate8-vtzioDrsMg
<?php
$verify = "aes7]";
$test = "09ske-2?;]3fs{";
if (strpos($verify, $test[4]) !== false && strpos($verify, $test[9]) !== false) {
echo "Match";
} else {
echo "No Match";
}
?>
As of PHP8 you can use str_contains()
<?php
$verify = "aes7]";
$test = "09ske-2?;]3fs{";
if (str_contains($verify, $test[4]) && str_contains($verify, $test[9])) {
echo "Match";
} else {
echo "No Match";
}
?>
Thanks to #symlink for the code snippet I butchered

PHP IF Statement To use Else If or Another IF Statement? [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 3 years ago.
I'm trying to write a PHP statement that solves the following. My attempt is not taking. Basically I have coded items regarding heating types. I need a PHP statement that says If x = 356 then display Gas if x = 357 then display Oil if x = 358 display coal, etc, etc. Here is what I have started with and I'm a total newbie at php, please help!
<?php
function heatfuel_function( $heatfuel ) {
if ( heatfuel = "543" ) {
echo "Electric";
}
if (heatfuel = "544") {
echo "LP Gas";
}
if (heatfuel = "545") {
echo "Natural Gas";
}
if (heatfuel = "546") {
echo "Natural Available Gas";
}
if (heatfuel = "550") {
echo "Multi Fuel";
}
if (heatfuel = "552") {
echo "Oil Fuel";
}
if (heatfuel = "556") {
echo "Wood Fuel";
}
} else {
echo "";
}
?>
You should be looking into a case switch statement. And use default as your "else"
IE
switch ($heatfuel) {
case 543:
echo "LP Gas";
break;
case 545:
echo "Natural Gas";
break;
case 546:
echo "Natural Available Gas";
break;
// As many cases as you want ..
default:
echo "None of the above";
}
REFERENCE FROM PHP.NET

how can I solve a unexpected end of file error [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 3 years ago.
I have some issue with my PHP file it's meant to put steps passed in a game on my database but when I try to submit my form it return me this error,
Parse error: syntax error, unexpected end of file in /***/**********/****/********/WWW/Page_Administrateur/InsertionEtape.php on line 31
here's my code:
<?php
include('../connexion.inc.php');
?>
<?php
if (isset($_POST["quantity"]) && $_POST["quantity"] != 0 && $_POST["quantity"] != "") {
$nbEtapes=$_POST['quantity'];
echo $nbEtapes;
for ($i=1; $i <= $nbEtapes; $i++) {
echo "etape".$i;
$DescriptionEtape=$_POST['NomEtape_'.$i];
$NomEtape=$_POST['DescriptionEtape_'.$i];
$req= "INSERT INTO `Etape` VALUES ('".$NomEtape."','".$DescriptionEtape."');";
try {
$dbh->query($req);
echo "Etape Ajouté";
} catch (\Exception $e) {
echo $e;
}
header('Refresh: 50; URL=FormulaireInsertion.php');
}
}else {
echo "aucune étapes ajouter car aucune étape n'a été entrée";
header('Refresh: 2; URL=FormulaireInsertion.php');
}
echo "done";
?>
how can I solve this ?
you are using header inside the loop try commenting
for ($i=1; $i <= $nbEtapes; $i++) {
echo "etape".$i;
$DescriptionEtape=$_POST['NomEtape_'.$i];
$NomEtape=$_POST['DescriptionEtape_'.$i];
$req= "INSERT INTO `Etape` VALUES ('".$NomEtape."','".$DescriptionEtape."');";
try {
$dbh->query($req);
echo "Etape Ajouté";
} catch (\Exception $e) {
echo $e;
}
//header('Refresh: 50; URL=FormulaireInsertion.php');
}

How to check a variable value is in an array? [duplicate]

This question already has answers here:
How can I check if an array contains a specific value in php? [duplicate]
(8 answers)
Closed 9 years ago.
How to check php variable with array?
When $user_check = "ccc";
<?php
$user_group = array('aaa' , 'bbb' , 'ccc' , 'ddd');
$name_group = join("','",$user_group);
if ($name_group != $user_check) {
echo "not found."
} else {
echo "found."
}
?>
Try in_array()
if (in_array($user_check,$user_group))
{ echo "found."; }
else
{ echo "Not found."; }
Check the documentation on this link.
Use in_array Function
if ( in_array($user_check, $user_group) ) {
echo "found."
} else {
echo "not found."
}

Categories