1

I have this block of PHP code which is pulling its information from a database.

All I want to do is filter/hide the rows that has "Player" like "string".

    <?php
    while ($row = mysql_fetch_assoc($result))
    {
        echo "<tr>";

        echo "<td>";
        echo $row["player"];
        echo "</td>";

        echo "<td>";
        echo $row["by"];
        echo "</td>";

        echo "</tr>";
    }
    ?>

For example I would have a table below:

BEFORE

And I want it to look like the table below:

AFTER

0

1 Answer 1

2

using strpos() you can check if (strpos($row["player"], 'String') === false) and only echo if true

<?php
while ($row = mysql_fetch_assoc($result))
{

  if (strpos($row["player"], 'String') === false){

    echo "<tr>";

    echo "<td>";
    echo $row["player"];
    echo "</td>";

    echo "<td>";
    echo $row["by"];
    echo "</td>";

    echo "</tr>";

  }
}
?>

Per @Fred-ii's comment-
If you have the possibility of string vs String, you could use stripos() instead of strpos()

if (stripos($row["player"], 'string') === false)

Edit
Per @Fred-ii's first comment, you could also filter them out in your query, so you don't have to 'hide' them in the php code.

SELECT ... FROM ... WHERE player NOT LIKE 'String%'
Sign up to request clarification or add additional context in comments.

4 Comments

I think LIKE may be better since there are "String" with different numbers following
@Fred-ii- LIKE would be in the mysql query, not the php loop though, correct?
indeed, yeah ok. I see what you did using strpos but since the columns have camel case, it'd probably best to use stripos, least I think it'd be best just in case it changes later on to "string" in lowercase. Just an insight.
That way the OP won't need to worry, should "string" or "STRING" etc. happen to be entered somewhere and if it's coming from user-input or not. Again, just an insight. It won't hurt anything to use it ;-)

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.