I am new to php so can anyone tell me why when I open the HTML, it is not showing “5! is 120” as it should be and am instead getting:
Applications
", $num, "! is ", factorial ($num), ".
"; } else { echo "
Please enter a positive integer.
"; } echo "
Return to the Entry Page
"; ?>
PHP functions
<?php
function isPositiveInteger ($n) {
$result = false;
if (is_numeric($n))
if ($n == floor($n))
if ($n>0 )
$result = true;
return $result;
}
function factorial ($n) { // declare the factorial function
$result = 1; // declare and initialise the result variable
$factor = $n; // declare and initialise the factor variable
while ($factor > 1) { // loop to multiple all factors until 1
$result = $result * $factor;
$factor--; // next factor
} // Note that the factor 1 is not multiplied
return $result;
}
?>
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="description" content="product" />
<meta name="keywords" content="HTML5, tags" />
<meta name="author" content="Ib" />
<title>Product</title>
</head>
<body>
<?php
include ("mathfunctions.php");
?>
<h1> Applications </h1>
<?php
$num=5;
if (isPositiveInteger($num)) {
echo "<p>", $num, "! is ", factorial ($num), ".</p>";
} else {
echo "<p> Please enter a positive integer. </p>";
}
echo "<p> <a href='factorial.html'> Return to the Entry Page </a></p>";
?>
</body>
</html>
For some reason I'm trying to have my "else" statement increment the amount of times the person has been to my website and it's not incrementing correctly. When I run my php, all it does it increment it once, after that, there are no more refreshes and the $cookieValue just keep echoing 2 instead of 3,4,5,6... What am I missing here?
<?php
date_default_timezone_set('EST');
if (!isset($_COOKIE["time"])) {
$cookieValue = 1;
setcookie("time", $cookieValue, time()+(86400*365));
}
$cookieLastVisit = date(DATE_RFC1036);
setcookie("lastVisit", $cookieLastVisit, time()+(86400*365));
?>
<html>
<head>
<title>Question 2</title>
</head>
<body>
<?php
if ($cookieValue == 1){
echo ("Welcome to my webpage! It is the first time that you are here.");
} else {
$cookieValue = ++$_COOKIE["time"];
echo("Hello, this is the " . $_COOKIE["time"] . " time that you are visiting my webpage. Last time you visited my webpage on: " . $cookieLastVisit . " EST");
$visit = date(DATE_RFC1036);
setcookie("lastVisit", $visit);
}
?>
</body>
</html>
Move the setting of the cookie time var to the same place as the declaration.
<?php
date_default_timezone_set('EST');
if (!isset($_COOKIE["time"])) {
$cookieValue = 1;
} else {
$cookieValue = ++$_COOKIE["time"];
}
setcookie("time", $cookieValue, time()+(86400*365));
$cookieLastVisit = date(DATE_RFC1036);
setcookie("lastVisit", $cookieLastVisit, time()+(86400*365));
?>
<html>
<head>
<title>Question 2</title>
</head>
<body>
<?php
if ($cookieValue == 1){
echo ("Welcome to my webpage! It is the first time that you are here.");
} else {
echo("Hello, this is the " . $_COOKIE["time"] . " time that you are visiting my webpage. Last time you visited my webpage on: " . $cookieLastVisit . " EST");
// you can't set cookie after you've output to the browser :/
//$visit = date(DATE_RFC1036);
//setcookie("lastVisit", $visit);
}
?>
</body>
</html>
You need to set the value of the cookie. The change to the $_COOKIE variable doesn't save the value of the cookie in "the next" page
else {
$cookieValue = ++$_COOKIE["time"];
setcookie("time", $cookieValue, time()+(86400*365));
...
}
Thank you for your time.
I am trying to modify an existing PhP script to accept a user input string on line 23. Right now it only accepts numbers put into the array.
<?php
function isJewishLeapYear($year) {
if ($year % 19 == 0 || $year % 19 == 3 || $year % 19 == 6 ||
$year % 19 == 8 || $year % 19 == 11 || $year % 19 == 14 ||
$year % 19 == 17)
return true;
else
return false;
}
function getJewishMonthName($jewishMonth, $jewishYear) {
$jewishMonthNamesLeap = array("Tishri", "Heshvan", "Kislev", "Tevet",
"Shevat", "Adar I", "Adar II", "Nisan",
"Iyar", "Sivan", "Tammuz", "Av", "Elul");
$jewishMonthNamesNonLeap = array("Tishri", "Heshvan", "Kislev", "Tevet",
"Shevat", "", "Adar", "Nisan",
"Iyar", "Sivan", "Tammuz", "Av", "Elul");
if (isJewishLeapYear($jewishYear))
return $jewishMonthNamesLeap[$jewishMonth-1];
else
return $jewishMonthNamesNonLeap[$jewishMonth-1];
}
$jdNumber = gregoriantojd(12, 12, 2016);
$jewishDate = jdtojewish($jdNumber);
list($jewishMonth, $jewishDay, $jewishYear) = explode('/', $jewishDate);
$jewishMonthName = getJewishMonthName($jewishMonth, $jewishYear);
echo "<p>The Jewish date of death is $jewishDay $jewishMonthName $jewishYear</p>\n";
?>
What I would LIKE is the line
$jdNumber = gregoriantojd(12, 12, 2016);
to accept instead of specific numbers, USER INPUT. I was thinking that you could use the $userinput, but that threw an error of expected 3 strings, got one.
Again this is not my forte, but I am thrown into the mix as back-end for a project. I do not expect code written for me, just nudges in the right direction. Thank you.
There are lots of ways to get user input, but I really like jQuery-UI's datepicker. And if it is set, then you can parse the date with the DateTime class and format its output as inputs to gregoriantojd
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$( "#datepicker" ).datepicker();
} );
</script>
</head>
<body>
<form>
<p>Date: <input type="text" name="datepicker" id="datepicker"></p>
<p><input type="submit" value="Submit"></p>
</form>
</body>
</html>
<?php
function isJewishLeapYear($year) {
if ($year % 19 == 0 || $year % 19 == 3 || $year % 19 == 6 ||
$year % 19 == 8 || $year % 19 == 11 || $year % 19 == 14 ||
$year % 19 == 17)
return true;
else
return false;
}
function getJewishMonthName($jewishMonth, $jewishYear) {
$jewishMonthNamesLeap = array("Tishri", "Heshvan", "Kislev", "Tevet",
"Shevat", "Adar I", "Adar II", "Nisan",
"Iyar", "Sivan", "Tammuz", "Av", "Elul");
$jewishMonthNamesNonLeap = array("Tishri", "Heshvan", "Kislev", "Tevet",
"Shevat", "", "Adar", "Nisan",
"Iyar", "Sivan", "Tammuz", "Av", "Elul");
if (isJewishLeapYear($jewishYear))
return $jewishMonthNamesLeap[$jewishMonth-1];
else
return $jewishMonthNamesNonLeap[$jewishMonth-1];
}
if(
isset($_GET['datepicker']) &&
$datetime = DateTime::createFromFormat('m/d/Y', $_GET['datepicker'])
){
$jdNumber = gregoriantojd($datetime->format('m'), $datetime->format('d'), $datetime->format('Y'));
$jewishDate = jdtojewish($jdNumber);
list($jewishMonth, $jewishDay, $jewishYear) = explode('/', $jewishDate);
$jewishMonthName = getJewishMonthName($jewishMonth, $jewishYear);
echo "<p>The Jewish date of death is $jewishDay $jewishMonthName $jewishYear</p>\n";
}
There are two ways. They both require that you convert your $userinput variable into an array.
if: $userinput = "12 6 2016";
You can do:
$input = explode($userinput, ' ');
$jdNumber = gregoriantojd($input[0], $input[1], $input[2]);
The other way is to pass the array as a list of inputs:
$jdNumber = call_user_func_array('gregoriantojd', $input);
Not tested, but should work.
can someone please help me how to fix this code. What I need is to let the current time compare it to the nearest saved schedule in the column "from"
because the current time is only comparing it to the first added schedule
code:
<?php
//Include the database configuration
include 'config.php';
//Get the data of the selected teacher
$teacher = $dbconnect->prepare("SELECT * FROM time WHERE IMEI = ? AND NFC = ?");
$teacher->bindValue(1,$_GET['IMEI']);
$teacher->bindValue(2,$_GET['NFC']);
$teacher->execute();
//Get the data
$time = $teacher->fetch();
//Store the current time
$current_time = new DateTime();
//Placeholder variables
$time_difference = 0;
$remark = "";
//If there is such a teacher let the teacher enter
if(!empty($time))
{
//Get the time difference
$time_difference = round( (strtotime($current_time->format('H:i:s')) - strtotime($time['From'])) / 60 );
//Check if the professor is late or not
//If the time difference is negative he/she is early
if($time_difference < 0)
{
$remark = 'Sir why are you so early?';
}
//If the prof is on time which is 0 time difference or less than 15 minutes
else if($time_difference == 0 || $time_difference < 15)
{
$remark = "Sir it's a miracle you are on time";
}
//Oh come on why are you so late Sir?Porn marathon at night?
else if($time_difference == 15 || $time_difference > 15)
{
$remark = 'Sir why are you late?';
}
$time_in = $dbconnect->prepare("INSERT INTO time_in (teacher_id,name,NFC,IMEI,time_in,remarks) VALUES (?,?,?,?,?,?)");
$time_in->bindValue(1,$time['teacher_id']);
$time_in->bindValue(2,$time['name']);
$time_in->bindValue(3,$time['NFC']);
$time_in->bindValue(4,$time['IMEI']);
$time_in->bindValue(5,$current_time->format('H:i:s'));
$time_in->bindValue(6,$remark);
$time_in->execute();
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome!</title>
</head>
<body>
<h1>
<?php
//If there is such a teacher,welcome him/her
if(!empty($time))
{
echo 'Welcome '.$time['name'].'! Your NFC is '.$time['NFC'];
echo '</br>';
echo 'Time in at: '.$current_time->format('H:i:s');
//This is just a temporary display to show the time difference for testing.Kindly delete it later
echo '</br>';
echo 'Time difference: '.$time_difference;
//Display remark
echo '</br>';
echo 'Remark:'.$remark;
}
else
{
echo 'You are not registered.';
}
?>
</h1>
</body>
</html>
The program gets 5 variables from 2 database tables and compares them to each other. Each correct match increments a correct variable.
I should be getting a number like "100%" but instead get "�%" and "?%" etc. or just random characters.
See below extract from the HTML page
<!DOCTYPE html>
<html xmlns="w3.org/1999/xhtml"; xml:lang="en" lang="en">
<head>
<meta charset="UTF-8" />
</head>
<body>
<?php
$user = $_SESSION['currentUser'];
$answerCheck1 = mysqli_query($conn,"SELECT * FROM tutorial_exam");
$answerRowCheck1 = mysqli_fetch_array($answerCheck1);
$answerCheck2 = mysqli_query($conn,"SELECT * FROM tutorials_test WHERE user_id='$user'");
$answerRowCheck2 = mysqli_fetch_array($answerCheck2);
$answerCheckT = mysqli_query($conn,"SELECT * FROM tutorials WHERE user_id='$user'");
$answerRowCheckT = mysqli_fetch_array($answerCheckT);
$total = $answerRowCheckT['exam'];
if($total > 0){$total = $total ."%";}else{$total = '';}
$counter1 = 1;
$correct1 = 0;
$userAnswer1 = '';
$databaseAnswer1 = '';
while($counter1 <= 5)
{
$index1 = 'q'.$counter1;
$userAnswer1 = $answerRowCheck2[$index1];
$databaseAnswer1 = $answerRowCheck1[$index1];
if($userAnswer1 == $databaseAnswer1)
{
$correct1 = $correct1 + 1;
//$correct = $answerRow[$counter];
}
$counter1 = $counter1 + 1;
}
$mark1 = $correct1 / 5 * 100;
$mark1 = round($mark1,0);
echo $mark1 . "%";
?>
</body>
</html>
Just a suggestion, try adding this in the <head> section of your page:
<meta charset="utf-8">
We run our system from memory sticks with Server2Go, and what seems to have fixed it is updating the PHP version on the server2Go systems.
We found out how to do that in another Stack Overflow thread: Other answer link