binary tree child counting php mysql - php

I am stuck in some situation where I have to count Left Child and Right Child of a Binary Tree My database structure is as under.
SELECT id,usr_name,rid,pid,l_mem,r_mem,position,joining_date FROM user WHERE id = '$id'"
Where rid is = referral id And pid = parent id,
I need to count all leaves of given parent id for example
if id 1 has left 2 and right 3 immediate childs i need to know total left member count and total right members count.
1
/ \
2 3
/ \ / \
4 5 6 7
/ \ \
8 9 11
/ \
10 12
/ \
13 14
I need to count all childs of 1.
I am using this function but it is only counting most left please modified it or explain your own
function leftcount($id) //Function to calculate leftcount
{
$sql = "SELECT id,usr_name,rid,pid,l_mem,r_mem,position,joining_date FROM user WHERE id = '$id'";
$execsql = mysql_query($sql);
$array = mysql_fetch_array($execsql);
//var_dump($array);
(array_count_values($array));
if(!empty($array['l_mem']))
{
$count += leftcount($array['l_mem']);
}
$totalcount = 1 + $count;
return $totalcount ;
}
$left = leftcount($id);
doing -1 because in function 1 + $count.
$left = $left-1;
Please do not mark Duplicate or any other if you don't have solution

You need to use these 3 function to calculate the left right and all child of any element.
function leftcount($id) //Function to calculate all left children count
{
$sql = "SELECT id,usr_name,rid,pid,l_mem,r_mem,position,joining_date FROM user WHERE id = '$id'";
$execsql = mysql_query($sql);
$array = mysql_fetch_array($execsql);
(array_count_values($array));
$count = 0;
if(!empty($array['l_mem']))
{
$count += allcount($array['l_mem']) +1;
}
return $count;
}
function rightcount($id) //Function to calculate all right children count
{
$sql = "SELECT id,usr_name,rid,pid,l_mem,r_mem,position,joining_date FROM user WHERE id = '$id'";
$execsql = mysql_query($sql);
$array = mysql_fetch_array($execsql);
(array_count_values($array));
$count = 0;
if(!empty($array['r_mem']))
{
$count += allcount($array['r_mem']) +1;
}
return $count;
}
function allcount($id) //Function to calculate all children count
{
$sql = "SELECT id,usr_name,rid,pid,l_mem,r_mem,position,joining_date FROM user WHERE id = '$id'";
$execsql = mysql_query($sql);
$array = mysql_fetch_array($execsql);
(array_count_values($array));
$count = 0;
if(!empty($array['l_mem']))
{
$count += allcount($array['l_mem']) +1;
}
if(!empty($array['r_mem']))
{
$count += allcount($array['r_mem']) +1;
}
return $count;
}
If you pass 1 to these function. answer for these are as follows
echo leftcount(1); // 8
echo rightcount(1); // 5
echo allcount(1); // 13

Related

Each row has the same random number being inserted

