Re: [PHP] logic operands problem

2009-12-07 Thread Merlin Morgenstern



Devendra Jadhav wrote:

what do you think about this?
if( ! (page == 1  page == 2)){
  //here
}



Well a simple  (and) does not help.

I want to have all results that contain either page = 1 OR page = 3, AND 
in the same time I want to have the results that contain page=2 OR page= 3
, But the result should never contain page = 1 and page = 2 in the same 
time.


Any further idea?





On Mon, Dec 7, 2009 at 4:22 PM, Merlin Morgenstern 
merli...@fastmail.fm mailto:merli...@fastmail.fm wrote:


Hello everybody,

I am having trouble finding a logic for following problem:

Should be true if:
page = 1 OR page = 3, but it should also be true if page = 2 OR
page = 3

The result should never contain 1 AND 2 in the same time.

This obviously does not work:
(page = 1 OR page = 3) OR (page = 2 OR page = 3)

This also does not work:
(page = 1 OR page = 3 AND page != 2) OR (page = 2 OR page = 3 AND
page != 1)

Has somebody an idea how to solve this?

Thank you in advance for any help!

Merlin

-- 
PHP General Mailing List (http://www.php.net/)

To unsubscribe, visit: http://www.php.net/unsub.php




--
Devendra Jadhav
देवेंद्र जाधव


Re: [PHP] logic operands problem

2009-12-07 Thread Ashley Sheridan
On Mon, 2009-12-07 at 11:52 +0100, Merlin Morgenstern wrote:

 Hello everybody,
 
 I am having trouble finding a logic for following problem:
 
 Should be true if:
 page = 1 OR page = 3, but it should also be true if page = 2 OR page = 3
 
 The result should never contain 1 AND 2 in the same time.
 
 This obviously does not work:
 (page = 1 OR page = 3) OR (page = 2 OR page = 3)
 
 This also does not work:
 (page = 1 OR page = 3 AND page != 2) OR (page = 2 OR page = 3 AND page != 1)
 
 Has somebody an idea how to solve this?
 
 Thank you in advance for any help!
 
 Merlin
 


I thought this might work:

(page = 3) OR (page = 1 XOR 2)

But having given it more thought, I'm not so sure. I assume from your
example that this is MySQL code and not PHP. Having said that, how can a
field of one row have more than one value? Surely `page` is either 1, 2
or 3, not two of them at once. How do you want your results pulled?
Assuming you have rows containing all three values, how do you decide
which of the pair of results you want?

Thanks,
Ash
http://www.ashleysheridan.co.uk




Re: [PHP] logic operands problem

2009-12-07 Thread Merlin Morgenstern



Ashley Sheridan wrote:

On Mon, 2009-12-07 at 11:52 +0100, Merlin Morgenstern wrote:

Hello everybody,

I am having trouble finding a logic for following problem:

Should be true if:
page = 1 OR page = 3, but it should also be true if page = 2 OR page = 3

The result should never contain 1 AND 2 in the same time.

This obviously does not work:
(page = 1 OR page = 3) OR (page = 2 OR page = 3)

This also does not work:
(page = 1 OR page = 3 AND page != 2) OR (page = 2 OR page = 3 AND page != 1)

Has somebody an idea how to solve this?

Thank you in advance for any help!

Merlin




I thought this might work:

(page = 3) OR (page = 1 XOR 2)

But having given it more thought, I'm not so sure. I assume from your 
example that this is MySQL code and not PHP. Having said that, how can 
a field of one row have more than one value? Surely `page` is either 
1, 2 or 3, not two of them at once. How do you want your results 
pulled? Assuming you have rows containing all three values, how do you 
decide which of the pair of results you want?


Thanks,
Ash
http://www.ashleysheridan.co.uk





You have described the problem very well. This is exactly where I can 
not find a solution.


the page number translates to the following: 1= first page 2= following 
pages 3= all pages


This are the options a user has while booking a product on my site. Now 
if ther is a new
client that wants to book all pages, I need to query the table to find 
out if the spot is available.
The spot would be full if page 1 has more results then 3 , OR all 
following pages have more then 3 results. So to find out if all pages 
option would be available I need to query the db to retrieve all 
results, that are (page = 3) OR (page = 1 XOR 2)


Am I wrong?



Re: [PHP] logic operands problem

2009-12-07 Thread Ashley Sheridan
On Mon, 2009-12-07 at 12:49 +0100, Merlin Morgenstern wrote:

 
 Ashley Sheridan wrote:
  On Mon, 2009-12-07 at 11:52 +0100, Merlin Morgenstern wrote:
  Hello everybody,
 
  I am having trouble finding a logic for following problem:
 
  Should be true if:
  page = 1 OR page = 3, but it should also be true if page = 2 OR page = 3
 
  The result should never contain 1 AND 2 in the same time.
 
  This obviously does not work:
  (page = 1 OR page = 3) OR (page = 2 OR page = 3)
 
  This also does not work:
  (page = 1 OR page = 3 AND page != 2) OR (page = 2 OR page = 3 AND page != 
  1)
 
  Has somebody an idea how to solve this?
 
  Thank you in advance for any help!
 
  Merlin
 
  
 
  I thought this might work:
 
  (page = 3) OR (page = 1 XOR 2)
 
  But having given it more thought, I'm not so sure. I assume from your 
  example that this is MySQL code and not PHP. Having said that, how can 
  a field of one row have more than one value? Surely `page` is either 
  1, 2 or 3, not two of them at once. How do you want your results 
  pulled? Assuming you have rows containing all three values, how do you 
  decide which of the pair of results you want?
 
  Thanks,
  Ash
  http://www.ashleysheridan.co.uk
 
 
 
 
 You have described the problem very well. This is exactly where I can 
 not find a solution.
 
 the page number translates to the following: 1= first page 2= following 
 pages 3= all pages
 
 This are the options a user has while booking a product on my site. Now 
 if ther is a new
 client that wants to book all pages, I need to query the table to find 
 out if the spot is available.
 The spot would be full if page 1 has more results then 3 , OR all 
 following pages have more then 3 results. So to find out if all pages 
 option would be available I need to query the db to retrieve all 
 results, that are (page = 3) OR (page = 1 XOR 2)
 
 Am I wrong?
 


I'm pretty confused by your logic, but I think the only way you can
achieve what you want is with multiple queries and counts.

Thanks,
Ash
http://www.ashleysheridan.co.uk




Re: [PHP] logic operands problem

2009-12-07 Thread Lester Caine

Merlin Morgenstern wrote:
You have described the problem very well. This is exactly where I can 
not find a solution.


the page number translates to the following: 1= first page 2= following 
pages 3= all pages


This are the options a user has while booking a product on my site. Now 
if ther is a new
client that wants to book all pages, I need to query the table to find 
out if the spot is available.
The spot would be full if page 1 has more results then 3 , OR all 
following pages have more then 3 results. So to find out if all pages 
option would be available I need to query the db to retrieve all 
results, that are (page = 3) OR (page = 1 XOR 2)


Am I wrong?


Yes I think you are! And XOR may be wrong here. XOR is a binary operator, so 
think of the number as 0b0011 for 3 0b0010 for 2 and 0b0001 for 1 ...

1 XOR 2 will give a result of 0b0011 - so = 3

What you are trying to do just seems wrong in general.
'if page 1 has more results then 3' requires you count the number of page 1 
records and compare with the number of page 3 records. And the same with page 2 
results.


I don't think you are giving enough detail to know exactly what you are trying 
to achieve, but what you are describing so far does not make sense 'logically'. 
If you are trying to book a 'set' of pages but can't if one of that set is 
already booked, then I don't think you can do this with a single query. You need 
to check each 'page' individually. If this was room booking, then one would have 
to check there are no other bookings for a day in the period for that particular 
room. Or putting it another way, there are no days in the period when all rooms 
are booked, but in this case you still need to know that a particular room is 
available for the whole period.


--
Lester Caine - G8HFL
-
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk//
Firebird - http://www.firebirdsql.org/index.php

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] logic operands problem

