Skip to content

Instantly share code, notes, and snippets.

@dhimasanb
Created February 14, 2018 16:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhimasanb/c1707b0f1634974f8afafcb0057d19af to your computer and use it in GitHub Desktop.
Save dhimasanb/c1707b0f1634974f8afafcb0057d19af to your computer and use it in GitHub Desktop.
Cari 3 Level yg sama
// Koneksi.php
<?php
class Koneksi {
private $SERVER = "localhost";
private $DATABASE = "contoh_test";
private $USERNAME = "root";
private $PASSWORD = "";
private $koneksi;
public function __construct() {
try {
$dsn = "mysql:host=" . $this->SERVER . ";port=3307;dbname=" . $this->DATABASE;
$this->koneksi = new PDO($dsn, $this->USERNAME, $this->PASSWORD);
} catch (PDOException $e) {
echo $e->getMessage();
}
}
function getKoneksi() {
return $this->koneksi;
}
}
?>
//----------------------------------------------------------------------------------------
// Pohon.php
<?php
include_once 'koneksi.php';
$countSameLevel = 0;
$res = false;
function getByUser($uid) {
$db = new Koneksi();
$koneksi = $db->getKoneksi();
$sql = "SELECT * FROM user where id=$uid";
$con = $koneksi->prepare($sql);
$con->execute();
return $con->fetchAll();
}
function getUpline($uid) {
$db = new Koneksi();
$koneksi = $db->getKoneksi();
$sql = "SELECT * FROM user where upline=$uid";
$con = $koneksi->prepare($sql);
$con->execute();
return $con->fetchAll();
}
function countLevel($upline, $level) {
$db = new Koneksi();
$koneksi = $db->getKoneksi();
$sql = "SELECT * FROM user where upline=$upline AND level=$level";
$con = $koneksi->prepare($sql);
$con->execute();
return $con->rowCount();
}
echo "<ul>";
foreach (getByUser(1) as $row) {
echo "<li>" . $row['name'] . "(" . $row['level'] . ")" . "</li>";
echo "<ul>";
foreach (getUpline($row['id']) as $upline) {
echo "<li>" . $upline['name'] . "(" . $upline['level'] . ")" . "</li>";
if (!$res) {
if (countLevel(1, $upline['level']) >= 3) {
$res = true;
}
}
}
echo "</ul>";
}
echo "</ul>";
echo "dapat bonus?\n";
if ($res) {
echo "<br>dapet<br>";
} else {
echo "<br>nggak<br>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment