From fedd816e73ab4d667d2ee14d01f57d68a644ce14 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Tue, 3 Apr 2018 20:02:23 +0530 Subject: [PATCH] Add get_longest_chain function and comments. --- update_chain.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/update_chain.php b/update_chain.php index 9b46939..3ca00c7 100644 --- a/update_chain.php +++ b/update_chain.php @@ -7,6 +7,7 @@ if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } +# Sends the given text wrapped in triple backticks as Markdown. function send_code($post_message) { global $bot_api; global $chat_id; @@ -23,6 +24,8 @@ function send_code($post_message) { $result = file_get_contents($url, false, $context); } +# Takes a user array as input. Checks who follows him/her and adds +# him/her to $output array. Exits when a user is followed by no one. function get_chain_from_user($user) { global $conn; $output = array($user); @@ -43,5 +46,29 @@ function get_chain_from_user($user) { return $output; } +# First generates a list of people not following anyone (end_points) +# Then runs get_chain_from_user on all of them and stores them in +# $chains array. Then compares the arrays inside $chains array and +# returns the longest one. +function get_longest_chain() { + global $conn; + $query = "SELECT user_id, username FROM users WHERE follows = -1;"; + $end_points = $conn->query($query); + if ($end_points->num_rows == 0) { + return array(); + } + $chains = array(); + while ($end_point = $end_points->fetch_assoc()){ + $chain = get_chain_from_user($end_points); + array_push($chains, $chain); + } + $longest_chain_index = 0; + for ($i = 0; $i < count($chains); $i++){ + if (count($chains[$i]) > count($chains[$longest_chain_index])) { + $longest_chain_index = $i; + } + } + return $chains[$longest_chain_index]; +} $conn->close(); ?>