0

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>";
        }
    }
?>
2
  • Question is what is your expected output? (btw: nice profile picture :) First if statements is: If the value isn't equals: time, avgspd and kartno at 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" ... Commented Aug 18, 2015 at 17:51
  • 1
    @Rizier123 I want it to display keys starting with 'lap'. Did say it in the question! :p . Always live in the terminal. Who needs a GUI! Lol Why did the days of MS-DOS disappear? Lol. Suppose Arch Linux is better! :) Commented Aug 18, 2015 at 17:54

3 Answers 3

2

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>";
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Wow I got beat pretty bad.
Cool. Thanks for that if starts with lap! Thats what I wanted to do but wasn't sure if I could.
2

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")))

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.