petattlebaum Posted February 22, 2013 Share Posted February 22, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/274824-show-if-date-is-before-and-total-is-less-than/ Share on other sites More sharing options...
Jessica Posted February 22, 2013 Share Posted February 22, 2013 (edited) 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 February 22, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/274824-show-if-date-is-before-and-total-is-less-than/#findComment-1414173 Share on other sites More sharing options...
Psycho Posted February 22, 2013 Share Posted February 22, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274824-show-if-date-is-before-and-total-is-less-than/#findComment-1414179 Share on other sites More sharing options...
Stooney Posted February 22, 2013 Share Posted February 22, 2013 This: $cntAll < 2 doesn't seem like it would be doing this: the total number of people signed up is less than 25 unless you remitted some stuff to make the question simple. Quote Link to comment https://forums.phpfreaks.com/topic/274824-show-if-date-is-before-and-total-is-less-than/#findComment-1414180 Share on other sites More sharing options...
Jessica Posted February 22, 2013 Share Posted February 22, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274824-show-if-date-is-before-and-total-is-less-than/#findComment-1414182 Share on other sites More sharing options...
petattlebaum Posted February 22, 2013 Author Share Posted February 22, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/274824-show-if-date-is-before-and-total-is-less-than/#findComment-1414184 Share on other sites More sharing options...
Jessica Posted February 22, 2013 Share Posted February 22, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274824-show-if-date-is-before-and-total-is-less-than/#findComment-1414185 Share on other sites More sharing options...
petattlebaum Posted February 22, 2013 Author Share Posted February 22, 2013 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> Quote Link to comment https://forums.phpfreaks.com/topic/274824-show-if-date-is-before-and-total-is-less-than/#findComment-1414193 Share on other sites More sharing options...
Jessica Posted February 22, 2013 Share Posted February 22, 2013 (edited) 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 February 22, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/274824-show-if-date-is-before-and-total-is-less-than/#findComment-1414194 Share on other sites More sharing options...
petattlebaum Posted February 22, 2013 Author Share Posted February 22, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/274824-show-if-date-is-before-and-total-is-less-than/#findComment-1414197 Share on other sites More sharing options...
Jessica Posted February 22, 2013 Share Posted February 22, 2013 No, you do ONE query. Quote Link to comment https://forums.phpfreaks.com/topic/274824-show-if-date-is-before-and-total-is-less-than/#findComment-1414198 Share on other sites More sharing options...
petattlebaum Posted February 22, 2013 Author Share Posted February 22, 2013 Thanks for the tip! It certainly minimizes the form code! Live and learn! Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/274824-show-if-date-is-before-and-total-is-less-than/#findComment-1414212 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.