2009-12-07 Thread Kim Madsen

Hey Merlin

Merlin Morgenstern wrote on 2009-12-07 11:52:

Hello everybody,

I am having trouble finding a logic for following problem:

Should be true if:
page = 1 OR page = 3, but it should also be true if page = 2 OR page = 3

The result should never contain 1 AND 2 in the same time.

This obviously does not work:
(page = 1 OR page = 3) OR (page = 2 OR page = 3)

This also does not work:
(page = 1 OR page = 3 AND page != 2) OR (page = 2 OR page = 3 AND page 
!= 1)


Has somebody an idea how to solve this?


I've read the entire thread and can see that this is a MySQL query you 
want to make (I was about to tell you about the == comparison and the $ 
in a variable in PHP).


What you want is all results containing 1,2 or 3, so make a WHERE page 
IN(1,2,3) and use PHP logic to figure out if a free slot is available 
or not. Or rewrite your booking routine (use data and time fields 
instead, maybe by creating a bunch of free slots and then a booked field 
with a default of 0, changed to 1 when booked)


--
Kind regards
Kim Emax - masterminds.dk

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] logic operands problem

2009-12-07 Thread tedd

At 11:52 AM +0100 12/7/09, Merlin Morgenstern wrote:

Hello everybody,

I am having trouble finding a logic for following problem:

