Jump to content

show if date is before and total is less than


petattlebaum

Recommended Posts

Hello,

 

I have a form for the purpose of people signing up for workshops.

Of the elements, a few are radio buttons for workshops on different dates.

I would like these dates to remain visible if:

  • the date is before the actual workshop date
  • the total number of people signed up is less than 25

Where I have had trouble is if only one of the above is the case.

The code below works. But if I change the && to an OR or XOR, it does not work.

Is it possible to combine an if statement with based on whether a certain has passed OR the max person limit has been reached?

 

<?php
$cntAll = "100";//max person limit

$date = "2013-05-01";
$unix_date = strtotime ($date);
if ($unix_date > time() && $cntAll < 2) {
echo "form fields...";
}
?>

 

Thanks!

 

Peter T

Link to comment
Share on other sites

Did you try || - the PHP or.

 

But it sounds like you want &&. You ONLY want it to show when both of those conditions are true. Otherwise people can sign up if it's after the date as long as it's not full, or people can sign up when it's full as long as it's before the date.

 

You should also make your $countAll variable be a NUMBER. Not "100" but 100.

 

As it is your script will always evaluate to false because 100 > 2. 

Edited by Jessica
Link to comment
Share on other sites

How is your data for the workshops stored? If this is in the database, then you should implement the functionality in your query. If not in a database, then (based upon your code above) it seems you would be repeating the logic over and over for each set of fields. You should create a loop for this.

 

if data is in a database, then only pull records for the workshops matching your condition. Note this is just an example since I would have no idea what your table structures are

SELECT list, of, fields, needed,
   SUM(reservations.attendees) AS total_reservations
FROM workshops
LEFT JOIN reservations
 ON workshops.id = reservations.workshop_id
GROUP BY workshops.id
WHERE workshops.date < NOW()
  AND total_reservations < 25

 

 

If you have the data stored another way please share that info to get a good solution.

Link to comment
Share on other sites

How is your data for the workshops stored? If this is in the database, then you should implement the functionality in your query. If not in a database, then (based upon your code above) it seems you would be repeating the logic over and over for each set of fields. You should create a loop for this.

 

if data is in a database, then only pull records for the workshops matching your condition. Note this is just an example since I would have no idea what your table structures are

SELECT list, of, fields, needed,
      SUM(reservations.attendees) AS total_reservations
FROM workshops
LEFT JOIN reservations
 ON workshops.id = reservations.workshop_id
GROUP BY workshops.id
WHERE workshops.date < NOW()
  AND total_reservations < 25

 

 

If you have the data stored another way please share that info to get a good solution.

 

If he wants to show all the dates but mark some as available and some as reserved, he'd have to get all of them.

Link to comment
Share on other sites

Thanks for all the help!

 

I actually had the correct code on a form but forgot which one!

The code below seems to work

 

 

<?php
$date = "2013-05-01";
$unix_date = strtotime ($date);
if (($cntAll < 2) &&($unix_date > time())) {
 echo "form fields...";
}
?>

 

I just had the count and time parts backwards!

I definitely need to keep better notes!

Link to comment
Share on other sites

It's "and" - It doesn't matter what order you put them in. 

 

(($cntAll < 2) && ($unix_date > time())) is EXACTLY the same as

(($unix_date > time() && $cntAll < 2)) except they get evaluated in a different order. If one is not true, PHP will exit without testing the other.

 

In order for the second version to work, the first would work too.

 

There is no way that was the problem. 

Link to comment
Share on other sites

Darn,

 

This code seems to work though. I am missing something obvious aren't I?

 

<?php
$cntLFALL = "query will count total database rows...";
$date = "2013-05-01";
$unix_date = strtotime ($date);
if (($cntLFALL < 125) && ($unix_date > time())) {
?>
<form>
<?php
 $date = "2013-03-06";
 $unix_date = strtotime ($date);
 if (($cntLF1 < 25) && ($unix_date > time())) {
?>
  <label for="xshort_field_001" class="radio">
   <input type="radio" name="date1" value="2013-03-07" id="xshort_field_001" tabindex="7" />How Full is Your Bucket
  </label>
<?php 
 }
  $date = "2013-04-21";
  $unix_date = strtotime ($date);
  if (($cntLF2 < 25) && ($unix_date > time()))  {
?>
  <label for="xshort_field_003" class="radio">
   <input type="radio" name="date1" value="2013-04-22" id="xshort_field_003" tabindex="9" />Resum&130; Leadership
  </label>
 <?php 
  } 
   $date = "2013-05-02";
   $unix_date = strtotime ($date);
   if (($cntLFALL < 125) && ($unix_date > time())) {
  ?>
  <p>Registration for the Leadership Fundamentals Workshops has closed.</p>
  <?php
  }
 ?>
</form>

Link to comment
Share on other sites

All of those dates are in the future, and your string will evaluate to 0, so every one of those will be true. (You also use $cntLF1 some places which should trigger a warning. Still 0.)

 

You need to clearly state what the problem is. What do you expect to happen? What does happen?

 

Why don't you go ahead and USE the database so you don't have to write all that code over and over.

 

You could do the logic in the query even and save a lot of trouble.

Edited by Jessica
Link to comment
Share on other sites

true, and I have tested with dates in the past too. I should have mentioned that.

 

If I have 12 different workshops then that would mean 12 different queries.

I thought that queries were more load intensive than php if statements. If that's not the case, then queries are definitely the way to go!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.