Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Select random distinct?? PHP

Options
  • 21-11-2006 2:52am
    #1
    Closed Accounts Posts: 8,866 ✭✭✭


    This may be a quickie, or may not. Anyone know how I can get this:

    [php]foreach($_SESSION as $value)
    {
    echo $value." ";
    $staff = $_SESSION[rand(1,count($_SESSION)-1)];
    echo "<font color='blue'>".$staff."</font> <br />";

    }[/php]

    To give out distinct results?

    What I mean is, as you may have noticed, I'm trying to develop a staff rotation program, and sometimes the result from that loop will give me back the same member of staff twice in one day, which I obviously don't want. Any ideas? Thanks!


Comments

  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    Could you add another item to the 'staff' elements to indicate that that person has already been picked/assigned?
    You'd probably have to change the rand code to be a loop of 'do .. while not already assigned'
    You'd reset the 'already assigned' data after each day.


  • Registered Users Posts: 32,136 ✭✭✭✭is_that_so


    Yep some kind of flag do while $i_am_selected != true


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Ok thats sounds sensible, but to be honest, I've sort of shied away from arrays in the 9 or 10 months I've been learning php, so while I understand them, I'm no master! Can you elaborate a bit on how I would specifiy this flag?


  • Closed Accounts Posts: 67 ✭✭verbatim


    How about randomly swapping the staff around in that array. That way you will have distinct results, without having to worry about storing flags etc...


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Again, I'm a little bit lost here, as I don't have any real experience with more than basic arrays, can you elaborate please?


  • Advertisement
  • Closed Accounts Posts: 1,200 ✭✭✭louie


    have a look into [php]array_unique($pr_name_array);[/php]

    I had to do something like this a while ago.


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Ok I got it from what I interpreted from verbatims post, cheers! Here's the code now:

    [php]
    <?php

    session_start();

    $_SESSION = array($_POST, $_POST, $_POST, $_POST);

    $_SESSION = array($_POST, $_POST, $_POST, $_POST, $_POST, $_POST, $_POST, $_POST, $_POST);

    shuffle($_SESSION);

    shuffle($_SESSION);

    ?>

    <table width="98%" border="2px">
    <tr>
    <td>

    <?

    echo "<strong>Friday: <br /><br /></strong>";

    echo "Supervisor: <font color='red'>".$_SESSION[0]."</font> <br /><br />";

    foreach($_SESSION as $value)
    {
    $i++;
    echo $value." ";
    echo "<font color='blue'>".$_SESSION[$i]."</font> <br />";

    }

    $i=0;

    echo "<br /><br />";

    ?>

    </td>
    <td>

    <?

    shuffle($_SESSION);

    shuffle($_SESSION);

    echo "<strong>Saturday: <br /><br /></strong>";

    echo "Supervisor: <font color='red'>".$_SESSION[0]."</font> <br /><br />";

    foreach($_SESSION as $value)
    {
    $i++;
    echo $value." ";
    echo "<font color='blue'>".$_SESSION[$i]."</font> <br />";

    }

    $i=0;

    echo "<br /><br />";

    ?>

    </td>
    <td>

    <?

    shuffle($_SESSION);

    shuffle($_SESSION);

    echo "<strong>Sunday: <br /><br /></strong>";

    echo "Supervisor: <font color='red'>".$_SESSION[0]."</font> <br /><br />";

    foreach($_SESSION as $value)
    {
    $i++;
    echo $value." ";
    echo "<font color='blue'>".$_SESSION[$i]."</font> <br />";

    }

    $i=0;

    echo "<br /><br />";

    ?>

    </td>
    </tr>
    <tr>
    <td>

    <?

    shuffle($_SESSION);

    shuffle($_SESSION);

    echo "<strong>Monday: <br /><br /></strong>";

    echo "Supervisor: <font color='red'>".$_SESSION[0]."</font> <br /><br />";

    foreach($_SESSION as $value)
    {
    $i++;
    echo $value." ";
    echo "<font color='blue'>".$_SESSION[$i]."</font> <br />";

    }

    $i=0;

    echo "<br /><br />";

    ?>

    </td>
    <td>

    <?

    shuffle($_SESSION);

    shuffle($_SESSION);

    echo "<strong>Tuesday: <br /><br /></strong>";

    echo "Supervisor: <font color='red'>".$_SESSION[0]."</font> <br /><br />";

    foreach($_SESSION as $value)
    {
    $i++;
    echo $value." ";
    echo "<font color='blue'>".$_SESSION[$i]."</font> <br />";

    }

    $i=0;

    echo "<br /><br />";

    ?>

    </td>
    <td>

    <?

    shuffle($_SESSION);

    shuffle($_SESSION);

    echo "<strong>Wednesday: <br /><br /></strong>";

    echo "Supervisor: <font color='red'>".$_SESSION[0]."</font> <br /><br />";

    foreach($_SESSION as $value)
    {
    $i++;
    echo $value." ";
    echo "<font color='blue'>".$_SESSION[$i]."</font> <br />";

    }

    $i=0;

    echo "<br /><br />";

    ?>

    </td>
    </tr>
    <tr>
    <td>

    <?

    shuffle($_SESSION);

    shuffle($_SESSION);

    echo "<strong>Thursday: <br /><br /></strong>";

    echo "Supervisor: <font color='red'>".$_SESSION[0]."</font> <br /><br />";

    foreach($_SESSION as $value)
    {
    $i++;
    echo $value." ";
    echo "<font color='blue'>".$_SESSION[$i]."</font> <br />";

    }

    $i=0;

    echo "<br /><br />";

    ?>

    </td>
    </tr>
    </table>
    [/php]

    I'm now wondering what the best way is to specify certain parameters, i.e. staff that only do weekdays/weekends etc. Any thoughts?


  • Registered Users Posts: 32,136 ✭✭✭✭is_that_so


    Does this application have a DB ? If so you can sort out all that in the DB through defined record fields. The application will just display what it's given. If you don't have a DB you should add one. It will increase the potential data you can store on each member of staff.


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Yeah I was sort of thinking a database might be in order... I wanted to be able to expand the system such that it could be used for other branches too, which could be done using dynamicaly created arrays etc but its a lot of work, I suppose I'll just go all out and do up a proper CMS for it... Its annoying me now because I had been thinking at any point I'd gotten stuck " i could do this if it were just a feckin sql query!"


  • Closed Accounts Posts: 2,046 ✭✭✭democrates


    Sounds like you want to create a shift roster.

    If so, easiest way is manually, use a spreadsheet to map out the shifts required by the enterprise by filling in the cells with grey per person required. Eg columns are days in the roster and rows are the shifts from start to finish eg 08:00-16:00, and if 2 people are required stick in 2 of those rows etc.

    Then type in different combinations for each person fulfilling certain shifts over the week(s) of the roster and by trial and error find something that works without end-to-end shifts and with decent breaks for any particular roster line.

    This is mathematically a "hard problem", and software may gice a solution by brute force but not easily minimise things like finishing at 4pm and being back in at midnight etc. Faster to set it with the spreadsheet and print it out for all to see.

    People like knowing in advance when they're on or not, swaps can deal with holidays, and sickies are a disaster regardless of how the roster is concocted.


  • Advertisement
  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Cheers for the input democrates, but I'd like to see if I can achieve this little yoke for my own benefit as much as anyone elses. And the random generation is handy, the hours are different week to week (its for the cinema I work in).

    I'm a bit stumped again, but not seriously. I'm sorting through each shift to determine if it crosses the 10pm mark (at which point anyone under sixteen must promptly shag off!) and I thought I had it sorted. I've created a database, it holds the age of the staff members. I've defined several functions to output the staff, here's some info:

    [php]

    <?php

    include('db.php');

    session_start();

    function getweekdaystaff($cinema_id, $before_ten) {

    $SQL="SELECT * FROM tbl_staff WHERE staff_cinema_id = '$cinema_id'";

    if($before_ten = '0'){

    $SQL.=" AND staff_age > '18'";

    }
    $SQL.=" AND staff_supervisor !='1' AND staff_weekdays = '1' AND staff_selected = '0' ORDER BY RAND() LIMIT 1";

    $result=mysql_query($SQL) or die(mysql_error());
    $row=mysql_fetch_array($result);

    $staff = $row;

    echo $staff;

    echo " ".$before_ten;

    $update="UPDATE tbl_staff SET staff_selected = '1' WHERE staff_id = {$row}";
    $update_result=mysql_query($update) or die(mysql_error());

    }

    function getweekendstaff($cinema_id, $before_ten) {

    $SQL="SELECT * FROM tbl_staff WHERE staff_cinema_id = '$cinema_id'";

    if($before_ten = '0'){

    $SQL.=" AND staff_age > '18'";

    }

    $SQL.=" AND staff_supervisor !='1' AND staff_weekends = '1' AND staff_selected = '0' ORDER BY RAND() LIMIT 1";

    $result=mysql_query($SQL) or die(mysql_error());
    $row=mysql_fetch_array($result);

    $staff = $row;

    echo $staff;

    $update="UPDATE tbl_staff SET staff_selected = '1' WHERE staff_id = {$row}";
    $update_result=mysql_query($update) or die(mysql_error());

    }

    function getweekdaysuper($cinema_id) {

    $SQL="SELECT * FROM tbl_staff WHERE staff_cinema_id = '$cinema_id' AND staff_supervisor ='1' AND staff_weekdays = '1' ORDER BY RAND() LIMIT 1";
    $result=mysql_query($SQL) or die(mysql_error());
    $row=mysql_fetch_array($result);

    $super = $row;

    echo $super;

    }

    function getweekendsuper($cinema_id) {

    $SQL="SELECT * FROM tbl_staff WHERE staff_cinema_id = '$cinema_id' AND staff_supervisor ='1' AND staff_weekends = '1' ORDER BY RAND() LIMIT 1";
    $result=mysql_query($SQL) or die(mysql_error());
    $row=mysql_fetch_array($result);

    $super = $row;

    echo $super;

    }

    function clearstaff() {

    $clear="UPDATE tbl_staff SET staff_selected = '0'";
    $clear_result=mysql_query($clear) or die(mysql_error());

    }

    function shifttime($value) {

    $str = $value;
    if(substr($str,-5,2) > '03' && substr($str,-5,2) <= '22') $before_ten = '1';
    if(substr($str,-5,2) > '22' || substr($str,-5,2) >= '00' && substr($str,-5,2) < '03') $before_ten = '0';

    return $before_ten;

    }


    ?>

    [/php]

    As you can see, I'm checking the 24 hour format time string to determine whether the shift borders 10pm, which works fine. The problem I'm having is that while shifttime() works (if i echo it for each shift the output is correct),

    the getstaff functions wont take the $before_ten operator at all... I imagine I have to globalise something or other but I've tried "global $before_ten" within the shifttime() function to no avail! Here's how I'm working the day loops:

    [php]

    <?

    echo "<strong>Friday: <br /><br /></strong>";

    echo "Supervisor: <font color='red'>";
    getweekdaysuper($cinema_id);
    echo "</font> <br /><br />";

    foreach($_SESSION as $value)
    {

    shifttime($value);

    $i++;
    echo $value.": ";
    echo "<font color='blue'>";
    getweekdaystaff($cinema_id, $before_ten);
    echo "</font> <br />";

    }

    clearstaff();

    $i=0;

    echo "<br /><br />";

    ?>

    [/php]

    Can anybody see where I'm going wrong? Thanks.


  • Registered Users Posts: 683 ✭✭✭Gosh


    Without going too much into the code I can see a few problems when you are comparing $before_ten, you only have 1 = sign, should be ==.

    Try changing this and post back ...


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Nope, no joy with that Gosh!


  • Registered Users Posts: 683 ✭✭✭Gosh


    in functions getweekendstaff and getweekdaystaff make sure you change:

    [php]
    if($before_ten = '0'){
    [/php]
    to
    [php]
    if($before_ten == '0'){
    [/php]
    and the call to function shifttime in your foreach loop should be

    [php]
    $before_ten = shifttime($value);
    [/php]

    or alternatively you could change the call to getweekdaystaff and getweekendstaff as follows:

    [php]
    getweekdaystaff($cinema_id, shifttime($value));

    getweekendstaff($cinema_id, shifttime($value));

    [/php]

    and remove the shifttime($value) line in the foreach loop


Advertisement