Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]Jak stosować konkatenację?
Marco1357
post 1.02.2023, 17:37:38
Post #1





Grupa: Zarejestrowani
Postów: 18
Pomógł: 0
Dołączył: 17.01.2023

Ostrzeżenie: (0%)
-----


Oto kod:
  1.  
  2. if ($_GET['wynik']==='='){
  3.  
  4. $tablica=array('+' => "dodawanie", '-' => "odejmowanie", '*' => "mnozenie", '/' => "dzielenie" );
  5.  
  6. foreach($tablica as $klucz => $wartosc){
  7.  
  8. $wartosc=explode($klucz,$_SESSION['liczba']);
  9. if(count($wartosc)>1) {
  10. $_SESSION[$tablica[$klucz]] = $wartosc[0] .$klucz. $wartosc[1];
  11. $_SESSION['liczba'] = $_SESSION[$tablica[$klucz]];
  12.  
  13. }
  14. }
  15. }
  16.  



Wszystko działa sprawnie do momentu dodawania dwóch liczb i przypisania wyniku do sesji . Przedostatnia linijka. Chodzi o to że elementy nie są dodawane do siebie i nie jest tworzony wynik, tylko są obie wartości wyświetlane wraz z operatorem. Pomimo zastosowania konkatenacji wraz ze zmienną $klucz.
Go to the top of the page
+Quote Post
Salvation
post 1.02.2023, 17:46:57
Post #2





Grupa: Zarejestrowani
Postów: 338
Pomógł: 70
Dołączył: 15.07.2014

Ostrzeżenie: (0%)
-----


Konkatenacja, to łącznie stringów w jeden. W tym co napisałeś, nie tylko dodawanie nie zadziała, a cała reszta działań.

Robiąc takie coś:
  1. $wartosc[0] .$klucz. $wartosc[1];

$wartosc[0] i $wartosc[1] nawet jak są intami, to niejawnie zostaną przeparsowane na string.

Więc teraz albo skorzystasz z eval() - czego nie polecam i wrzucisz do niego ten otrzymany string, albo zrobisz ten kalkulator do porządku i poprawisz działanie smile.gif
Go to the top of the page
+Quote Post
Marco1357
post 2.02.2023, 14:46:27
Post #3





Grupa: Zarejestrowani
Postów: 18
Pomógł: 0
Dołączył: 17.01.2023

Ostrzeżenie: (0%)
-----


Dzieki za podpowiedź oto poprawiony, przerobiony działający kod:
  1.  
  2. if ($_GET['wynik']==='='){
  3.  
  4. $tablica=array('+' => "dodawanie", '-' => "odejmowanie", '*' => "mnozenie", '/' => "dzielenie" );
  5.  
  6. foreach($tablica as $klucz => $wartosc){
  7.  
  8. $wartosc=explode($klucz,$_SESSION['liczba']);
  9. if(count($wartosc)>1) {
  10.  
  11. if($klucz=='+'){
  12. $_SESSION[$tablica[$klucz]] = $wartosc[0] + $wartosc[1];
  13. }
  14. elseif($klucz=='-'){
  15. $_SESSION[$tablica[$klucz]] = $wartosc[0] - $wartosc[1];
  16. }
  17. elseif($klucz=='*'){
  18. $_SESSION[$tablica[$klucz]] = $wartosc[0] * $wartosc[1];
  19. }
  20. elseif($klucz=='/'){
  21. $_SESSION[$tablica[$klucz]] = $wartosc[0] / $wartosc[1];
  22. }
  23.  
  24. $_SESSION['liczba'] = $_SESSION[$tablica[$klucz]];
  25. }
  26. }
  27. }
  28.  

Go to the top of the page
+Quote Post
Salvation
post 3.02.2023, 00:15:28
Post #4





Grupa: Zarejestrowani
Postów: 338
Pomógł: 70
Dołączył: 15.07.2014

Ostrzeżenie: (0%)
-----


Ten kod na pewno działa tak jak chcesz?
Go to the top of the page
+Quote Post
Marco1357
post 3.02.2023, 14:50:06
Post #5





Grupa: Zarejestrowani
Postów: 18
Pomógł: 0
Dołączył: 17.01.2023

