Forum Moderators: coopster

Message Too Old, No Replies

Inverse PHP queries

need some help

         

knighty

10:46 am on Dec 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have an array which contains ID's

ie. id1,id2,id3 etc

I am using a for each statement to match these ids from a table

foreach ($array as $id)
{
select * from table where ID='$id'
print row from table
}

Anyway this all works fine BUT I want to then return all the rows that do NOT have that ID in them - not sure how to go about this.

I was thinking I could do select * from table where ID!='$id' but how?

Xuefer

11:05 am on Dec 10, 2002 (gmt 0)

10+ Year Member



NOT ID='$id'

it's sql query, not php query

knighty

11:49 am on Dec 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



At the moment the main problem is the foreach statement only brings back a few results because if there are only 3 ids it will only spit out 3 rows

Obviously I need some way to spit out everything that IS NOT in the array, maybe if I select all from the table and then do a while id exits in row print row, if id isn't in row print id does not exist in row?

Xuefer

1:12 pm on Dec 10, 2002 (gmt 0)

10+ Year Member



sorry, how many id will u check?

select * from table where ID NOT IN (1,2,3,4,5) <-- use foreach or implode to build this list from $array

aspr1n

8:04 pm on Dec 10, 2002 (gmt 0)

10+ Year Member



I would:

$query = "SELECT * FROM table;

$result = mysql_query( $query );

do {

for( $i = 0; $i < array_size; $i++ ) {
if ( $row["id"]!= array[$i]["id"] )
echo "some stuff";
}
reset( array );

} while ( $row = mysql_fetch_assoc( $result ) );

Basically do one query and iterate through the array looking for matches.

Extra thought:

Because this would be quite aggressive on large data sets, if $id was numeric, I think I'd be tempted to add a little more intelligence to the inner loop:

Sort the array["id"] by $id.

Then the for loop could:

for( $i = 0; $row["id"] > array[$i]["id"] && $i < array_size; $i++ ) {
if ( $row["id"]!= array[$i]["id"] )
echo "some stuff";
}

asp

knighty

9:06 am on Dec 11, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



aspr1n,

Thanks in the end I did something fairly similar...

select everything from table then

$levels = explode(",", $userlevel);
$z=0;
while ( $z<count($levels)) {
if ($levels[$z]=="$table_userlev") {
"Do some stuff"
$test=1;
}
++$z
if ($test!=1){
"do some other stuff"
}
else { $test=2; }

appreciate any comments flaws, better ways to do this?

All i want to do is output what the user can access and what they can't