Should be true if:
page = 1 OR page = 3, but it should also be true if page = 2 OR page = 3

The result should never contain 1 AND 2 in the same time.

This obviously does not work:
(page = 1 OR page = 3) OR (page = 2 OR page = 3)

This also does not work:
(page = 1 OR page = 3 AND page != 2) OR (page = 2 OR page = 3 AND page != 1)

Has somebody an idea how to solve this?

Thank you in advance for any help!

Merlin


Merlin:

The variable page cannot hold two values at the same time and thus 
your statement The result should never contain 1 AND 2 in the same 
time is nonsense.


Now if you are working two variables, namely $a and $b and you want 
an algorithm to solve your problem, it's simple.


if ($a + $b  3)
   {
   $result = true;
   }
else
{
   $result = false;
   }

Cheers,

tedd

--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] logic operands problem

2009-12-07 Thread Merlin Morgenstern

Hi everybody,

thank you for all the help and thoughts. I have solved it, but I guess 
it is not an elegant solution. What I do now, is simply check again for 
the second case. There are 2 cases. Either first page OR all pages, 
second case: following pages OR all pages.


My booking checking looks now as following:

  
   


   # on which page will the tl be placed?
   if ($data[page] == 3){ // all pages
   $where_page = 'AND (page = 1 OR page = 3)'; // unfortunatelly we 
have to test later on page = 2 OR page = 3. No solution inside one 
query. We tried also (page=1 XOR page=2) OR page = 3

   $where_page_2 = 'AND (page = 2 OR page = 3)';
   }
   else{ // page one or all following pages
   $where_page = 'AND page = '.$data[page];
   }
   

   


   # find out first possible booking period
   do{
   // get all toplistings that are at least with one day inside the 
desired booking period

   $stmt= 
   SELECT
   *
   FROM
   $DB.$T54
   WHERE
   cat_id = '$data[cat_id]'
   AND cat_type = '$data[cat_type]'
   $where_page
   AND
   (
   (start_date = '$new_start' AND expires = 
'$new_start')

   OR (start_date = '$new_end' AND expires = '$new_end')
   OR (start_date = '$new_start' AND start_date= 
'$new_end')
   )   
   ;

   #echo $stmt.$br;
   $result = execute_stmt($stmt, $link);
   while ($row = db_get_row($result)){   
   $booked[start][] = $row-start_date;

   $booked[end][]= $row-expires;
   }
   $possible_bookings = count($booked[start]);
   // would there be more bookings then possible?
   if ($possible_bookings = $places){ // not enough space. Try 
nest day

   $shift = true; // shift period for one day
   $reservation = 1;
   $new_start =  date(Ymd,strtotime($new_start. + 1 day));
   $new_end = date(Ymd,strtotime($new_end. + 1 day));
   }
   else{ // enough space
   unset($shift);
   }
   unset($booked);   
   } while ($shift); // shift as long as we find free space
   




   

   # if client wants to book all pages, we have to try also the second 
constellation

   # we could not find a way to do this in one sql query
   # find out if booking period has to be shifted even further due to 
all pages booking

   if ($page == 3){
   do{
   // get all toplistings that are at least with one day inside 
the desired booking period

   $stmt= 
   SELECT
   *
   FROM
   $DB.$T54
   WHERE
   cat_id = '$data[cat_id]'
   AND cat_type = '$data[cat_type]'
   $where_page_2
   AND
   (
   (start_date = '$new_start' AND expires = 
'$new_start')
   OR (start_date = '$new_end' AND expires = 
'$new_end')
   OR (start_date = '$new_start' AND start_date= 
'$new_end')
   )   
   ;

   //echo $stmt.$br;
   $result = execute_stmt($stmt, $link);
   while ($row = db_get_row($result)){   
   $booked[start][] = $row-start_date;

   $booked[end][]= $row-expires;
   }
   $possible_bookings = count($booked[start]);
   // would there be more bookings then possible?
   if ($possible_bookings = $places){ // not enough space. Try 
nest day

   $shift = true; // shift period for one day
   $reservation = 1;
   $new_start =  date(Ymd,strtotime($new_start. + 1 
day));

   $new_end = date(Ymd,strtotime($new_end. + 1 day));
   }
   else{ // enough space
   unset($shift);
   }
   unset($booked);   
   } while ($shift); // shift as long as we find free space

   }
   

  

This is rather a dirty solution. Maybe someone has an idea on how to do 
it more elegant? I believe there should be one simple line that is 
different instead of checking it all over again for a second time.



Any ideas?


Kim Madsen wrote:

Hey Merlin

Merlin Morgenstern wrote on 2009-12-07 11:52:

Hello everybody,

I am