Ostrzeżenie: (0%)
-----


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
  1.  
  2. <?php
  3.  
  4.  
  5.  
  6. if (isset($_GET['wynik']) ) {
  7. // if (is_numeric($_GET['wynik'])) {
  8.  
  9. if (!isset($_SESSION['liczba']))
  10. {
  11. $_SESSION['liczba'] = '';
  12. }
  13.  
  14. $_SESSION['liczba'] .= $_GET['wynik'];
  15.  
  16.  
  17. // }
  18. if ($_GET['wynik']==='C'){
  19. }
  20.  
  21.  
  22. if ($_GET['wynik']==='='){
  23.  
  24. $tablica=array('+' => "dodawanie", '-' => "odejmowanie", '*' => "mnozenie", '/' => "dzielenie" );
  25.  
  26. foreach($tablica as $klucz => $wartosc){
  27.  
  28. $wartosc=explode($klucz,$_SESSION['liczba']);
  29. if(count($wartosc)>1) {
  30.  
  31. if($klucz=='+'){
  32. $_SESSION[$tablica[$klucz]] = $wartosc[0] + $wartosc[1];
  33. }
  34. elseif($klucz=='-'){
  35. $_SESSION[$tablica[$klucz]] = $wartosc[0] - $wartosc[1];
  36. }
  37. elseif($klucz=='*'){
  38. $_SESSION[$tablica[$klucz]] = $wartosc[0] * $wartosc[1];
  39. }
  40. elseif($klucz=='/'){
  41. $_SESSION[$tablica[$klucz]] = $wartosc[0] / $wartosc[1];
  42. }
  43.  
  44. $_SESSION['liczba'] = $_SESSION[$tablica[$klucz]];
  45. }
  46. }
  47. }
  48.  
  49. include 'kalkulator.php';
  50. }
  51. ?>
  52.  



