Cytat(Salvation @ 3.02.2023, 00:15:28 )

Ten kod na pewno działa tak jak chcesz?
Tak. Kod działa tak jak tego oczekuję. Po wciśnięciu klawisza "=" wykonywane jest albo tylko dodawanie, albo tylko odejmowanie, albo tylko mnożenie, albo tylko dzielenie i wyświetlany jest wynik w "Wynik" w kalkulator.php. Wynik jest także przepisywany do $_SESSION['liczba'] i wyświetlany w "Działanie" w kalkulator.php. I znów wykonujemy kolejne działanie i naciskamy "=". Oto całość kodu:
kalk.php
<?php
if (isset($_GET['wynik']) ) { // if (is_numeric($_GET['wynik'])) {
if (!isset($_SESSION['liczba'])) {
$_SESSION['liczba'] = '';
}
$_SESSION['liczba'] .= $_GET['wynik'];
// }
if ($_GET['wynik']==='C'){
}
if ($_GET['wynik']==='='){
$tablica=array('+' => "dodawanie", '-' => "odejmowanie", '*' => "mnozenie", '/' => "dzielenie" );
foreach($tablica as $klucz => $wartosc){
$wartosc=explode($klucz,$_SESSION['liczba']);
if($klucz=='+'){
$_SESSION[$tablica[$klucz]] = $wartosc[0] + $wartosc[1];
}
elseif($klucz=='-'){
$_SESSION[$tablica[$klucz]] = $wartosc[0] - $wartosc[1];
}
elseif($klucz=='*'){
$_SESSION[$tablica[$klucz]] = $wartosc[0] * $wartosc[1];
}
elseif($klucz=='/'){
$_SESSION[$tablica[$klucz]] = $wartosc[0] / $wartosc[1];
}
$_SESSION['liczba'] = $_SESSION[$tablica[$klucz]];
}
}
}
include 'kalkulator.php';
}
?>
kalkulator.php
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>KALKULATOR</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' media='screen' href='main1.css'>
<script src='main.js'></script>
</head>
<body>
<table class="tabela">
<tr>
<td colspan="4" class="wyswietlacz">
Działanie: <?php
if (isset($_SESSION['liczba'])){ echo $_SESSION['liczba']; }
?>
<br>
Wynik:
<?php
if (isset($_SESSION['dodawanie'])){ echo $_SESSION['dodawanie']; unset($_SESSION['dodawanie']);
}
if (isset($_SESSION['odejmowanie'])){ echo $_SESSION['odejmowanie']; unset($_SESSION['odejmowanie']);
}
if (isset($_SESSION['mnozenie'])){ echo $_SESSION['mnozenie']; unset($_SESSION['mnozenie']);
}
if (isset($_SESSION['dzielenie'])){ echo $_SESSION['dzielenie']; unset($_SESSION['dzielenie']);
}
?>
</td>
</tr>
<tr>
<td>
<form action="kalk.php" method="get">
<input type=submit value="1" class="input"/>
<input type="hidden" name="wynik" value= "1" />
</form>
</td>
<td>
<form action="kalk.php" method="get">
<input type=submit value="2" class="input"/>
<input type="hidden" name="wynik" value="2" />
</form>
</td>
<td>
<form action="kalk.php" method="get">
<input type=submit value="3" class="input"/>
<input type="hidden" name="wynik" value="3" ?>
</form>
</td>
<td>
<form action="kalk.php" method="get">
<input type=submit value="C" class="input"/>
<input type="hidden" name="wynik" value="C" ?>
<input type="hidden" name="zn" value="kasowanie"/>
</form>
</td>
</tr>
<tr>
<td>
<form action="kalk.php" method="get">
<input type=submit value="4" class="input"/>
<input type="hidden" name="wynik" value="4" ?>
</form>
</td>
<td>
<form action="kalk.php" method="get">
<input type=submit value="5" class="input"/>
<input type="hidden" name="wynik" value="5" ?>
</form>
</td>
<td>
<form action="kalk.php" method="get">
<input type=submit value="6" class="input"/>
<input type="hidden" name="wynik" value="6" ?>
</form>
</td>
<td>
<form action="kalk.php" method="get">
<input type=submit value="-" class="input"/>
<input type="hidden" name="wynik" value="-" ?>
<input type="hidden" name="zn" value="minus"/>
</form>
</td>
</tr>
<tr>
<td>
<form action="kalk.php" method="get">
<input type=submit value="7" class="input"/>
<input type="hidden" name="wynik" value="7" ?>
</form>
</td>
<td>
<form action="kalk.php" method="get">
<input type=submit value="8" class="input"/>
<input type="hidden" name="wynik" value="8" ?>
</form>
</td>
<td>
<form action="kalk.php" method="get">
<input type=submit value="9" class="input"/>
<input type="hidden" name="wynik" value="9" ?>
</form>
</td>
<td>
<form action="kalk.php" method="get">
<input type=submit value="+" class="input"/>
<input type="hidden" name="wynik" value="+" ?>
<input type="hidden" name="zn" value="plus"/>
</form>
</td>
</tr>
<tr>
<td>
<form action="kalk.php" method="get">
<input type=submit value="." class="input"/>
<input type="hidden" name="wynik" value="." ?>
</form>
</td>
<td>
<form action="kalk.php" method="get">
<input type=submit value="/" class="input"/>
<input type="hidden" name="wynik" value="/" ?>
<input type="hidden" name="zn" value="dzielenie"/>
</form>
</td>
<td>
<form action="kalk.php" method="get">
<input type=submit value="0" class="input"/>
<input type="hidden" name="wynik" value="0" ?>
</form>
</td>
<td>
<form action="kalk.php" method="get">
<input type=submit value="=" class="input"/>
<input type="hidden" name="wynik" value="=" ?>
<input type="hidden" name="zn" value="rowna"/>
</form>
</td>
</tr>
<tr>
<td>
<form action="kalk.php" method="get">
<input type=submit value="*" class="input"/>
<input type="hidden" name="wynik" value="*" ?>
<input type="hidden" name="zn" value="mnozenie"/>
</form>
</td>
<td>
<form>
<input type=submit value=" " class="input"/>
</form>
</td>
<td>
<form>
<input type=submit value=" " class="input"/>
</form>
</td>
<td>
<form>
<input type=submit value=" " class="input"/>
</form>
</td>
</tr>
</table>
</body>
</html>
</html>
oraz
main1.css
.tabela {
padding:0;
border:1px solid black;
}
tr {
padding: 3px;
}
td {
padding: 3px;
}
.input{
width:40px;
height:40px;
background-color: blue;
border:0;
color:white;
font-size:20px;
border-radius:5px;
}
input:hover {
background-color:purple;
}
.wyswietlacz{
height:40px;
padding:5px;
background-color: rgb(93, 93, 214);
color:white;
font-size:15px;
}