I'm looking to insert random numbers into each column in a database.
The issue that is occuring is that each column for some reason has the same random generated value being passed inside.
$sql = "SELECT * FROM players";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$targScore = 3;
while($row = $result->fetch_assoc()) {
$target = 3; // The target number the sequence is adding up to
$n = 4; // Max numbers within sequence
while ($n) {
if (1 < $n--) {
$addend = rand(0, $target - ($n - 1));
$target -= $addend;
$num[] = $addend;
} else {
$num[] = $target;
}
}
/* Rows to update */
/* Output looks like:
[0] = 1
[1] = 2
[0] = 0
[0] = 1
*/
$changeScore = "UPDATE players SET doja='$num[0]' WHERE score='$targScore'";
$conn->query($changeScore);
$changeScore = "UPDATE players SET 7acres='$num[1]' WHERE score='$targScore'";
$conn->query($changeScore);
$changeScore = "UPDATE players SET tweed='$num[2]' WHERE score='$targScore'";
$conn->query($changeScore);
$changeScore = "UPDATE players SET bickel='$num[3]' WHERE score='$targScore'";
$conn->query($changeScore);
}
}
The data looks like: https://imgur.com/a/nhhZDVo
What I need is that each loop, it will run the randomizer again but change the numbers its inserting so that each row will have different numbers.
You need to first get a list of all the rows that meet the score condition. Then you have to update them one by one, so you can set different random values in each.
$select_stmt = $conn->prepare("SELECT id FROM players WHERE score = ?");
$select_stmt->bind_param("i", $targScore);
$select_stmt->execute();
$res = $select_stmt->get_result();
$update_stmt = $conn->prepare("UPDATE players SET doja = ?, 7acres = ?, tweed = ?, bickel = ? WHERE id = ?");
$update_stmt->bind_param("iiiii", $doja, $acres, $tweed, $bickel, $id);
$target = 3;
while ($row = $res->fetch_assoc()) {
$n = 4;
$num = [];
while ($n) {
if (1 < $n--) {
$addend = rand(0, $target - ($n - 1));
$target -= $addend;
$num[] = $addend;
} else {
$num[] = $target;
}
}
[$doja, $acres, $tweed, $bickel] = $num;
$id = $row['id'];
$update_stmt->execute();
}
There's also no need to use four UPDATE statements to update 4 columns in the same rows.
MySQL has a rand() function https://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_rand
(Note the warning that it's not perfectly random / security secure, but nor is PHPs rand()).
If you do not need to know what the values are, you can simply do UPDATE myCol=FLOOR(RAND() * 200) WHERE myConditionalField=MyCondition
This will allocate a random number from 0 to 199; examples in the MySQL manual (linked above) as to how to set a range, e.g. FLOOR(7 + (RAND() * 5)) gives an integer from 7 to 11.

how to get post page with post id in MySQL

i wrote a query to get my posts with DESC order as you see its limitet to show 10 posts per page
but how i can find the post with "pid = 18" in what page?
TABLE:
pid, posterid, content
1 , 26 , blabla
2 , 8 , sec balnla
3 , 9 , lollll
4 , 26 , orddddd
5 , 10 , sssssdsd
...
PHP:
function getComments($poster){
$sql = "SELECT * FROM table WHERE posterid = $poster"
$smt = $db->query($sql);
$total = $smt->rowCount();
$result = $db->prepare($sql . " ORDER BY pid DESC $limit");
$result->execute();
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$Comments[] = $row;
}
return [
'comments' => $Comments,
'total' => $total
];
}
NEEDED FUNCTION:
$poster = 26;
$pid = 18;
$posttperpage = 10;
public function getPostPage($pid, $poster, $posttperpage)
{
$page = '';
$totlaPosts = getComments($poster)['total']; //70
$pages = $totlaPosts / $posttperpage; //7
for($i=0, $i<$totlaPosts, $i--)
{
// ?????
// i confuzed no idead what can i do :(
}
return $page; //it must be 2
}
Since pid is not necessarily consistent (you might have deleted some rows) you should have another query.
$stmt = $db->prepare("SELECT COUNT(1) FROM table WHERE pid <= ? AND poster = ?");
$stmt->execute([$pid, $poster]);
$count = $stmt->fetchColumn();
echo ceil($count / $posttperpage);
If you're looking to determine what page a post is on:
$k = 1;
for($i=0, $i<$pid; $i += $posttperpage){
$page = $k;
$k +=1;
}
//after loop $page now contains what page the post is on.
Is this what you're looking for?
In your LIMIT 0,10 you have a range of 10 items per page, right? If you want to keep track of which page you are viewing you need a variable that behaves like this:
$range = 10;
$currentPage = 2;
$offset = $range * ($currentPage - 1);
Inside your query you can change that 0 to $offset and there you have it, if you find your result in with this query it means your pid is in page 2, you can also increase the $currentPage by 1 if you can't find your result and so on.
This algo is simple,
You need to know the total of row, the position of your specific post and number of post per page.
And you do like this :
floor(position_of_you_post / number_total_of_post * number_of_post_per_page)
so if you have 100 post with 10 post per page and your post is the number 51, you can do :
51 /100 * 10 = 5.1
so the post is on the page 5

Select Query in Group of 10

