views:

83

answers:

4

I've been working with PHP for a few months now. I've put together a small PHP site with three groups of users: admin, teacher, students.

Here's what I would like to do:

Students are in different groups. I have five different groups now. Each one of these five groups has access to a link that directs them to an online classroom. We can have a variable number of classrooms.

Let's say we have five classrooms for Group 1. Let's say that 20 students from Group 1 click on a link at 5:00p.m. This link brings them to an online classroom. I would like the first five students to be redirected to one classroom/link, the second five students redirected to another classroom/link, the third five students redirected to another classroom and so on.

My questions is, would it be best to do this in PHP? JavaScript? Is this overly complicated or actually quite trivial?

Cheers!

Sam

+3  A: 

This is best to be done in the server side. Give them all the same link. On the page / PHP file behind the link hold a counter which counts the times that the link is requested. Based on the count, redirect to the desired page using the location header.

BalusC
A: 

Well, assuming that you are storing the number of people that have clicked the link so far in a database:

// Get the number of people who clicked from a database or somewhere
$people_clicked = getCurrentNumber();

// Update the Number in the Database ASAP (As per comments)
updateNumber();

// Divide it by room number
// Make sure it is a whole number
$room = floor($people_clicked/5);

// Define each room into an array
$room_links = array("http://firstroom.com", "http://secondroom.com");

// Redirect
// This redirects people 1-5 to the first item in the array, 6-10 to the second, etc.
header("Location: ".$room_links[$people_clicked]);

Basically, you have the link redirect to a page. The page grabs the number of people that have clicked on the link so far. Then you find out which classroom they are going to, and then update the number in the database. Finally, redirect them to the correct location.

There is a lot of open room, but those are the basic steps.

Also, as the comments said, you might want to read/write the number at the same time in order to assure that it is written as close to the call as possible. You might want to to lock the counter on the DB in some fashion, and if the script encounters a locked, sleep for a second and try again.

Chacha102
This solution is vulnerable to several users clicking close together or in rapid succession. The read of the counter occurs several instructions before the counter update. You need to make sure that your read and update are atomic.
Jeff B
A: 

The problem here is that you need some server-side state to make this happen, i.e. a counter of some kind that is stored on the server.

You also need to be careful that this counter can be locked from read until update, or else your numbers will be off. If several people click the link close together, they may all read the same counter value, and you could end up with more than 5 people in a classroom.

Jeff B
A: 

I would define the problem in terms of an array of "seats", and search through the array to find the next available seat.

$seat[0] => 'link1';
$seat[1] => 'link1';
etc.
$seat[4] => 'link1';
$seat[5] => 'link2';
$seat[6] => 'link2';
etc.

That way when a student leaves a classroom, the seat is free for the next student. This minimizes the required number of classes (bandwidth).

Michelle