php - Getting the first available null value in an array -


i have small volunteering system working on , in volunteering table, have 2 columns users can volunteer take part in event.

the table looks so:

id event_name user_1 user_2 created_at 

what achieve when user volunteers event, next available user slot given him. ie, if user_1 , user_2 have no values, , user volunteers, assign first available slot him (user_1) , user_id not null, save user id in user_2 column.

i have ran query return user_1 , user_2 columns based on event id. problem having writing php code select first available empty array item can save user id?

hope sort of making sense..

thanks

you'd better off normalising table, events table, , event_volunteers table.

event(id, name, max_volunteers)

event_volunteers(event_id, user_id)

then don't have worry field null or not, , don't limit 2 volunteers per event - ie, more flexibility.

instead, can check if event 'open' volunteers comparing count of event_volunteers max_volunteers field so.

select e.id, e.name   event e     left join event_volunteers ev       on e.id = ev.event_id   group e.id   having count(ev.event_id) < e.max_volunteers 

with left join, return null values fields of event_volunteers if there no volunteers particular event. count() ignores null values, tho there row there count, count 0.

you can limit check specific event introducing where clause.

and assign volunteer event, insert user_id event_volunteers table.

you pull list of of volunteers performing above query, without group by or having clause, eg:

select e.id, e.name, ev.user_id   event e     inner join event_volunteers ev       on e.id = ev.event_id 

this show events there least 1 volunteer (because of inner join). should wish show events, volunteers (if any). change inner join left join, , null values in ev.user_id if there no volunteers event.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -