Home > Software engineering >  Random number generator in php with twist
Random number generator in php with twist

Time:05-29

Currently I am working on a php Projekt for school. It includes customers and employees. I want to assign a employee to a customer which created a account automatically, but it shouldn't be allowed to only have 1 employee for all customers. It should be even separated. So for example there are 2 employees and 10 customers each then should have 5 customers and not 3 / 7 etc. .

I somehow know how to randomize but the distribution is the problem. Code looks like this for now:

<?php
    //1. Verbindungsaufbau
    $nm = $_POST['nm'];
    $pw = $_POST['pw'];
    $name= $_POST['name'];
    $vname = $_POST['vname'];
    $email = $_POST['email'];
try{
    $pdo = new PDO('mysql:host=localhost;dbname=gold-market_main', 'root', '');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e) {
    echo $e->getMessage();
    die();
}
    
    $query = $pdo->query('SELECT MAX(kundenNr) AS sum FROM kunden');

    $r = $query->fetch(PDO::FETCH_ASSOC);
    $sum = $r['sum'];

    $kundenMAX = $sum   1;
 
    $sql = "INSERT INTO kunden VALUES ('$kundenMAX', '$nm', '$pw','$name','$vname','$email',$emp)";
    $count = $pdo->exec($sql);

  
    if ($count > 0) {
        echo "<div>Der Datensatz wurde erfolgreich gespeichert</div>";
    } else {
        echo "<div>Der Datensatz konnte nicht gespeichert werden</div>";
    }

    //4. Verbindungsabbau
    $pdo = null;
?>

Table:

-- phpMyAdmin SQL Dump
-- version 4.7.2
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Erstellungszeit: 28. Mai 2022 um 19:05
-- Server-Version: 10.5.12-MariaDB-0 deb11u1
-- PHP-Version: 7.1.33

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = " 00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Datenbank: `gold-market_main`
--

-- --------------------------------------------------------

--
-- Tabellenstruktur für Tabelle `mitarbeiter`
--

CREATE TABLE `mitarbeiter` (
  `mitarbeiterNr` int(11) NOT NULL,
  `m_name` varchar(45) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  `m_vorname` varchar(45) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  `m_benutzername` varchar(45) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  `m_passwort` varchar(45) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Daten für Tabelle `mitarbeiter`
--

INSERT INTO `mitarbeiter` (`mitarbeiterNr`, `m_name`, `m_vorname`, `m_benutzername`, `m_passwort`) VALUES
(1, 'Dickins', 'Chad', 'dickie123', '1234');

--
-- Indizes der exportierten Tabellen
--

--
-- Indizes für die Tabelle `mitarbeiter`
--
ALTER TABLE `mitarbeiter`
  ADD PRIMARY KEY (`mitarbeiterNr`);
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Second Table:

-- phpMyAdmin SQL Dump
-- version 4.7.2
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Erstellungszeit: 28. Mai 2022 um 19:05
-- Server-Version: 10.5.12-MariaDB-0 deb11u1
-- PHP-Version: 7.1.33

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = " 00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Datenbank: `gold-market_main`
--

-- --------------------------------------------------------

--
-- Tabellenstruktur für Tabelle `kunden`
--

CREATE TABLE `kunden` (
  `kundenNr` int(11) NOT NULL,
  `k_nutzername` varchar(45) DEFAULT '''NULL''',
  `k_passwort` varchar(45) DEFAULT 'NULL',
  `k_name` varchar(45) DEFAULT NULL,
  `k_nachname` varchar(45) DEFAULT NULL,
  `k_email` varchar(45) DEFAULT NULL,
  `mitarbeiterNr` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Daten für Tabelle `kunden`
--

INSERT INTO `kunden` (`kundenNr`, `k_nutzername`, `k_passwort`, `k_name`, `k_nachname`, `k_email`, `mitarbeiterNr`) VALUES
(1, 'aToMike', '1234', 'Mike', 'Thomson', '[email protected]', 1),
(2, 'gabriel.test', 'test12', 'Gabriel', 'Test', '[email protected]', 1);

--
-- Indizes der exportierten Tabellen
--

--
-- Indizes für die Tabelle `kunden`
--
ALTER TABLE `kunden`
  ADD PRIMARY KEY (`kundenNr`),
  ADD KEY `fk_persoenlicheDaten_mitarbeiter1_idx` (`mitarbeiterNr`);

--
-- Constraints der exportierten Tabellen
--

--
-- Constraints der Tabelle `kunden`
--
ALTER TABLE `kunden`
  ADD CONSTRAINT `fk_persoenlicheDaten_mitarbeiter1` FOREIGN KEY (`mitarbeiterNr`) REFERENCES `mitarbeiter` (`mitarbeiterNr`) ON DELETE NO ACTION ON UPDATE NO ACTION;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;


CodePudding user response:

With this sql command you can get the employe ID, which has the lowest amount of customers assigned

SELECT mitarbeiterNr, count(*) AS mitarbeiterCount FROM kunden GROUP BY mitarbeiterNr ORDER BY mitarbeiterCount ASC LIMIT 1

Query Result

Kunden table

$result = mysqli_query($conn, $sql);
$emp= mysqli_fetch_array($result);
$emp[0]; // Outputs 2
  • Related