got multiples number in php - javascript

Make a program that prints the numbers from 1 to 100,
which, if the numbers are multiples of three will print the word
"Max" (without the quotes two), and if the number is a multiple of five
will be scored "Good", and if the number is a multiple of three and five
both, then it will print "MaxGood".
here my script
<?php
for ($x = 1; $x <= 100; $x++) {
echo "$x,";
}
$check_number = 100;
for ($i = 1; $i < $check_number; $i += 3)
{
echo "<br>$i = Max";
}
for ($i = 1; $i < $check_number; $i += 5)
{
echo "<br>$i = Good";
}
for ($i = 1; $i < $check_number; $i += 3 and $i += 5)
{
echo "<br>$i = MaxGood";
}
?>
the last function is wrong please help me

for ($i = 1; $i <= 100; i++) {
echo $i;
if($i % 3 == 0) echo "Max";
if($i % 5 == 0) echo "Good";
echo "<br/>";
}
php modulo is very practicable in this case.
http://php.net/manual/en/internals2.opcodes.mod.php
greetings

<?php
for ($x = 1; $x <= 100; $x++) {
echo "$x, ";
if($x % 3 == 0)echo 'Max';
if($x % 5 == 0)echo 'Good';
echo '<br>';
}
?>

<?php
for ($num = 1; $num <= 100; $num++) {
if ($num % 3 == 0 && $num % 5 == 0)
echo "MaxGood" . '</br>';
elseif ($num % 3 == 0)
echo "Max" . '</br>';
elseif ($num % 5 == 0)
echo 'Good' . '</br>';
else
echo $num . '</br>';
}

Related

window.location.href doesn't submit form [duplicate]

This question already has answers here:
JavaScript post request like a form submit
(32 answers)
How can I submit a form using JavaScript?
(10 answers)
Closed 4 years ago.
I used the Javascript below.
If the time is finished it must redirect to the next page "result.php" by submitting a form. But unfortunately the page displayed is empty.
below my form which must send to the next page "result.php".
I want to have how to send this form and redirect to the next page.
how to send the form with this Javascript to make my page work?
var hoursleft = 3;
var minutesleft = 0;
var secondsleft = 0;
var end1;
var progressBar = document.getElementById('progressBar');
if (localStorage.getItem("end1")) {
end1 = new Date(localStorage.getItem("end1"));
} else {
end1 = new Date();
end1.setMinutes(end1.getMinutes() + minutesleft);
end1.setSeconds(end1.getSeconds() + secondsleft);
end1.setHours(end1.getHours() + hoursleft);
}
progressBar.max = end1 - new Date();
var counter = function() {
var now = new Date();
var diff = end1 - now;
diff = new Date(diff);
var sec = parseInt((diff / 1000) % 60)
var mins = parseInt((diff / (1000 * 60)) % 60)
var hours = parseInt((diff / (1000 * 60 * 60)) % 24);
if (hours < 10) {
hours = "0" + hours;
}
if (mins < 10) {
mins = "0" + mins;
}
if (sec < 10) {
sec = "0" + sec;
}
if (now >= end1) {
clearTimeout(interval);
localStorage.setItem("end", null);
localStorage.removeItem("end1");
localStorage.clear();
if (confirm("TIME UP!"))
window.location.href = "result.php";
//alert("Time finished!");
} else {
progressBar.value = progressBar.max - (end1 - now);
localStorage.setItem("end1", end1);
document.getElementById('hours').innerHTML = "<b>" + hours + "</b> " + ":";
document.getElementById('mins').innerHTML = "<b>" + mins + "</b> " + ":";
document.getElementById('sec').innerHTML = "<b>" + sec + "</b>";
}
}
var interval = setInterval(counter, 1000);
<form class="form-horizontal" role="form" id='question' name="question" method="post" action="result.php">
<?php
$number_question=1;
$limit=10;
$row = mysqli_query( $conn, "select id,question_name,image from testquestion where email = '".$email."' ");
$total = mysqli_num_rows( $row );
$remainder = $total/$number_question;
$i = 0;
$j = 1; $k = 1;
?>
<?php while ( $result = mysqli_fetch_assoc($row) ) {
if ( $i != 0)
echo "<div id='question_splitter_$j'>";
if ( $i == 0)
echo "<div class='cont' id='question_splitter_$j'>";?>
<div id='question<?php echo $k;?>' >
<p class='questions' id="qname<?php echo $j;?>"> <?php echo $k?>.<?php echo $result['question_name'];?>
<input type="checkbox" id="review" name="review" />
<label style="color:#0080ff">Review</label></p>
</br>
<?php echo '<img src="data:image/png;base64,'.($result['image']).'" />'; ?></br><br/><br/>
<input type="text" id="answer<?php echo $result['id'];?>" name="<?php echo $result['id'];?>" placeholder="Enter your answer" />
<br/>
<br/>
</div>
<?php
$i++;
if ( ( $remainder < 1 ) || ( $i == $number_question && $remainder == 1 ) ) {
echo "<button id='".$j."' class='next btn btn-primary' type='submit'>Finish</button>";
echo "</div>";
} elseif ( $total > $number_question ) {
if ( $j == 1 && $i == $number_question ) {
echo "<button id='".$j."' class='next btn btn-primary' type='button'>Next</button>";
echo "</div>";
$i = 0;
$j++;
} elseif ( $k == $total ) {
echo " <button id='".$j."' class='previous btn btn-primary' type='button'>Previous</button>
<a href='review.php'><button id='review.php' class='previous btn btn-primary' type='button'>Review</button></a>
<button id='".$j."' class='next btn btn-primary' type='submit'>Finish</button>";
echo "</div>";
$i = 0;
$j++;
} elseif ( $j > 1 && $i == $number_question ) {
echo "<button id='".$j."' class='previous btn btn-primary' type='button'>Previous</button>
<button id='".$j."' class='next btn btn-primary' type='button' >Next</button>";
echo "</div>";
$i = 0;
$j++;
}
}
$k++;
}
?>
<br/>
<br/>
<br/>
</form>
You have to use document.getElementById('question').submit(); with window.location.href = "result.php"; the POST data isnt posted

