Hello all,

I have a large database that I would like to query but I would like to exclude a large number of records. I am wondering if the query is the place to do this, since using multiple 'WHERE field1 !=' or 'WHERE field1 NOT LIKE '% would get a bit messy.

So, I started thinking arrays. I could run the query and get all records based on other criteria. I could then set up an array based on the content in the field names 'hname' and display only what I do want to display. Eventually, there would be about 40 unique values in 'hname' that I would like to include.

When I display the data, I could run through the array using a foreach loop and display only the rows with the value in the array for 'hname'.

$query = "SELECT * FROM documents";
$result = mysql_query($query) or
die (mysql_error());

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

$include = array("Jim", "Bob", "Sam", "Jerry");

$hname = $row['hname'];

foreach ($hname as $include) {
$staff = $include;
}

echo "<tr><td width='10%'>" . $staff . "</td>";

}

When I open this page, I get a long listing of:

Warning: Invalid argument supplied for foreach() in /disk2/www/othersites/counturls1.php on line 31

Then all the data follows, including data that I don't want.

I am somewhat new to arrays, so this is all an experiment. Your help is most appreciated.

    If you know what you wish to include in the result set, why don't you query for those records only?
    From your code snippet you only have 4 names to include so the query wouldn't be that hard to create.

      You could also look into NOT IN:

      SELECT whatever FROM table_name WHERE column_name NOT IN (val1, val2, val3, etc.)

      In regard to your foreach loop, you're getting an error because $hname isn't an array, $include is:

      foreach ($include as $hname)

        Write a Reply...