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

Pie
  • 856
  • 4
  • 12
  • 33

1 Answers1

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%'
Sean
  • 12,443
  • 3
  • 29
  • 47
  • I think `LIKE` may be better since there are "String" with different numbers following – Funk Forty Niner May 28 '15 at 03:53
  • @Fred-ii- `LIKE` would be in the mysql query, not the php loop though, correct? – Sean May 28 '15 at 03:54
  • 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. – Funk Forty Niner May 28 '15 at 03:56
  • 1
    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 ;-) – Funk Forty Niner May 28 '15 at 04:00