how to get last logged in admin time and date after login me in php

I have 5 Admin in the database. So now I want to get the last user logged in date and time detail. Who worked before me.means after my logged in the dashboard I want to see previous USER login and logout time and date.
I am able to get my (current user) login date and time. But I don't have an idea how can I get previous user(Admin) login and logout date/time detail.
I am sending my login.php code.Where i wrote current user time/date code.Please see the code and give me suggestion how can i get previous user login and logout date/time.
Thanks.
<?php include "db.php"; ?>
<?php session_start(); ?>
<?php
if(isset($_POST['login'])){
$username = $_POST['username'];
$password = $_POST['password'];
$username = mysqli_real_escape_string($connection,$username);
$password = mysqli_real_escape_string($connection,$password);
$password = md5($password);
$login_query = "SELECT * FROM users WHERE user_username = '$username'" ;
$result_query = mysqli_query($connection,$login_query);
$count = mysqli_num_rows($result_query);
if(!$count){
die("QUERY FAILED". mysqli_error($connection));
}
while($row= mysqli_fetch_array($result_query)){
$log_user_id = $row['user_id'];
$log_user_username = $row['user_username'];
$log_user_password = $row['user_password'];
$log_user_firstname = $row['user_firstname'];
$log_user_lastname = $row['user_lastname'];
$log_user_role = $row['user_role'];
$log_user_time = $row['time'];
$log_user_ip = $row['ip'];
}
if($username !== $log_user_username && $password !== $log_user_password){
header ("location: ../index.php");
}elseif($username == $log_user_username && $password == $log_user_password){
if($count == 1) {
$_SESSION['username']=$log_user_username;
$_SESSION['firstname']=$log_user_firstname;
$_SESSION['lastname']=$log_user_lastname;
$_SESSION['user_role']=$log_user_role;
$_SESSION['last_login'] = $log_user_time;
$_SESSION['last_login_ip'] = $log_user_ip;
date_default_timezone_set("Asia/Kolkata");
$current_date = date("F d, Y, h:i:s A");
$ip = $_SERVER['REMOTE_ADDR'];
$query = "UPDATE users SET time= NOW() ,ip='$ip' WHERE user_username='$username'";
mysqli_query($connection,$query);
$last_login_date = $_SESSION['last_login'];
$last_login_date2 = date('F d, Y, h:i:s A');
$diffs = abs(strtotime($last_login_date2) - strtotime($last_login_date));
$year = floor($diffs / (365*60*60*24));
$month = floor(($diffs - $year * 365*60*60*24) / (30*60*60*24));
$day = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24)/ (60*60*24));
$hour = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24)/ (60*60));
$minute = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24 - $hour*60*60)/ 60);
$second = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24 - $hour*60*60 - $minute*60));
if($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 0 AND $second < 30 ) {
$time1 = 'Just now';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 0 ) {
$time1 = 'few seconds ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 1) {
$time1 = '1 minute ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 ) {
$time1 = $minute . ' minutes ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 1 ) {
$time1 = '1 hour ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 ) {
$time1 = $hour . ' hours ago';
} elseif($year == 0 AND $month == 0 AND $day == 1 ) {
$time1 = 'Yesterday';
} elseif($year == 0 AND $month == 0 ) {
$time1 = $day . ' days ago';
} elseif($year == 0 AND $month == 1 ) {
$time1 = '1 month ago';
} elseif($year == 0) {
$time1 = $month . ' months ago';
} elseif($year == 1 ) {
$time1 = '1 year ago';
} else {
$time1 = $year . ' years ago';
}
}
if($_SESSION['last_login_ip'] != $_SERVER['REMOTE_ADDR']) {
$last_login_ip = "from this IP address (".$_SERVER['REMOTE_ADDR'].")";
}
else {
$last_login_ip = "IP address ".$_SESSION['last_login_ip'];
}
}
header ("location: ../admin");
}else{
header ("location: ../index.php");
}
?>
So after my logged in on Dashboard (index.php) the page i did echo the variables and i can able to see current user login date/time detail.Which is my login detail.
Please give me suggestion how can i get previous user login and logout detail.
Thanks
notification.php
<span class="pull-right text-muted small"><em></em></span></a>
<?php
date_default_timezone_set("Asia/Kolkata");
$last_login_date = date("F d, Y, h:i:s A");
$last_login_date = $_SESSION['last_login'];
$last_login_date2 = date('F d, Y, h:i:s A');
$diffs = abs(strtotime($last_login_date2) - strtotime($last_login_date));
$year = floor($diffs / (365*60*60*24));
$month = floor(($diffs - $year * 365*60*60*24) / (30*60*60*24));
$day = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24)/ (60*60*24));
$hour = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24)/ (60*60));
$minute = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24 - $hour*60*60)/ 60);
$second = floor(($diffs - $year * 365*60*60*24 - $month*30*60*60*24 - $day*60*60*24 - $hour*60*60 - $minute*60));
if($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 0 AND $second < 30 ) {
$time1 = 'Just now';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 0 ) {
$time1 = 'few seconds ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 AND $minute == 1) {
$time1 = '1 minute ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 0 ) {
$time1 = $minute . ' minutes ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 AND $hour == 1 ) {
$time1 = '1 hour ago';
} elseif($year == 0 AND $month == 0 AND $day == 0 ) {
$time1 = $hour . ' hours ago';
} elseif($year == 0 AND $month == 0 AND $day == 1 ) {
$time1 = 'Yesterday';
} elseif($year == 0 AND $month == 0 ) {
$time1 = $day . ' days ago';
} elseif($year == 0 AND $month == 1 ) {
$time1 = '1 month ago';
} elseif($year == 0) {
$time1 = $month . ' months ago';
} elseif($year == 1 ) {
$time1 = '1 year ago';
} else {
$time1 = $year . ' years ago';
}
if($_SESSION['last_login_ip'] != $_SERVER['REMOTE_ADDR']) {
$last_login_ip = "From this IP address (".$_SERVER['REMOTE_ADDR'].")";
}
else {
$last_login_ip = "IP address ".$_SESSION['last_login_ip'];
}
?>
<a href="#" class="list-group-item">
<i class="fa fa-user"></i> User Name
<span class="pull-right text-muted small"><em><?php echo $_SESSION['username']; ?></em>
</span>
</a>
<a href="#" class="list-group-item">
<i class="fa fa-sign-in"></i> Last Login Time
<span class="pull-right text-muted small"><em><?php echo $time1; ?></em>
</span>
</a>
<a href="#" class="list-group-item">
<i class="fa fa-sign-out"></i> Last Logout Time
<span class="pull-right text-muted small"><em>27 minutes ago</em>
</span>
</a>
<a href="#" class="list-group-item">
<i class="fa fa-tasks fa-fw"></i> User Ip
<span class="pull-right text-muted small"><em><?php echo $last_login_ip; ?></em>
</span>
</a>
here iam getting admin login date and time detail.But i unable to get previous visitor login detail . i can just see only my login time/date detail after login in here.
So please help.
I can give you the basic logic for this. In this you can set the session variable that it will act as flag.
// start session
session_start();
// if not yet logged in update database
if(!isset($_SESSION['logged']))
{
// update your database here
$query = "UPDATE table
SET LastLogin=now()
WHERE ID='$ID'";
$result = mysql_query($query); // etc...
// if the table was updated
if($result === true)
{
// then create a session var
$_SESSION['logged'] = 1;
}
}
OR
You can insert the user id, date and time at the time of login and get it back as last id that will be your previous login.
Create one activity table in database. Click the link to view example here->
User Activity Table Structure
Every time when any user logs in, just enter the login time and user id along with other required details in the activity table. At the time of login you can change the login status to deactivated for that particular session.
Next time when any user logs in you can get the last login time from database and current time from PHP date() function. Just get the time difference between those 2 dates and you will get the last login time and duration by previous user.
That's it.
Hope this would work.

