I’m afraid I cannot fix this out-of-index-problem…
Could anyone provide me with a hint that could help me with that matter?
[ In the form there are two fields
- in the first field there are bus stops - first, second,…, eight.
- in the second field the user must enter a number (“range”). How many nearby bus stops would she or he like to see before and after the selected bus-stop. ]
The “key”-code starts from the line 17-th.
[php]
Peatused
<?php
$peatused=array("esimene", "teine", "kolmas", "neljas", "viies", "kuues", "seitsmes", "kaheksas");
if (!empty ($_POST["peatus"]) && !empty($_POST["range"]) ) {
$key = array_search($_POST["peatus"], $peatused);
for ($i=$key-($_POST["range"]); $i<=$key+($_POST["range"]); $i++) {
echo $peatused[$i]."
";
}
} else {
//do nothing
}
?>
Vali peatus ja mitu peatust selle ümber kuvada
<?php
echo "
";
if(!empty($_POST['peatus'])){
echo "Vali peatus";
for( $i = 0; $i < count($peatused); $i++ ) {
if($i == array_search($_POST['peatus'], $peatused)) {
$selected = "selected";
}else{
$selected = "";
}
echo "$peatused[$i]";
}
} else {
echo "<option value=\"\" selected>Vali peatus</option>";
for ($i = 0; $i < count($peatused); $i++) echo "<option value=\"$peatused[$i]\">$peatused[$i]</option>";
}
echo "</select>";
?>
</select>
<br/>
<input type="number" name="range" <?php if(!empty($_POST["range"]) && is_numeric($_POST["range"]))
echo "value=\"".htmlspecialchars($_POST["range"])."\" "; ?> /><br/>
<input type="submit" value="kuva"/>
</form>
<?php
if(!empty($_POST)) { // vorm esitati
$errors=array();
if(!empty($_POST["peatus"])) {
//do sth
} else {
$errors[]="Palun vali peatus!";
}
if(!empty($_POST["range"])) {
//do sth
} else {
$errors[]="Palun vali peatuste arv!";
}
}
?>
<?php if (!empty($errors)): ?>
<?php foreach($errors as $e): ?>
<p style="color:red"> <?php echo $e; ?> </p>
<?php endforeach?>
<?php endif;?>
</body>
[/php]