Reputation: 6547
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>";
}
}
?>
Upvotes: 0
Views: 68
Reputation: 327
Here you go:
foreach($_POST as $key=>$val)
{
if($key != "time" && $key != "avgspd" && $key != "kartno")
{
echo $key . "<br>";
}
}
Explaination: If you put the OR statement here, it will always print because you basically told your program to print it if it is different from "time" OR different from "avgspd". And "time" is different from "avgspd" so it prints.
Upvotes: 1
Reputation: 326
I believe you are looking for &&
. Or else I'm missing something.
foreach($_POST as $key=>$val)
{
if($key != "time" && $key != "avgspd" && $key != "kartno")
{
echo $key . "<br>";
}
}
You want all three conditions to be met in order to print.
Edit: Even better! Check if it starts with lap
. This is more appropriate for what you want.
foreach($_POST as $key=>$val)
{
if(substr($key, 0, 3) == "lap")
{
echo $key . "<br>";
}
}
Upvotes: 2
Reputation: 2698
This is always true:
if($key != "time" || $key != "avgspd" || $key != "kartno")
What you mean is:
if($key != "time" and $key != "avgspd" and $key != "kartno")
You can rewrite it to:
if(! in_array($key,array("time","avgspd","kartno")))
Upvotes: 2