Calendar PHP - error at first day

Javascript - functions enable switch months
function goLastMonth(month, year)
{
if (month == 1)
{
--year;
month = 13;
}
--month
var monthstring= ""+month+"";
var monthlength = monthstring.length;
if(monthlength <=1)
{
monthstring = "0" + monthstring;
}
document.location.href ="<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
}
function goNextMonth(month, year)
{
if(month == 12)
{
++year;
month = 0;
}
++month
var monthstring= ""+month+"";
var monthlength = monthstring.length;
if(monthlength <=1)
{
monthstring = "0" + monthstring;
}
document.location.href ="<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
}
</script>
PHP - creating calendar
if (isset($_GET['day']))
{
$day = $_GET['day'];
}
else
{
$day = date("j");
}
if(isset($_GET['month']))
{
$month = $_GET['month'];
}
else
{
$month = date("n");
}
if(isset($_GET['year']))
{
$year = $_GET['year'];
}
else
{
$year = date("Y");
}
$currentTimeStamp = strtotime( "$day-$month-$year");
$monthName = date("F", $currentTimeStamp);
$numDays = date("t", $currentTimeStamp);
$counter = 0;
?>
<table border='1'>
<tr>
<td><input style='width:50px;' type='button' value='<'name='previousbutton' onclick ="goLastMonth(<?php echo $month.",".$year?>)"></td>
<td colspan='5'><?php echo $monthName.", ".$year; ?></td>
<td><input style='width:50px;' type='button' value='>'name='nextbutton' onclick ="goNextMonth(<?php echo $month.",".$year?>)"></td>
</tr>
<tr>
<td width='50px'>Poniedziałek</td>
<td width='50px'>Wtorek</td>
<td width='50px'>Środa</td>
<td width='50px'>Czwartek</td>
<td width='50px'>Piątek</td>
<td width='50px'>Sobota</td>
<td width='50px'>Niedziela</td>
</tr>
<?php
echo "<tr>";
for($i = 1; $i < $numDays+1; $i++, $counter++)
{
$timeStamp = strtotime("$year-$month-$i");
if($i == 1)
{
$firstDay = date("w", $timeStamp);
for($j = 0; $j < $firstDay-1; $j++, $counter++)
{
echo "<td> </td>";
}
}
if($counter % 7 == 0)
{
echo"</tr><tr>";
}
$monthstring = $month;
$monthlength = strlen($monthstring);
$daystring = $i;
$daylength = strlen($daystring);
echo "<td align='center' ";
echo "><a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$daystring."&year=".$year."'>".$i."</a></td>";
}
echo "</tr>";
?>
</table>
The calender works well, but when the first day in the month is Sunday, then it starts to count the days from Monday (for example May 2016). How do I fix this?
Try the following code
if($i == 1)
{
$firstDay = date("w", $timeStamp) - 1;
if ($firstDay < 0)
$firstDay = 6;
// Now monday is 0, and sunday is 6
for($j = 0; $j < $firstDay; $j++, $counter++) // remove the -1 in $firstDay-1
{
echo "<td> </td>";
}
}
The first column of your calendar is Monday, and the last column is Sunday. $firstDay = date("w", $timeStamp) returns a number from 0-6 where 0 is Sunday, and 6 is Saturday. So I change it so that 0 is Monday, and 6 is Sunday.
So now the loop for($j = 0; $j < $firstDay; $j++, $counter++)
If Monday, then since it's the first column you don't want the loop to run. On Monday, $firstDay is 0 so the loop doesn't run. If Tuesday, then $firstDay is 1 and the loop runs once to make the empty space for Monday.