I followed one link here, and modified my code accordingly. What I am trying to do achieve is for example a table name media_ids contains 45 rows(which is dyanamic), I want to divide the no of rows in a group of 10 rows minimum (in my case four groups of 10 rows and 1 group of 5) and execute code of each group per hour.
For example from select record from id 1 to 10 then second hour from 11 to 20 and so on unless fetches the last record and start again from id 1.
I added a tracker, which check whats the current limit and proceed.But its giving me the required result what I am trying to achieve.
<?php
require('inc/dbConnect.php');
$lastPointerq = "select tracker from media_tracker";
$lastPointerResult = mysqli_query($con, $lastPointerq);
$lastPointerRes = mysqli_fetch_row($lastPointerResult);
$lastPointer = $lastPointerRes[0];
$lastPointer10=$lastPointer+10;
$currentMediaQuery = "select * from media_ids where id > ".$lastPointer." limit ".$lastPointer.",".$lastPointer10."";
$mediaQuery = mysqli_query($con, $currentMediaQuery);
if (mysqli_num_rows($mediaQuery) > 0) {
while ($row = mysqli_fetch_assoc($mediaQuery)) {
// do stuff…
echo $id=$row['id']."<br>";
}
}
else
{echo "0";}
if ($lastPointer + 10 > 40) {
$lastPointer = 1;
} else {
$lastPointer += 10;
}
mysqli_query($con, "update media_tracker set tracker = $lastPointer");
?>
I am assuming you are calling this script every hour via ajax/Js...
In your code if you delete some data from another script you may find your first row id i.e 100 !
require('inc/dbConnect.php');
$limit = 10 ;
$lastPointerq = "select tracker from media_tracker";
$lastPointerResult = mysqli_query($con, $lastPointerq);
$lastPointerRes = mysqli_fetch_row($lastPointerResult);
$lastPointer = $lastPointerRes[0];
$lastPointerRes = mysqli_fetch_assoc($lastPointerResult);
$lastPointer = empty($lastPointerRes['tracker']) ? 0 : $lastPointerRes['tracker'] ;
$lastPointer10=$lastPointer* $limit;
$currentMediaQuery = "select * from media_ids limit ".$limit." OFFSET ".$lastPointer10."";
$mediaQuery = mysqli_query($con, $currentMediaQuery);
if (mysqli_num_rows($mediaQuery) > 0) {
while ($row = mysqli_fetch_assoc($mediaQuery)) {
// do stuff…
echo $id=$row['id']."<br>";
}
}
else
{echo "0";}
//do a total row count query here and set as value of $total rather than 40
$total =40 ;
$flag = ceil($total/$limit);
if ($lastPointer == $flag) {
$lastPointer = 0;
} else {
$lastPointer ++;
}
mysqli_query($con, "update media_tracker set tracker = $lastPointer");

How to get total from previous record in php mysql

I have a problem with this:
count this id no 1 = usage/nod
count this id no 2 = (usage[id1]+usage[id2])/(nod[id1]+nod[id2])
count this id no 3 = (usage[id1]+usage[id2]+usage[id3])/(nod[id1]+nod[id2]+nod[id3])
And so on...
"usage/nod" is a field in the database.
How can I count that with PHP and MySQL?
$total_usage = 0;
$total_nod = 0;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$total_usage += $row['usage'];
$total_nod += $row['nod'];
$div = $total_usage/$total_nod;
echo "count this id no $row[id] = $div\n";
}

PHP sorting array output

