How to create a proper recursive function for mlm project - php

I've got various solution from SO to create my first MLM project, but now I got stuck in total down-line count (via recursive function), as I've no prior Idea on this, please anyone help me.
My Database Table structure is as below (table name member):
`member_id | member_name | node_left | node_right
where member's relation is as:
member_id (Id 101)
/\
/ \
node_left(Id 102) node_right(Id 103)
/\ /\
/ \ blank / \blank
(again) blank node_right (104)
...... and so on. The above is just an example
`
Now I need to count total downline of any member. eg: suppose of above example, I want to know total downline of member_id 101
How to create the recursive function to do this which ends in finite loop ?
please give me any Idea..

I'm not sure if you've tried to implement the nested set model here, but the implementation doesn't look right..
With a nested set, the left/right values representing your tree structure would look like this:
member 101 (root): left=1, right=8
member 102: left=2, right=5
member 103: left=3, right=4
member 104: left=6, right=7
Then, counting the childs of member #101 would be as simple as:
SELECT COUNT(*) FROM member WHERE node_left > 1 AND node_right < 8

you can use/create your own customize function from the below code, Just check it out and try to implement for your case.
function Recursive_getsubcategory($parent_id,$cat_group,$intLevel)
{
global $intLevel;
$sql = "select *,".$intLevel." as level from tbl_name where parent_id = '".$parent_id."' ";
$result = mysql_query($sql);
$cat_name = $result->fetch_array_multiple();
if(count($cat_name) > 0)
{
for($k=0;$k<count($cat_name);$k++)
{
$cat_group[] = array('id'=>$cat_name[$k]['sub_id'],
'parent_id'=>$cat_name[$k]['parent_id'],
'level' => $cat_name[$k]['level']
);
$parent_id[] = $cat_name[$k]['parent_id'];
//Function For Recursive Get Sub Category...
Recursive_getsubcategory($cat_name[$k]['ebay_sub_id'],$cat_group,$intLevel++);
}
}
// count of total downline nodes
return count($cat_group);
}
This code may helpful for your task.

Related

How can I add a css class to the 10 last entries of a mysql database?

