Files
serwerowe_mendela/Lekcja_01/petle.php
T

224 lines
6.1 KiB
PHP

<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pętle</title>
<style>
table, td {
border: 1px solid black;
}
.noborder {
border: none;
}
.noborder td {
border: none;
}
th {
background-color: gray;
}
</style>
</head>
<body>
<h1>Pętle</h1>
<h2>1</h2>
<form action="./petle.php" method="post">
<p>Ile cyfr wypisać</p>
<input type="number" name="ile1">
<input type="submit" value="ok">
</form>
<?php
$i = 1;
while($i <= @$_POST['ile1']) {
echo $i . " ";
$i++;
}
?>
<h2>2</h2>
<form action="./petle.php" method="post">
<p>Ile cyfr wypisać</p>
<input type="number" name="ile2">
<input type="submit" value="ok">
</form>
<?php
if(isset($_POST['ile2'])) {
$i = 1;
do {
echo $i . "<br>";
} while (++$i <= $_POST['ile2']);
}
?>
<h2>3</h2>
<form action="./petle.php" method="post">
<p>Schodzimy do zera. Od ilu?</p>
<input type="number" name="ile3">
<input type="submit" value="ok">
</form>
<?php
for($i = @$_POST['ile3']; $i > 0; $i--) {
echo $i . " ";
}
?>
<h2>4</h2>
<form action="./petle.php" method="post">
<label for="start">start:</label>
<input type="number" name="start" id="start" size=1>
<label for="krok">krok:</label>
<input type="number" name="krok" id="krok" size=1>
<label for="koniec">koniec:</label>
<input type="number" name="koniec" id="koniec" size=1>
<input type="submit" value="ok">
</form>
<?php
if(isset($_POST['start']) && isset($_POST['krok']) && isset($_POST['koniec'])) {
if(intval($_POST['krok']) != 0) {
if ((($_POST['start'] <= $_POST['koniec']) && ($_POST['krok'] > 0)) || (($_POST['start'] >= $_POST['koniec']) && ($_POST['krok'] < 0))) {
for($i = intval($_POST['start']); $i <= intval($_POST['koniec']); $i += intval($_POST['krok'])) {
echo $i . " ";
}
} else {
echo "Nie wykonam nieskończonej pętli!";
}
} else {
echo "Nie wykonam nieskończonej pętli!";
}
}
?>
<h2>5</h2>
<form action="./wynik.php" method="post">
<p>Ile masz lat?</p>
<select name="wiek">
<?php
for($i = 1; $i < 134; $i++) {
echo "<option>$i</option>";
}
?>
</select>
<input type="submit" value="ok">
</form>
<h2>6</h2>
<form action="./petle.php" method="get">
<p>Ile cyfr:</p>
<input type="number" name="ile">
<p>Kierunek:</p>
<select name="kierunek">
<option>Poziom</option>
<option>Pion</option>
</select>
<p>Krok:</p>
<input type="number" name="krok">
<input type="submit" value="ok"><br><br>
</form>
<table>
<?php
for($i = 0; $i < @$_GET['ile']; $i++) {
if(@$_GET['kierunek'] == 'Pion') {
echo "<tr>";
}
echo '<td>' . $i * @$_GET['krok'] + 1 . '</td>';
if(@$_GET['kierunek'] == 'Pion') {
echo "</tr>";
}
}
?>
</table>
<h2>7</h2>
<form action="./petle.php" method="post">
Wierszy: <input type="number" name="wiersze">
Kolumn: <input type="number" name="kolumny">
<input type="submit" value="ok">
</form>
<table>
<?php
for($i = 0; $i < @$_POST['wiersze']; $i++) {
echo "<tr>";
for($j = 0; $j < @$_POST['kolumny']; $j++) {
echo '<td>' . ($i * @$_POST['kolumny']) + $j + 1 . '</td>';
}
echo "</tr>";
}
?>
</table>
<h2>8</h2>
<form action="./petle.php" method="post">
SZACHOWNICA:
Wierszy: <input type="number" name="szachownica_wiersze">
Kolumn: <input type="number" name="szachownica_kolumny">
<input type="submit" value="ok">
</form>
<table>
<?php
for($i = 0; $i < @$_POST['szachownica_wiersze']; $i++) {
$black = false;
if($i % 2 != 0) {
$black = true;
}
echo '<tr>';
for($j = 0; $j < @$_POST['szachownica_kolumny']; $j++) {
echo '<td style="width: 16px; height: 16px; ';
if($black) {
echo 'background-color: black;';
}
echo '"></td>';
$black = !$black;
}
echo '</tr>';
}
?>
</table>
<h2>9</h2>
<form action="./petle.php" method="get">
Rozmiar: <input type="number" name="tsize">
<input type="submit" value="ok">
</form>
<table>
<?php
for($i = 0; $i < @$_GET['tsize']; $i++) {
echo '<tr>';
for($j = 0; $j < @$_GET['tsize']; $j++) {
echo '<td>' . ($i + 1) * ($j + 1) . '</td>';
}
echo '</tr>';
}
?>
</table>
<h2>10</h2>
<form action="./petle.php" method="get">
Rozmiar: <input type="number" name="tsize2">
<input type="submit" value="ok">
</form>
<table class="noborder">
<?php
echo '<tr><td></td>';
for($i = 0; $i < @$_GET['tsize2']; $i++) {
echo '<th>' . $i+1 . '</th>';
}
echo '</tr>';
for($i = 0; $i < @$_GET['tsize2']; $i++) {
echo '<tr>';
echo '<th>' . $i+1 . '</th>';
for($j = 0; $j < @$_GET['tsize2']; $j++) {
echo '<td>' . ($i+1) * ($j+1) . '</td>';
}
echo '</tr>';
}
?>
</table>
</body>
</html>