How to set custom month start date and end date in php

my salary month start in previous month 26th and ends in this month 25
ie February month is 2016-01-26 to 2016-02-25.
i need the out put as below
$date = '2014-02-27';
echo date('F', strtotime($date)); = "March" //if date > 25 print next month
I believe what you have to do is get the day:
$date = '2014-02-27';
$day = date('d', strtotime($date));
Then see if bigger than 26, increase one month
$month = date('F', strtotime($date));
if($day > 26)
{
$ts = mktime(0, 0, 0, date("n", strtotime($date)) + 1, 1);
$tmpNewDate = date("Y-m-d H:i:s", $ts);
$month = date('F', strtotime($date));
}
echo $month;
Try this one, I have prepared several date inputs :
<?php
$input_date = '2014-2-2';
// $input_date = '2014-2-27';
// $input_date = '2014-12-27';
$date = DateTime::createFromFormat('Y-m-d', $input_date);
$next_month = null;
$year = $date->format('Y');
$next_month = $date->format('m');
if ($date->format('d') >= 26)
$next_month = intval($date->format('m')) + 1;
if ($next_month == 13){
$next_month = 1;
$year = intval($year) + 1;
}
echo "next month = " . $next_month . ", year = " . $year;
?>

creating custom time algorith like timeago in javascript

