How to show calculated history in php - php

I'm creating a calculator using PHP and would like to show what has been calculated (list the calculated history) but I have no idea how to store and display the information.
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Basic PHP Calculator</title>
</head>
<body>
<?php
//var_export($_POST);
//echo "<br>";
$buttons=[1,3,5,'+',7,9,0,'/','C','='];
$pressed='';
if(isset($_POST['pressed']) && in_array($_POST['pressed'],$buttons)){
$pressed=$_POST['pressed'];
}
$stored='';
if(isset($_POST['stored']) && preg_match('~^(?:[\d.]+[*/+-]?)+$~',$_POST['stored'],$out)){
$stored=$out[0];
}
$display=$stored.$pressed;
//echo "$pressed & $stored & $display<br>";
if($pressed=='C'){
$display='';
}elseif($pressed=='=' && preg_match('~^\d*\.?\d+(?:[*/+-]\d*\.?\d+)*$~',$stored)){
$display.=eval("return $stored;");
}
echo "<form action=\"\" method=\"POST\">";
echo "<table style=\"width:300px;border:solid thick black;\">";
echo "<tr>";
echo "<td colspan=\"4\">$display</td>";
echo "</tr>";
foreach(array_chunk($buttons,4) as $chunk){
echo "<tr>";
foreach($chunk as $button){
echo "<td",(sizeof($chunk)!=4?" colspan=\"4\"":""),"><button name=\"pressed\" value=\"$button\">$button</button></td>";
}
echo "</tr>";
}
echo "</table>";
echo "<input type=\"hidden\" name=\"stored\" value=\"$display\">";
echo "</form>";
?>
</body>
</html>
Expected to get a list of what has been calculated like:
Calculate history:
2+2=4
3+4=7

Append results in SESSION only when isequalto(=) button pressed:
session_start();
elseif($pressed=='=' && preg_match('~^\d*\.?\d+(?:[*/+-]\d*\.?\d+)*$~',$stored)){
$display.=eval("return $stored;");
$_SESSION["list"].=$display."<br>";
}
And finally display the history:
<?php echo $_SESSION["list"];?>

Related

Saving quiz results to database

I have created a multiple choice quiz/questionnaire. The questions and answers are pulling from the database and I have sessions setup on the webpage. At the minute the user can only print the webpage when they get their results.
I am using phpmyadmin and php for the quiz. I am currently pulling from one database table called questions with the fields, questionID, question, ansYes and ansNo.
<?
//Always start this first
session_start();
include ("dbConnect.php");
if ( isset( $_SESSION['user_email'] ) ) {
//Grab user data from the database using user email
} else {
// Redirect them to the login page
header("login.php");
}
$sql = "SELECT * FROM questions";
$dbQuery = $db->prepare($sql);
$dbQuery->execute();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>List</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<style>
/* CSS animation */
#-webkit-keyframes fadeIn {
0% {
opacity:0;
transform: scale(0.6);
}
100% {
opacity:100%;
transform: scale(1);
}
}
#keyframes fadeIn {
0% { opacity:0; }
100% { opacity:100%; }
}
</style>
<?php include("navIn.php"); ?>
<body>
<div class="container-fluid">
<br>
<?php
/*Printing out questions*/
if(!isset($_POST["submitForm"])) {
echo "<form method='post' action='checklist.php'>";
if (isset($_SESSION["first_name"])) {
echo '<span style="color:#000000;position:left; left:1060px;">Hello '.$_SESSION["first_name"].'!<br></span>';
}
while($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) {
echo"<div class=container-fluid text-center>";
echo "<h4>" . $dbRow["question"] . "</h4>";
echo "<h4><input type='radio' required name='" . $dbRow["questionID"] . "' value='Yes'> Yes <br></h4?";
echo "<h4><input type='radio' required name='" . $dbRow["questionID"] . "' value='No'> No <br><br></h4>";
echo "</div>";
}
echo "<input type='submit' name='submitForm' value='Submit'>";
echo "</form>";
echo"</div>";
} else {
if (isset($_SESSION["first_name"])) {
echo '<span style="color:#000000;position:left; left:1060px;">Hello '.$_SESSION["first_name"].'!<br></span>';
}
/*printing out results from questions */
echo"<div class=container-fluid text-center>";
while($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) {
if($_POST[$dbRow["questionID"]] == "Yes") {
echo '<h4 span style="color:blue;"> '.$dbRow["question"].'</span>';
/*echo "<h4>" .$dbRow["question"]."<br></h4><br>";*/
echo "<h4>" . $dbRow["ansYes"] . "</h4><br>";
} else {
echo '<h4 span style="color:blue;"> '.$dbRow["question"].'</span>';
/* echo "<h4>" .$dbRow["question"]."<br></h4><br>";*/
echo "<h4>" . $dbRow["ansNo"] . "</h4><br>";
}
}
echo"Click the button bellow to print this page!";
echo'<br><form> <input type=button value="Print me!" onClick="javascript:window.print()"> </form>';
echo"</div>";
}
?>
</div>
</body>
<?php include ("footer.php"); ?>
</html>
I would like to be able to save the users results so anytime they access checklist.php their results are there instead of having to print the page out.