kalkulator.php
  1.  
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset='utf-8'>
  6. <meta http-equiv='X-UA-Compatible' content='IE=edge'>
  7. <title>KALKULATOR</title>
  8. <meta name='viewport' content='width=device-width, initial-scale=1'>
  9. <link rel='stylesheet' type='text/css' media='screen' href='main1.css'>
  10. <script src='main.js'></script>
  11. </head>
  12. <body>
  13.  
  14.  
  15. <table class="tabela">
  16. <tr>
  17. <td colspan="4" class="wyswietlacz">
  18. Działanie: <?php
  19.  
  20. if (isset($_SESSION['liczba'])){
  21. echo $_SESSION['liczba'];
  22. }
  23.  
  24. ?>
  25. <br>
  26. Wynik:
  27. <?php
  28. if (isset($_SESSION['dodawanie'])){
  29. echo $_SESSION['dodawanie'];
  30. unset($_SESSION['dodawanie']);
  31.  
  32. }
  33.  
  34. if (isset($_SESSION['odejmowanie'])){
  35. echo $_SESSION['odejmowanie'];
  36. unset($_SESSION['odejmowanie']);
  37.  
  38. }
  39.  
  40. if (isset($_SESSION['mnozenie'])){
  41. echo $_SESSION['mnozenie'];
  42. unset($_SESSION['mnozenie']);
  43.  
  44. }
  45.  
  46. if (isset($_SESSION['dzielenie'])){
  47. echo $_SESSION['dzielenie'];
  48. unset($_SESSION['dzielenie']);
  49.  
  50. }
  51. ?>
  52. </td>
  53. </tr>
  54.  
  55. <tr>
  56.  
  57. <td>
  58. <form action="kalk.php" method="get">
  59. <input type=submit value="1" class="input"/>
  60. <input type="hidden" name="wynik" value= "1" />
  61.  
  62. </form>
  63. </td>
  64.  
  65. <td>
  66. <form action="kalk.php" method="get">
  67. <input type=submit value="2" class="input"/>
  68. <input type="hidden" name="wynik" value="2" />
  69. </form>
  70. </td>
  71.  
  72. <td>
  73. <form action="kalk.php" method="get">
  74. <input type=submit value="3" class="input"/>
  75. <input type="hidden" name="wynik" value="3" ?>
  76. </form>
  77. </td>
  78.  
  79. <td>
  80. <form action="kalk.php" method="get">
  81. <input type=submit value="C" class="input"/>
  82. <input type="hidden" name="wynik" value="C" ?>
  83. <input type="hidden" name="zn" value="kasowanie"/>
  84. </form>
  85. </td>
  86.  
  87. </tr>
  88.  
  89. <tr>
  90.  
  91. <td>
  92. <form action="kalk.php" method="get">
  93. <input type=submit value="4" class="input"/>
  94. <input type="hidden" name="wynik" value="4" ?>
  95. </form>
  96. </td>
  97.  
  98. <td>
  99. <form action="kalk.php" method="get">
  100. <input type=submit value="5" class="input"/>
  101. <input type="hidden" name="wynik" value="5" ?>
  102. </form>
  103. </td>
  104.  
  105. <td>
  106. <form action="kalk.php" method="get">
  107. <input type=submit value="6" class="input"/>
  108. <input type="hidden" name="wynik" value="6" ?>
  109. </form>
  110. </td>
  111.  
  112. <td>
  113. <form action="kalk.php" method="get">
  114. <input type=submit value="-" class="input"/>
  115. <input type="hidden" name="wynik" value="-" ?>
  116. <input type="hidden" name="zn" value="minus"/>
  117. </form>
  118. </td>
  119.  
  120. </tr>
  121. <tr>
  122. <td>
  123. <form action="kalk.php" method="get">
  124. <input type=submit value="7" class="input"/>
  125. <input type="hidden" name="wynik" value="7" ?>
  126. </form>
  127. </td>
  128.  
  129. <td>
  130. <form action="kalk.php" method="get">
  131. <input type=submit value="8" class="input"/>
  132. <input type="hidden" name="wynik" value="8" ?>
  133. </form>
  134. </td>
  135.  
  136.  
  137. <td>
  138. <form action="kalk.php" method="get">
  139. <input type=submit value="9" class="input"/>
  140. <input type="hidden" name="wynik" value="9" ?>
  141. </form>
  142. </td>
  143. <td>
  144.  
  145. <form action="kalk.php" method="get">
  146. <input type=submit value="+" class="input"/>
  147. <input type="hidden" name="wynik" value="+" ?>
  148. <input type="hidden" name="zn" value="plus"/>
  149. </form>
  150.  
  151. </td>
  152. </tr>
  153.  
  154. <tr>
  155. <td>
  156. <form action="kalk.php" method="get">
  157. <input type=submit value="." class="input"/>
  158. <input type="hidden" name="wynik" value="." ?>
  159. </form>
  160.  
  161.  
  162. </td>
  163.  
  164. <td>
  165. <form action="kalk.php" method="get">
  166. <input type=submit value="/" class="input"/>
  167. <input type="hidden" name="wynik" value="/" ?>
  168. <input type="hidden" name="zn" value="dzielenie"/>
  169. </form>
  170.  
  171.  
  172.  
  173. </td>
  174.  
  175. <td>
  176. <form action="kalk.php" method="get">
  177. <input type=submit value="0" class="input"/>
  178. <input type="hidden" name="wynik" value="0" ?>
  179. </form>
  180. </td>
  181.  
  182. <td>
  183. <form action="kalk.php" method="get">
  184. <input type=submit value="=" class="input"/>
  185. <input type="hidden" name="wynik" value="=" ?>
  186. <input type="hidden" name="zn" value="rowna"/>
  187. </form>
  188. </td>
  189.  
  190.  
  191. </tr>
  192. <tr>
  193. <td>
  194. <form action="kalk.php" method="get">
  195. <input type=submit value="*" class="input"/>
  196. <input type="hidden" name="wynik" value="*" ?>
  197. <input type="hidden" name="zn" value="mnozenie"/>
  198. </form>
  199. </td>
  200.  
  201. <td>
  202. <form>
  203. <input type=submit value=" " class="input"/>
  204.  
  205. </form>
  206. </td>
  207.  
  208. <td>
  209. <form>
  210. <input type=submit value=" " class="input"/>
  211.  
  212. </form>
  213. </td>
  214.  
  215. <td>
  216. <form>
  217. <input type=submit value=" " class="input"/>
  218.  
  219. </form>
  220. </td>
  221.  
  222.  
  223. </tr>
  224.  
  225. </table>
  226.  
  227. </body>
  228. </html>
  229. </html>
  230.  



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;


}
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 28.03.2024 - 14:01