tags:

views:

161

answers:

3

Hello I'm getting results in format

location1 2 4
location2 3 2
location3 0 0
location1 1 0

How can I trim results so that row returning 0 and 0 is not displayed ? thank you

.................

Here is Mysql query, I don't know how to trim it from MySQL so I thought using PHP ..

    SELECT hotelas.name, hotelas.address, hotelas.city, hotelas.country, hotelas.hotel_id 
AS hotelid, 
COUNT( DISTINCT apart.apartman_id ) AS number_of_free_ap, 
COUNT( DISTINCT room.apartman_id ) AS num_of_free_rooms
    FROM hotel AS hotelas
    LEFT JOIN apartman AS apart ON ( apart.apartman_hotel = hotelas.hotel_id
    AND apart.occupied =0
    AND apart.what =1 )
    LEFT JOIN apartman AS room ON ( room.apartman_hotel = hotelas.hotel_id
    AND room.occupied =0
    AND room.what =0 )
    GROUP BY hotelas.hotel_id

TABLE field what, 0 - for room, 1 - apartment

So I get a few columns among which the most important ones are count columns , free rooms and free apartments. So I have a test hotel which is full 0 rooms and 0 apartments and I want is removed from this list where all other hotels have at least one room or one apartment available.

+1  A: 
select * from table
where column1 !=0
or column2 !=0

That's how you can trim it in MySQL.

OTOH, if you have an array such as this ( you will usually get this after you query from a db)

array(array('location1', 2, 4), array('location2', 3, 2))

Then you need to loop over the array and do the manual filtering:

$newarrs=array();
foreach($arrs as $values)
{
  if($values[1]!=0 || $values[2]!=0)
    $newarrs[]=$values;
}
return $newarrs;
Ngu Soon Hui
I have a pretty complex LEFT join query I don't know how to modify it. Its working so I should edit the results from PHP
c0mrade
You could post the query here. We might be able to help.
gnud
+6  A: 

if you are querying you database to get this information and you can change the query, that would be the best place for it.

SELECT *
FROM myTable
WHERE
    firstColumn != 0
OR
    secondColumn != 0

If you are getting this in PHP from some other source (CSV or something), or you don't have control over the query, use array_filter():

// i'm assuming each line in your example is an array of values
$myArray = array_filter($myArray, 'noDoubleZeroes');

function noDoubleZeroes($line) {
    return $line[1] != 0 || $line[2] != 0;
}
nickf
Your SQL is wrong - should be OR not AND
Greg
No, his SQL seems correct to me. The asker wants to remove results where BOTH columns are 0, so AND is the correct choise.
gnud
actually, greg is right! ;)
nickf
your solution works when I change your function to function noDoubleZeroes($line) { return $line[number_of_free_ap] != 0 || $line[num_of_free_rooms] != 0;}But I'm now getting empty results from column name, address, city, country
c0mrade
Ah, of course. The SQL _excludes_, so it's an OR. Now I feel stupid.
gnud
@gnud - you and me both, buddy. @c0mrade, are you sure that your array's keys are what you think they are? try adding some echoes in the function to help debug: `print_r($line)` would help.
nickf
A: 

If i understood you correctly this should help

$columns = explode($resultLine, " ");
$whatYouWant =  $columns[0];
MoreThanChaos
You did not understand correctly. He wants to remove the rows with two 0's.
gnud