Get logged in username with php from database

in the first php code !$fgmembersite->CheckLogin() works perfectly. also next in the html code <?= $fgmembersite->UserFullName(); ?>! works perfectly. But in the second php it won't work correctly. Am I doing something wrong in that code? if($row['name']=="$fgmembersite->UserFullName()")
<?PHP
require_once("./include/membersite_config.php");
if(!$fgmembersite->CheckLogin())
{
$fgmembersite->RedirectToURL("login.php");
exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
<title>Home page</title>
<link rel="STYLESHEET" type="text/css" href="style/fg_membersite.css">
</head>
<body>
<div id='fg_membersite_content'>
<h2>Home Page</h2>
Welcome back <?= $fgmembersite->UserFullName(); ?>!
</div>
</body>
<?php
include("db.php");
$select=mysql_query("select * from commenttable");
while($row=mysql_fetch_array($select))
{
if($row['name']=="$fgmembersite->UserFullName()")
{
echo "<div id='sty'>";
echo "<img src='files/fav icon.png'"."' width='50px' height='50px' align='left' />";
echo "<div id='nameid'>".$row['name']."</div>";
echo "<div id='msgid'>".$row['message']."</div>";
echo "</div><br />";
}else{
ob_start();
echo "<div id='sty'>";
echo "<img src='files/fav icon.png'"."' width='50px' height='50px' align='left' />";
echo "<div id='nameid'>".$row['name']."</div>";
echo "<div id='msgid'>".$row['message']."</div>";
echo "</div><br />";
ob_end_clean();
}
}
?>
You have an error with your PHP, you have enclosed what should be PHP code within a string causing it to output as you have typed.
Change the following line:
if($row['name']=="$fgmembersite->UserFullName()")
To the following:
if($row['name']==$fgmembersite->UserFullName())

How to update a row in a field one at a time on php?

I have a problem on updating the status field on tblitemlist. I wanted to display all status = 'PENDING' and when it's done, what I want to do is update its status to finished with just clicking a button. I wanted it one by one and not clicking a button and update everything at the same time. Please help me
here's what i did:
$query_update = mysql_query("UPDATE tblitemlist SET status = 'FINISHED'") or die(mysql_error());
I tried this one. There's no error but when I checked my database, the status is not changing.
$id=$_GET['id'];
$query_update = mysql_query("UPDATE tblitemlist SET status = 'FINISHED' WHERE id='$id'") or die(mysql_error());
Gonna show my codes..
inner.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
include("../connection/connection.php");
//
if(!isset($_SESSION['u']))
{
header("location../resto/index.php");
$result = mysql_query("SELECT tblcustomer.fname, tblitemlist.item, tblitemlist.category, tblitemlist.date_ordered FROM tblitemlist INNER JOIN tblcustomer ON tblcustomer.cust_id=tblitemlist.cust_id WHERE tblitemlist.status='PENDING' ORDER BY tblcustomer.cust_id" ) or die(mysql_error());
echo "<table width='900' border='1' align='center'>";
echo "<tr>";
//echo "<td>Code</td>";
echo "<td align='center' style='font-size:20px;'>Name</td>";
echo "<td align='center' style='font-size:20px;'>Item</td>";
echo "<td align='center' style='font-size:20px;'>Category</td>";
echo "<td align='center' style='font-size:20px;'>Date</td>";
echo "<td align='center' style='font-size:20px;'>Status</td>";
echo "<tr>";
while ($row=mysql_fetch_array($result))
{
echo "<tr>";
//echo "<td>" .$row['code'];
echo "<td>" .$row['fname'];
echo "<td>" .$row['item'];
echo "<td>" .$row['category'];
echo "<td>" .$row['date_ordered'];
?>
<td>DONE</td>
<?php
echo "<tr>";
}
echo "</table>";
}
?>
</p>
</body>
</html>
update.php
<?php error_reporting(0); ?>
<?php
include ("../connection/connection.php");
$id=$_GET['id'];
$query_update = mysql_query("UPDATE tblitemlist SET status='FINISHED' WHERE id='$id'") or die(mysql_error());
if($query_update){
echo '<script type="text/javascript">alert("COOKED");
window.location="index.php";
</script>';
}
else{
echo '<script type="text/javascript">alert("UNSUCCESSFULL");
window.location="index.php";
</script>';
}
?>
You have to add WHERE statement to specify which table item has to be updated. First you have to provide the query with the ID. For example, each button would post the 'id_to_update' through a form.
$query_update = mysql_query("UPDATE tblitemlist SET status = 'FINISHED' WHERE id = ".$_POST['id_to_update']) or die(mysql_error());
Is that what you want ?

Get values of a checked box list,one at a time with PHP

Good Day to you.This is what I have been trying to do.
I have a php page where I have drop down list with options as lecturers and students.
Once I select any option, details regarding lecturers or students will be displayed in a div using ajax.
Code is as follows.(This will be called in onchange event of the drop down list with a javascript.
<?php
session_start();
require "configuration.php";
$q = intval($_GET['q']);
if($q==1)
{
$sql="SELECT * FROM temp_instructor_log";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th></th>
<th><u><B>NIC</u></B></th>
<th><u><B>Name</u></B></th>
<th><u><B>Email</u></B></th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>"?>
<input type="checkbox" name="emplist[]" value="<?php echo $row['nic']?>">
<?php echo "</td>";
echo "<td>" . $row['nic'] . "</td>";
echo "<td>" . $row['firstName'] ." ".$row['lastName']. "</td>";
echo "<td>" . $row['email']."</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
}
?>
Its working and giving me the display with checkboxes at the very beginning.
<?php session_start();
require "configuration.php";
?>
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Manage courses</title>
<script type="text/javascript">
</script>
</head>
<body>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
if (isset($_POST['btnAccept']))
{
$utype=$_POST['category'];
$arr = $_POST['emplist'];
foreach( $arr as $item)
{
echo $item . "</br>"; /* Will print 1,2 and 3 (mind newlines)*/
}
First I tried to echo the values after checking some of the checkboxes.But it didnt work out.Your kind consideration given on this is highly appreciated.
Thanks in advance.

How to color even/odd table rows in PHP

OK I have a question I have this code which I will list below. I need to make the even rows a light blue and the odd a white. Now it doesnt show up so I am assume I am doing soemthing wrong. Now do I need to do the order in it so that way the rows will look the way I need it to do?
<html>
<head>
<title> Html Tables</title>
</head>
<body>
<?php
echo "<table width=\"50%\" cellpadding=\"2\" cellspacing=\"2\" border=\"1\">";
echo "<tr bgcolor=\"#FFFFFF\">";
$rowcount=0;
for($x=1;$x<=12;$x++){
echo " <td align=\"center\" style=\"width:100px\">".$x."</td>\n";
if ($x%4==0) {
if ($rowcount%2==0){
echo "</tr>";
echo "<tr bgcolor=\"#5CCDC9\">\n";
}
else{
echo "</tr>";
echo "<tr bgcolor=\"#FFFFFF\">\n";
}
$rowscount++;
}
}
echo "</tr>";
echo "</table>";
?>
</body>
</html>
ok I am trying to understand this better after reading a few things this is my new code
<html>
<head>
<title> Html Tables</title>
<style type=<\"text/css\">
.even { bgcolor:#5CCDC9; }
.odd { bgcolor:#FFFFFF; }
</style>
</head>
<body>
<?php
echo "<table width=\"50%\" cellpadding=\"2\" cellspacing=\"2\" border=\"1\">";
echo "<tr bgcolor=\"#FFFFFF\">";
$rowcount=0;
for($x=1;$x<=12;$x++){
echo " <td align=\"center\" style=\"width:100px\">".$x."</td>\n";
if ($x%4==0) {
if ($rowcount%2==0){
echo "</tr>";
echo "<tr class=\"even\">\n";
}
else{
echo "</tr>";
echo "<tr class=\"odd\">\n";
}
$rowcount++;
}
}
echo "</tr>";
echo "</table>";
?>
</body>
</html>
now i just dont understand how to write it in PHP I am reading and trying to figure out how to make sense of it. Sorry I am a newbie at this.
You are using $rowcount in your conditional and initialization but you are using $rowscount (with in "s") in your incrementing.
Note: you should really be using CSS for that rather than the bgcolor property.
you have a typo...change $rowscount++ to $rowcount++
The easiest way to do this is with CSS. You can use the nth-child rule to select odd and even rows of the table and colour them differently. That way you don't need the modulus operator if statement you're using.
See this fiddle for an example.
Try this. It's a little bit cleaner and it works:
<html>
<head>
<title> Html Tables</title>
</head>
<body>
<table width="50%" cellpadding="2" cellspacing="2" border="1">
<?php
for($x = 1; $x <= 12; $x++) {
if ($x % 2 == 0) {
echo ' <tr bgcolor="#5CCDC9">', PHP_EOL;
} else {
echo ' <tr bgcolor="#FFFFFF">', PHP_EOL;
}
echo ' <td align="center" style="width:100px">' . $x . '</td>', PHP_EOL;
echo ' </tr>', PHP_EOL;
}
?>
</table>
</body>
</html>

Categories