I want a custom time algorithm like javascript. I was able to achieve this using php but am not able to do the same with javascript!
WHat i had in php was like.....
PHP FIDDLE(i want something like this in js output)
function getTime($mytime){
// $mytime is a datetime sql database format time
$thetime = strtotime($mytime);
$q = new DateTime($mytime);
$current_time = date('Y-m-d H:i:s',time());
$w = new DateTime($current_time);
$result = $w ->diff($q);
$string = "";
$year = $result->y;
$month = $result->m;
$day = $result->d;
$hour = $result->h;
$min = $result->i;
$sec = $result->s;
if($year==0){
if($month==0){
if($day==0){
if($hour==0){
if($min==0){
if($sec <30){
$string .= "Just now";}else if($sec<60){ $string .= "A few seconds ago";}
}else{
if($min<5){$string .="A few minutes ago";}else if($min>=5){$string .=$min . ' minutes ago'; }
}}else{
if($hour<6){$string .=$hour . ' hours ago';}else if($hour>=6){
$chour = (date('H',$thetime));
if( ($chour>=5)&&($chour<=17) ){$string .= 'Today at ' .date('h.m a',$thetime);}else{$string .= 'Tonight at '. date('h.m a',$thetime);}}
}}else{
if($day==1){$string .= "Yesterday at " . date('h:m a',$thetime);}else if(($day>1) && ($day<=7)){$string .= $day . " days ago";}else if($day>7){$string .= date('M, d',$thetime) . ' at '. date('h:i a',$thetime);}
}}else{
if($month<5){$string .=$month . ' months ago'; }else if($month>=5){$string .= date('M,d',$thetime) .' at ' . date('h.i a',$thetime) ;}
}}else{
$string .=date('dS M , Y',$thetime) .' at '. date('h.i a',$thetime);
}
return $string;
}
What i thought was to dynamically update time using javascript! so i did this......
$(function(){
var $second = 1000;
var $minute = $second * 60;
var $hour = $minute * 60;
var $day = $hour * 24;
setInterval(function(){
$('.comment').each(function(){
var $this = $(this);
var $time = new Date($this.attr('datetime'));
var $now = new Date();
var $distance = $now - $time;
var $days = Math.floor($distance / $day);
var $hours = Math.floor(($distance % $day) / $hour);
var $minutes = Math.floor(($distance % $hour) / $minute);
var $seconds = Math.floor(($distance % $minute) / $second);
var $time_string = '';
if($days > 0) {
$time_string = $time_string + $days + ' Day(s) ';
}
if($hours > 0) {
$time_string = $time_string + $hours + ' Hour(s) ';
}
if($minutes > 0) {
$time_string = $time_string + $minutes + ' Minute(s) ';
}
if($seconds > 0) {
$time_string = $time_string + $seconds + ' Second(s)';
}
$this.text($time_string);
});
}, 1000);
});
but his outputs something like this fiddle here.
What i wanted is like my prev. php output i.e X min ago or X seconds ago or 12 May 2014 etc how do i implement my above php code with this js ?

Categories