How to code this?

Status
Not open for further replies.
Newbie Spellweaver
Joined
Feb 8, 2005
Messages
34
Reaction score
0
I have to do this random list of combination of employees for school, i use MySQL and PHP, the code that i have is this:
PHP:
<?
// Settings
$mysql_server = "localhost";
$mysql_user = "user";
$mysql_password = "pass";
$mysql_database = "employees";
// Conectarse de nuevo
include 'empleo-conect.php';
// Conect Database
$connection = mysql_connect("$mysql_server","$mysql_user","$mysql_password") or die ("Can't connect.");
$db = mysql_select_db("$mysql_database") or die ("Can't connect.");
?>
<?
//$res=mysql_query("SELECT Name, Req FROM name");
$con = mysql_query("SELECT Req FROM name");
$nom = mysql_query("SELECT Name FROM name");

if ($con="Danger")
{
$con=$p;
}
if ($con="Suspect")
{
$con=$s;
}
if ($con="Good")
{
$con=$c;
}
?>
And this is the table, it is only made for using it on mondays:
Code:
<table width="75%" border="1">
  <tr>
    <td>Location/time</td>
    <td>Morning</td>
    <td>Dust</td>
    <td>Afternoon</td>
  </tr>
  <tr>
    <td>Location1</td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td>Location2</td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td>Location3</td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td>Location4</td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td>Location5</td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td>Location6</td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
</table>
So what i wanna do is drop a random couple of employees every time on every location... but the thing is that every employee has different requierements:
-Cook
-Wisdom
-Helpfull
And some of them cant be with others, like this:
-Cook should always be with Wisdom
-Cook mustn't be with Helpfull
-Wisdom could be with Wisdom
-Helpfull could be with Wisdom
-Helpfull may be with Helpfull,
That's the most difficult part, i don't know how to put: "mustn't be with","could be" in php...
The database "employee" is this:
Code:
CREATE TABLE `name` (
  `Name` varchar(20) NOT NULL,
  `Req` varchar(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `nombre` VALUES ('Mariah', 'Cook');
and other names like that...
Please help me cos i need it for next friday!!!
 
I have to do this random list of combination of employees for school, i use MySQL and PHP, the code that i have is this:

So what i wanna do is drop a random couple of employees every time on every location... but the thing is that every employee has different requierements:
-Cook
-Wisdom
-Helpfull
And some of them cant be with others, like this:
-Cook should always be with Wisdom
-Cook mustn't be with Helpfull
-Wisdom could be with Wisdom
-Helpfull could be with Wisdom
-Helpfull may be with Helpfull,
That's the most difficult part, i don't know how to put: "mustn't be with","could be" in php...
The database "employee" is this:

Please help me cos i need it for next friday!!!

Well I propably didn't understand what you meant but lets give it a try anyway.

Assuming you got a table with employees and each employee has a certain attribute. Some of the attributes should not be mixed. But you want to have a random pair of employees.

Try the following idea:

1. Randomly pick 1 employee
2. Randomly pick a second employee (and if it is a one with a 'conflicting' attribute, try again, until you get one that does match)

Here is some dummy code (VB.net, but just to get the idea).

Code:
        dataReader = GetDatabaseRecord("SELECT empID, empAttribute FROM tblEmployee")

        'take one of the random records
        'and store it
        Dim i_EmployeeID As Integer = dataReader("empID")
        Dim s_EmployeeAttribute As String = dataReader("empAttribute")

        Dim s_ConflictAttribute As String = ""
        Select Case s_EmployeeAttribute
            Case "cook"
                s_ConflictAttribute = "wisdom"
            Case "wisdom"
                s_ConflictAttribute = "funny"
        End Select

        'get a another bunch of records which match your required records
        "SELECT * FROM tblEmployees WHERE (empAttribute <> s_EmployeeAttribute) AND (empAttribute <> s_ConflictAttribute) AND (empID <> i_EmployeeID)"

        'again take one of the random records
        'and store it

I realize this isn't the most optimal code and you get a massive amount of records from the database just to dump them the next minute, so you might wanna go for the 'pick random row' options which are floating around on internet but are unfortunatly database depending.
 
Last edited:
Your thread title should contain a tag and be descriptive, simply writing 'How to code this?' is no use. This section also isn't a dump-your-code-here-and-expect-a-response. Try something out yourself and have a REAL go before you come back here. Especially as from what I understand in your first sentence this is a SCHOOL project. Surely you are educated better if you learn it yourself rather than being spoonfed and copying? So go and write some code out and test it, if you can't quite iron out the bugs, come back here.
PHP: Hypertext Preprocessor
SQL Tutorial
 
ever heard of select random ? :)

Pete Freitag said:
Select a random row with MySQL:

SELECT column FROM table
ORDER BY RAND()
LIMIT 1

Select a random row with PostgreSQL:

SELECT column FROM table
ORDER BY RANDOM()
LIMIT 1

Select a random row with Microsoft SQL Server:

SELECT TOP 1 column FROM table
ORDER BY NEWID()

Select a random row with IBM DB2

SELECT column FROM table
ORDER BY RAND()
FETCH FIRST 1 ROWS ONLY

Select a random record with Oracle:

SELECT column FROM
( SELECT column FROM table
ORDER BY dbms_random.value )
WHERE rownum = 1

Yes, I did. But as you can see this isn't quite the default command which is cross-DB compatible.
 
Status
Not open for further replies.
Back