I have a very simple PHP function, fetching all results (from two tables "Items" and "Categories" and displaying them on a single site (directly on index.php)
function fetchAllItems($pdo)
{
$statement = $pdo->prepare('select Items.*, Categories.*
from Items
INNER JOIN Categories ON Items.ItemCategoryID = Categories.id
ORDER BY Items.ItemName ASC'
);
$statement->execute();
return $statement->fetchAll(PDO::FETCH_CLASS, 'Item');
}
Now I want to add an icon to the left with css to the newest (= 10 last sql database entries / rows), but I don't know how to do.
As a PHP newbie my logic goes like this:
Identify the highest id
Subtract the number 10 of the above result / value
Create an if-function that goes something like this: "if $highest_id = within the range of 'lastInsertId() - 10' then apply css-class 'new-item'" (sorry for writing this down like a sentence, not like a real if-function, but as mentioned I am new...)
But I don't really know where to start / go from here and googling for the last 3.15 hours did not bring me any further?!
Thank you.
You can't simply subtract 10 from the highest ID to get the 10th highest, because there can be gaps in the ID sequence.
To get the 10 highest IDs, use:
SELECT id
FROM Items
ORDER BY id DESC
LIMIT 10
Put these into an array $first_10, and then when you're displaying the results of fetchAllItems you can do:
if (in_array($row->id, $first_10) {
$class = "newest";
} else {
$class = "";
}

How to select database query in this case?

Hi I am running into a unique problem. I have database structure of sales order and comparision tables like below.
enter image description here
There will be more records in comparision table.
Basically I want to get the result like the pictures belows. Note: the AFFID can be any random number.
I haven't be able to think of a good way to call SQL. I feel like I have to call SQL then create a new Array that has structure
[
'campaign_left',
'campaign_right'
'Comparision_id'
]
to be able to achieve this.
I think I can get the answer by doing this :
step 1 : get the uniqueAFFIDS array = [1020,1040,1028]
step 2 : sort the uniqueAFFIDS array = [ 1020,1028,1040]
ForEach Comparisions as Comparision
------ ForEach uniqueAFFIDS as uniqueAFFID
------------- $total left = Select Where campaign_id = comparision->campaign_id-left && affid = uniqueAFFID ;
------------- $total right = Select Where campaign_id = comparision->campaign_id-right && affid = uniqueAFFID ;
Then i just display it.
I wonder if anyone has good idea other that this.
Thanks.

Combine multiple queries to get single sum answer

Quite new to all the SQL/PHP stuff - dabbled with basic queries and outputting them to PHP previously but now trying something a bit more complicated and hoping someone can help with this as I've been trying to work it out with no luck so far:
I have 2 MS SQL tables:
Table 1 - Faults
faultid ... requestnumber
1 ........... 6
2 ........... 5
3 ........... 6
Table 2 - actions
faultid ....who ..... when...... timetaken
1.......... John....... Mon......... 1.00
2.......... Peter...... Mon.......... 2.00
3.......... Luke....... Tues........ 1.00
2.......... John....... Tues........ 0.5
1.......... Mike....... Mon......... 0.75
What I am trying to achieve is create a variable I can use in a front end php based webpage that gets a sum of the timetaken column in Table 2 where the requestnumber in Table 1 is equal to a specific number (i.e. 6)
I'm guessing it will start with something like:
$sql1 = "select faultid FROM Faults WHERE requestnumber = '6'";
$sqlresult = sqlsrv_query($conn, $sql1);
while ($row = sqlsrv_fetch_array($sqlresult)){
}
After that I get a bit stuck. How do I take each result from this and then run another query to get the sum of the timetaken column in Table 2 for just the corresponding faultid's? I want to hazard a guess at using foreach but not sure on the syntax (or even if I'm guessing correctly).
So in this example I would get back a result of 2.75 as a variable in PHP.
Any help would be appreciated. Thanks in advance.
Andy
Best to use just one SQL-statement
$sql = 'SELECT SUM(t2.timetaken)';
$sql .= ' FROM Faults t1 INNER JOIN actions t2 ON (t2.faultid = t1.faultid)';
$sql .= ' WHERE t1.requestnumber = ?';
Use this as prepared statement and pass your requestnumber (6 or something) as argument when executing this statement.
To get all or multiple sums you can use group by (maybe combine with WHERE):
SELECT t1.requestnumber, SUM(t2.timetaken)
FROM Faults t1
INNER JOIN actions t2 ON (t2.faultid = t1.faultid)
GROUP BY t1.requestnumber
Edit:
According to your own comment, you can use a SELECT with subquery, but use IN, not =. When using '=' the subquery must return only one row.
select SUM (timetaken) FROM actions WHERE faultid IN (select Faultid from Faults WHERE requestnumber = '6')
-- ^^
But this way is usually slower than the one I posted above

php pdo displaying rowCount in mvc

I am working with a new mvc framework (view here) and I have ran into a slight problem.
I have a table named 'favourite' which looks like below;
---------------------------
id user_id book_id
1 2 1777
2 3 1887
3 2 1023
4 7 8776
5 2 8811
---------------------------
I am trying to display the number of favourites for a specific on their profile page (Hello. You have x favourites). I feel I am misunderstanding something regarding the mvc structure or have a syntax issue somewhere?
On my showprofile I don't have any errors, it's simply blank where the count should be. When I run the query in phpmyadmin I get a result of 3 which is correct.
I have read that rowCount() can be unreliable so I have also tried fetchColumn() amongst other variations. Do I need to use a second query?
New to php/sql/mvc so any advice is appreciated.
My code is as follows;
login_model.php (model)
class LoginModel
{
public function favouriteTotal()
{
$query = $this->db->prepare("SELECT COUNT(book_id) FROM favourite WHERE user_id = 2");
$query->execute();
$count = $query->rowCount()
}
}
showprofile.php (view)
<div>
Hello. Your have this number of favourites:
<?php echo $this->count ?>
</div>
login.php (controller)
class Login extends Controller
{
function showProfile()
{
$login_model = $this->loadModel('Login');
$this->view->count = $login_model->favouriteTotal();
$this->view->render('login/showprofile');
}
}
Your issue is that you're executing a COUNT query, when you should be using a SELECT query if you want to use the rowCount function.
This query would let your rowCount function work:
SELECT `book_id` FROM favourite WHERE user_id = 2
Alternatively, you could continue to use the COUNT statement, but you would then need to get the first result from your query instead of a row count.

create tree through recursive function

I have a table
id,name,parent_id,designation columns,
i want create tree through recursive function in php.
every parent_id is looking in id column and if user login then user can see own and all below records according parent_id.
like
A
|
B
|
c
|
D
|
E
|
F
if A user login then he can all(A,B,C,D,E,F) details.and if B login then see (B,c,D,E,F) and like all... if F login then he can see only own records..
Thanks for advance
create a function fetch_parent;
function fetch_parent($parent_id) {
$query = 'SELECT * FROM `my_table` WHERE `parent_id`='. $parent_id;
// use your own sql class/function whatever to retrieve the record and store it in variable $parent
if($parent->parent_id !== null) { // asuming a 'root' record will have null as it's parent id
fetch_parent($parent->parent_id); // here you go with your recursion
}
return;
}
Then just call the function with the record you want it's parents from:
$first_parent_id = 8;
fetch_parent($first_parent_id);
Notes:
the $parent var can also be an array, depending on the mysql result set
PLEASE PLEASE PLEASE check $parent_id in the query for mysql injection etc.

Categories