UPDATE 2 (Players Handicap Index Calculation)
$sql3 = "SELECT roundID FROM rounds WHERE userID='$userID'";
$result3 = mysql_query($sql3) or die(mysql_error());
$total_rounds = mysql_num_rows($result3);
//CALCULATE USER HANDICAP INDEX IF TOTAL_ROUNDS > 4
if($total_rounds > 4){
if($total_rounds<7) { $score_count = 1; }
elseif($total_rounds<9) { $score_count = 2; }
elseif($total_rounds<11) { $score_count = 3; }
elseif($total_rounds<13) { $score_count = 4; }
elseif($total_rounds<15) { $score_count = 5; }
elseif($total_rounds<17) { $score_count = 6; }
elseif($total_rounds<18) { $score_count = 7; }
elseif($total_rounds<19) { $score_count = 8; }
elseif($total_rounds<20) { $score_count = 9; }
else { $score_count = 10; }
$sql2 = "SELECT differential FROM rounds WHERE userID='$userID' ORDER BY date DESC LIMIT 20";
$result2 = mysql_query($sql2) or die(mysql_error());
$diff_results = array();
while($row = mysql_fetch_assoc($result2)){
$diff_results[] = $row['differential'];
}
sort($diff_results);
$diff_results = array_slice($diff_results, 0, $score_count);
$handicapIndex = array_sum($diff_results) / $score_count * 0.96;
$handicapIndex = (floor($handicapIndex * 10)) / 10;
Hopefully this will give you all and idea of how I calculate a players handicap index. Now I would like to show the player (user) the rounds (date ordered) that are used to calculate his index.
Always appreciative!
UPDATE (structure of rounds table)
roundID - auto incrementing primary key
userID - INT
courseID - INT
tee - VARCHAR
differential - FLOAT
date - DATE
I am struggling to even get started with this feature I am trying to implement. Any help would be much appreciated.
I have a set of mysql db results I would like to sort by field differential. I pull them out of the db like this:
$sql4 = "SELECT * FROM rounds WHERE userID='$userID' ORDER BY date DESC LIMIT 20";
$result4 = mysql_query($sql4) or die(mysql_error());
$total_rounds = mysql_num_rows($result4);
As you can see above I counted the rows returned to run through this if-elseif-else statement to figure out how many scores I need to highlight with different css:
if($total_rounds<7) { $score_count = 1; }
elseif($total_rounds<9) { $score_count = 2; }
elseif($total_rounds<11) { $score_count = 3; }
elseif($total_rounds<13) { $score_count = 4; }
elseif($total_rounds<15) { $score_count = 5; }
elseif($total_rounds<17) { $score_count = 6; }
elseif($total_rounds<18) { $score_count = 7; }
elseif($total_rounds<19) { $score_count = 8; }
elseif($total_rounds<20) { $score_count = 9; }
else { $score_count = 10; }
For example, if $total_rounds = 16 my $score_count would be 6.
Now I need to take this data set of 16 rows and spit it out with php so I maintain my ORDER BY date while applying a different css format to the 6 figured in the above if-elseif-else statement. The $score_count is figured because I need to highlight (aka apply different css) to the 6 lowest scores of the 16 row data set WHILE maintaining my date order.
The desired output would look like this (with the * denoting the separate css format *).
01-08-2013 - 16
01-07-2012 - 1 *
01-06-2013 - 15
01-05-2012 - 2 *
01-04-2013 - 14
01-03-2012 - 3 *
01-02-2013 - 13
01-01-2012 - 4 *
12-31-2012 - 12
12-30-2012 - 5 *
12-29-2012 - 11
12-28-2012 - 6 *
12-27-2012 - 10
12-26-2012 - 9
12-25-2012 - 8
12-24-2012 - 7
Please let me know if you have questions.
Thanks
There are couple of steps that you will have to follow.
1) Sort the results with score(differential) and get the ids of the six records having lowest score.
$sql4 = "SELECT * FROM rounds WHERE userID='$userID' ORDER BY differential LIMIT 20";
$result4 = mysql_query($sql4) or die(mysql_error());
$total_rounds = mysql_num_rows($result4);
if($total_rounds<7) { $score_count = 1; }
elseif($total_rounds<9) { $score_count = 2; }
elseif($total_rounds<11) { $score_count = 3; }
elseif($total_rounds<13) { $score_count = 4; }
elseif($total_rounds<15) { $score_count = 5; }
elseif($total_rounds<17) { $score_count = 6; }
elseif($total_rounds<18) { $score_count = 7; }
elseif($total_rounds<19) { $score_count = 8; }
elseif($total_rounds<20) { $score_count = 9; }
else { $score_count = 10; }
$idsArray = array();
for($i=0; $i<$score_count; $i++){
$dets = mysql_fetch_array($result4);
$idsArray[] = $dets['roundID'];
}
2) Loop over records and match the id in array that you have made by first step. If id matches apply CSS otherwise not.
$sql4 = "SELECT * FROM rounds WHERE userID='$userID' ORDER BY date DESC LIMIT 20";
$result4 = mysql_query($sql4) or die(mysql_error());
while($dets = mysql_fetch_array($result4)){
if(in_array($dets['roundID'], $idsArray)){
//apply CSS
//display details here
}
else{
//display details here
}
}
Note: You should stop using mysql_* now. Its highly recommended by experts to use mysqli_* instead
I will take your example of $total_rounds = 16 my $score_count would be 6.
$total_rounds = 16;
$score_count = 6 // comes from the if-else loop you already have
// now we loop over the result
// the css is applied to every odd result, until $score_count is not 0
$counter = 0;
while( ( $data = mysql_fetch_assoc( $result4 ) ) !== false ) {
if( ( $counter % 2 ) && $score_count ) {
echo $data['date']; // apply your css here
} else {
echo $data['date'];
}
$counter++;
$score_count--;
}
Hope this helps.

Categories