I am trying to loop through my $_POST variables and display data that has key = lap* only. This is the code I have so far.
foreach($_POST as $key=>$val)
{
if($key != "time" || $key != "avgspd" || $key != "kartno")
{
echo $key . "<br>";
}
}
The result it gives me is this
time
lap1
lap2
lap3
lap4
lap5
lap6
lap7
lap8
lap9
lap10
lap11
lap12
lap13
lap14
lap15
lap16
lap17
avgspd
kartno
As you can see, it still displays the keys avgspd,kartno and time. The strange and confusing this is if I change my code to this:
foreach($_POST as $key=>$val)
{
if($key == "time" || $key == "avgspd" || $key == "kartno")
{
echo $key . "<br>";
}
}
I get this as the result:
time
avgspd
kartno
This doesn't make any sense. If I check if the key is not equal to time,avgspd or kartno it displays all keys, yet when I say for it to only display keys "time","avgspd" or "kartno" it works as expected! Whats going on here? What do I need to do to fix it. Is some earlier code causing the error? If so here is my code.
Index.php
<?php
if(!$_POST)
{
include("functions.php");
echo "<link rel='stylesheet' type='text/css' href='style.css'>";
echo "<form action='' method='POST'>";
echo "<table><tr>";
table_head("Time","lap 1","lap 2","lap 3","lap 4","lap 5","lap 6","lap 7","lap 8","lap 9","lap 10","lap 11","lap 12","lap 13","lap 14","lap 15","lap 16","lap 17","Avg Spd");
echo "</tr><tr>";
display_fields();
echo "</tr></table>";
echo "Kart Number: <input type='text' size='2' name='kartno'/><br>";
echo "<input type='submit'/>";
echo "</form>";
} else {
foreach($_POST as $key=>$val)
{
if($key == "time" || $key == "avgspd" || $key == "kartno")
{
echo $key . "<br>";
}
}
}
?>
functions.php
<?php
function table_head()
{
if ( func_num_args() > 0 )
{
$args = func_get_args();
foreach($args as $value)
{
echo "<th>" . $value . "</th>";
}
}
}
function display_fields()
{
$i = 0;
$a = 19;
$name="hi";
while($i++ < $a)
{
array_push($numbers,$i);
if($i==1)
{
$name = "time";
} else if($i > 1 && $i < 19){
$name = "lap" . strval($i-1);
} else {
$name = "avgspd";
}
echo "<td><input type='text' size='8' name='" . $name . "'/></td>";
}
}
?>
time, avgspd and kartnoat the same time it will enter it. (So a value which is equals to 3 different values is impossible as you might see) Second if statement: If it is equals to one of the 3 values enter the if statement. What I guess you probably want to do is say: If it isn't one of the 3 values, which is:$key != "val1" && $key != "val2" ...