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.
Related
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);
}
}
I want to make php fgets(STDIN). But I don't know how to restrict the input count and i don't know how to accept only specific letters. Please guide me. I write down the problems of mine. I'm the beginner in STDIN usage. It's important for me because i have to do it in my Programming Test Exam. Help me please.
Here is Problem 1
Problem 1: I want to get only 10 Letters from cmd input and these
input should be W & S.
Here is Problem 2
Problem 2 : I want to get only 3 numbers and how to restrict the cmd
input to only number.
I just realized Ive misread your question for part 2, but this will work for part one and be a good help to start problem 2:
<?php
$errorMessage = false;
$attempts = 1;
$maxAttempts = 5;
function validates($string)
{
global $errorMessage; //tell this function to use global errormessage
if (strlen($string) > 10) {
$errorMessage = 'Exceeded String Length. Please ensure no more than 10 characters are entered';
return false;
}
preg_match('/\d+/', $string, $matches);
$extractedDigits = (isset($matches[0]) ? $matches[0] : 0 );
if ($extractedDigits > 3) {
$errorMessage = 'Exceeded allowed number of digits. Please ensure no more than 3 digits are contained in the string';
return false;
}
$errorMessage = false;
return true;
}
$result = readline('please enter a valid string: ');
while (!validates($result) && $attempts < $maxAttempts) { //while fail validation and have attempted less than the max attampts
echo "\n";
echo '*******************************************';
echo "\n";
echo 'Attempts Left (' . ($maxAttempts - $attempts) . ')';
echo "\n";
$result = readline($errorMessage . '. Please try again: ');
$attempts++;
}
echo "\n";
echo '*******************************************';
echo "\n";
if ($attempts >= $maxAttempts) {
echo 'Failed, too many attempts';
} else {
echo "\n";
echo 'Success...';
echo "\n";
echo "\n";
echo $result;
echo "\n";
echo "\n";
}
echo "\n";
This question already has answers here:
Equal string comparisons are failing
(2 answers)
Closed 8 years ago.
So i have been trying to compare two strings. One from a $_POST method , and one read from a text file. Although they seem to be the same when I print both of them, my strcmp() never returns 0 meaning that they are not equal. Why does my strcmp() function never return 0?
Here is the actual code :
$fileRead = 'Users.txt';
$wasRead = FALSE;
$handleRead = fopen($fileRead,'r');
$character = fread($handleRead,1);
echo"<p> ".$character." </p>";
fgets($handleRead);
while($character != 'Q')
{
$lineName = fgets($handleRead);
echo "<p> ".$lineName." </p>";
$linePassword = fgets($handleRead);
echo "<p> ".$linePassword." </p>";
$character = fread($handleRead,1);
echo"<p> ".$character." </p>";
fgets($handleRead);
$porfavor = $_POST['newUserId'];
$porfavor = strtolower($porfavor);
echo $porfavor."<br>";
echo $lineName."<br>";
//$comparison = strcmp($lineName." ",$_POST['newUserId']);
//echo $comparison;
$comparison = strcmp($porfavor,$lineName);
echo $comparison;
if($comparison == 0)
{
$character = 'Q';
echo "<p> User Already Exists </p>";
echo " Sing In";
echo "<br>";
}
}
fclose($handleRead);
You may have an invisible character in one of your strings. Try doing a var dump.
$comparison = strcmp($porfavor,$lineName);
if($comparison != 0){
echo "<pre>";
var_dump($porfavor);
var_dump($lineName);
exit;
}
some tips:
You may need to use trim() on your variables to account for whitespace;
consider using strtoupper() if either might vary in case. Keep in mind it's case sensitive.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
please help me with code to check if numbers in a table is prime: this is my code below. I used the isPrime function but I get an error where by when I call the function in the table it returns the words "isPime"
<?php
$x="num1";
$y="num2";
if(isset($_POST['Submit'])) {
$start=$_POST['num1'];
$end=$_POST['num2'];
echo "<table width='250' cellpadding='2' cellspacing='4' border='1'>";
echo "<tr><td>Number</td><td>Odd or Even?</td><td>Prime?</td></tr>";
for ($start;$start<=$end;$start++) {
$answer=$start;
$number=$answer;
$check="text";
$num=$answer;
if($number%2==0) {
$check="Even";
} else {
$check="Odd";
}
{ }
echo "<tr><td>$answer</td><td>$check</td><td>
<?php ?>
</td></tr>";
} echo "</table>";
} ?>
You are missing the entire prime check and you aren't even printing anything in your third column.
Also someone seems to have deleted your code in the first EDIT and changed it in the second. I somehow had a copy of the first edit and could make an example.
This is a working example within your code:
<?php
$x="num1"; $y="num2";
if(isset($_POST['Submit']))
{
$start=$_POST['num1']; $end=$_POST['num2'];
echo "<table width='250' cellpadding='2' cellspacing='4' border='1'>";
echo "<tr><td>Number</td><td>Odd or Even?</td><td>Prime?</td></tr>";
for ($start;$start<=$end;$start++)
{
$answer=$start; $number=$answer; $check="text"; $num=$answer;
if($number%2==0)
{ $check="Even"; }
else
{ $check="Odd"; }
$prime = (isPrime($number)? 'Yes':'No');
echo "<tr><td>$answer</td><td>$check</td><td>$prime</td></tr>";
}
echo "</table>";
}
function isPrime($num) {
//1 is not prime. See: http://en.wikipedia.org/wiki/Prime_number#Primality_of_one
if($num == 1)
return false;
//2 is prime (the only even number that is prime)
if($num == 2)
return true;
/**
* if the number is divisible by two, then it's not prime and it's no longer
* needed to check other even numbers
*/
if($num % 2 == 0) {
return false;
}
/**
* Checks the odd numbers. If any of them is a factor, then it returns false.
* The sqrt can be an aproximation, hence just for the sake of
* security, one rounds it to the next highest integer value.
*/
for($i = 3; $i <= ceil(sqrt($num)); $i = $i + 2) {
if($num % $i == 0)
return false;
}
return true;
}
?>
try this
<?php
$x="num1";
$y="num2";
if(isset($_POST['Submit'])) {
$start=$_POST['num1'];
$end=$_POST['num2'];
echo "<table width='250' cellpadding='2' cellspacing='4' border='1'>";
echo "<tr><td>Number</td><td>Odd or Even?</td><td>Prime?</td></tr>";
for ($start;$start<=$end;$start++) {
$answer=$start;
$number=$answer;
$check="text";
$num=$answer;
if($number%2==0)
{
$check="Even";
}
else
{
$check="Odd";
}
if(isPrimeNumber($number))
{
$pirme_status = "Yes";
}
else
{
$pirme_status = "No";
}
echo "<tr><td>$answer</td><td>$check</td><td>$pirme_status</td><</tr>";
} echo "</table>";
}
and PHP function is
function isPrimeNumber($number)
{
$flag = true;
$max_count = ceil($number/2);
for($i=2; $i<$max_count; $i++)
{
if($number%$i==0)
{
$flag = false;
break;
}
}
return $flag;
}
?>
This question already exists:
Closed 10 years ago.
Possible Duplicate:
same roll number can not be added
I want my code to behave this way that if once i had added a roll number, it can not be added again in the registration..........same as if once an email adress is registered the error displays that EMAIL ADDRESS ALREADY BEEN TAKEN....
I am creating a function . of roll numbr value is 1 it shall display error and if not 1 it shall not display the error
function selectroll($studentname,$rollnumber)
{
$sql = "SELECT * FROM tblstuden WHERE studentname = 'studentname' and rollnumber = '$rollnumber';";
$obj_db = new DB();
$obj_db->query($sql);
$row = $obj_db->rsset();
if{
$val = 1;
}
else{
$val = 0;
}
$obj_db->db_close();
return $val;
}
$this->_id($row[id]);
$this->_studentname($row[studentname]);
$this->_rollnumber($row[rollnumber]);
$this->_classname($row[classname]);
$obj_db->db_close();
}
}
?>
and the function is called at the page by this method
<?php
if(isset($_POST['btnSubmit']) and $_GET['action'] == "Add")
{
$val = $Tblstuden->selectroll($_POST['studentname'],$_POST['rollnumber']);
if ($val =='1')
{
$Tblstuden->copyPostData();
$Tblstuden->insert();
echo"asdf";
}
redirect("index.php?page=studentlist");
}
else
{
echo"abc";
}
?>
You probably want
function selectroll($studentname,$rollnumber)
{
$sql = "SELECT * FROM tblstuden WHERE studentname = 'studentname' and rollnumber = '$rollnumber';";
$obj_db = new DB();
$obj_db->query($sql);
$row = $obj_db->rsset();
if ($row){
$val = 1;
$this->_id($row[id]);
$this->_studentname($row[studentname]);
$this->_rollnumber($row[rollnumber]);
$this->_classname($row[classname]);
}
else{
$val = 0;
}
$obj_db->db_close();
return $val;
}
}
?>
in line 8 of the top function - as otherwise the code won't compile.
Sub-note, your code is subject to mysql injection, you should look at using PDO (http://php.net/manual/en/book.pdo.php) for your database functions before you get used to the old method. Please. Do it now ;)