Last active
December 21, 2015 21:48
-
-
Save MaleXachi/6370660 to your computer and use it in GitHub Desktop.
Age Range Using CURDATE() Mysqli and Php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- Tabelstructuur voor tabel `users` | |
-- | |
CREATE TABLE IF NOT EXISTS `users` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`username` varchar(15) NOT NULL, | |
`gender` varchar(10) NOT NULL, | |
`birthdate` date NOT NULL, | |
PRIMARY KEY (`id`), | |
UNIQUE KEY `username` (`username`) | |
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=19 ; | |
-- | |
-- Gegevens worden uitgevoerd voor tabel `users` | |
-- | |
INSERT INTO `users` (`id`, `username`, `gender`, `birthdate`) VALUES | |
(1, 'Mike', 'Man', '1988-06-01'), | |
(2, 'Vera', 'Vrouw', '1988-06-01'), | |
(3, 'Ahmed', 'Man', '1983-04-01'), | |
(4, 'Laura', 'Vrouw', '1996-06-06'), | |
(5, 'Davy', 'Man', '1991-05-02'), | |
(6, 'Gizmo', 'Man', '1980-12-02'), | |
(16, 'Stefanie', 'Vrouw', '1989-03-05'), | |
(17, 'Melanie', 'Vrouw', '1986-11-03'), | |
(18, 'Kippie', 'Man', '1993-03-03'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$db = new mysqli('localhost', 'root', '', 'traindb'); | |
$db->select_db("traindb"); | |
if($db->connect_errno > 0) { | |
die('Er is een fout opgetreden: ' . $db->connect_error); | |
} | |
if($_SERVER['REQUEST_METHOD']=="POST") { | |
$maxDatum = $_POST['MaxAge']; | |
$minDatum = $_POST['MinAge']; | |
// Indien min en max omgedraait zijn dan weer terug draaien. | |
if($minDatum > $maxDatum) | |
{ | |
$temp = $minDatum; | |
$minDatum = $maxDatum; | |
$maxDatum = $temp; | |
} | |
// Dit function berekent geboortedatum naar leeftijdsgetal | |
function Age($Date) | |
{ | |
list($Year, $Month, $Day) = explode("-", $Date); | |
$Day_Diff = date("d") - $Day; | |
$Month_Diff = date("m") - $Month; | |
$Year_Diff = date("Y") - $Year; | |
if($Day_Diff < 0 || $Month_Diff < 0) $Year_Diff--; | |
return $Year_Diff; | |
} | |
$sqlRijder = 'SELECT username, birthdate FROM users WHERE birthdate BETWEEN CURDATE() - INTERVAL '.$maxDatum.' YEAR AND CURDATE() - INTERVAL '.$minDatum.' YEAR'; | |
$result = $db->query($sqlRijder); | |
$CheckQuery = $result->num_rows; | |
if($CheckQuery == 0) | |
{ | |
echo "Er zijn geen resultaten gevonden."; | |
} else { | |
while($row = $result->fetch_assoc()){ | |
$Username = $row['username']; | |
$BirthDate = $row['birthdate']; | |
$Leeftijd = Age($BirthDate); | |
echo $Username . ' - ' . $BirthDate . ' - ' . $Leeftijd . '<br />'; | |
} | |
} | |
$result->close(); | |
} else { | |
?> | |
<form action="" method="post"> | |
Minimale Leeftijd: <input type="text" name="MinAge" placeholder="16"><br /> | |
Maximale Leeftijd: <input type="text" name="MaxAge" placeholder="30"><br /><br /> | |
<input type="submit" name="submit" value="Zoeken"> | |
</form> | |
<?php | |
} | |
$db->close(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment