Iterating through items and subitems - knowing when to stop [duplicate] - php

This question already has an answer here:
recursive self query
(1 answer)
Closed 9 years ago.
I'm building a task system in php/mysql, imagine I have the following structure:
id | parentid
1 0
2 1
3 1
4 1
5 4
A parentid of 0 means there are no sub items.
So basically, I need to query all items that have a parentid of 1.
In a tree view example I'd get:
1
--2
--3
--4
But I also need to grab all the other subitems:
1
--2
--3
--4
---5
I hope this clears up what I need, this only goes into 3 levels, but I could very well have a huge number of levels.
How should I approach this in MYSQL? Getting a full list of all items with a specific parentid and ALL the other subitems?
Is this even a possible without knowing a limit? Maybe approach this via server side? (but then again, when to stop?)

This question has already been answered. Best way to go about this is using recursive query which MySQL doesn't support.
Here is a workaround for it: Using MySQL query to traverse rows to make a recursive tree
You could do it in PHP or any other language you use for server side but it would take a lot of requests to get all the data you need.

Related

Why have 2 values when i use query select in php? [duplicate]

This question already has answers here:
PHP PDO retrieves duplicate data
(2 answers)
Closed 1 year ago.
I'm newbie in PHP and when I use PHP to query select, I got 2 values, I realize it may be make slow if have 2 duplicated values.
So I want to ask about it and I also want to get just 1 values from select
thanks for reading my question.
here is picture:
It dipends on what your i guess sql query respondes to your query. However if you want cut down your result in sql you can use LIMIT 1
SELECT ...
FROM ...
WHERE ...
LIMIT 1

PHP Select random but variabeles have different weights [duplicate]

This question already has answers here:
MySQL: Select Random Entry, but Weight Towards Certain Entries
(11 answers)
Closed 8 years ago.
I'm working on a simple advertisement script, I have a database with all the ads and a weight.
The ad is a picture and the weight is a number from 1 to 20 depending on it's importance (1 shows up rarely and 20 shows up a lot.
But if I'm getting this information from my database how do I sort and choose an ad to display? So can I put a weight on a variabele in a creative way? It's possible to make and array for each weight point and then select a random ad from that array but is there a better alternative?
#Dagon is correct. Use the example from that other question. Again Dagon found it here: MySQL: Select Random Entry, but Weight Towards Certain Entries The comments are really helpful. You basically multiple that row by its weight. To add to that question and answers I would add the rows into an array and then randomly pick one (one array value) to display. When something has more weight just place it in that array times its weight.

How to return the latest added rows from MySql database. [duplicate]

This question already has answers here:
How can I return 10 of the most recent results in sql?
(4 answers)
Closed 9 years ago.
How can i ask a question to a mysql-database (via PDO) to return the 3 latest added rows? The id column is key and auto incremented. That means that highest id means latest added. Don't take into account the fact that rows can have been deleted and such.
Could i somehow use * and LIMIT 3 and start from the bottom some way - or something?
Should be fairly easy but i am kind of stuck.
Order by ID desc limit 3
gives you the three rows with the highest ID. Those are not necessarily the latest three added rows. But they are the latest three added rows still existing in the table.

MySQL traverse graph data and get non-cyclic path

This one is a hard one for me to solve, basically I have data in a table looking like this:
id a b
1 2 4
2 4 2
3 5 2
4 2 5
5 6 2
6 2 6
7 8 6
8 6 8
9 5 6
...
You can see how complex the structure gets if you try to establish what parent has what children:
[2->[4->[2->RECURSIVE], 5->[2->RECURSIVE], 6->[2->RECURSIVE, 8->[6->[8->RECURSIVE]]]], ...]
I fear MySQL alone can't manage to efficiently find a path, but considering I write this in PHP maybe I could write some way for it to maybe first query the 800 lines of code then program an efficient way to parse the data.
The idea is that this function that I am trying to make, let's call it makePath() takes two parameters and returns an array with sub-arrays and the id's referring to a line in the data table.
For example makePath(2,8) would return:
array(array(2,6,8), array(2,5,6,8))
Since these are the only paths going from 2 to 8 without backtracking (i.e. going [2,5,2,6,8] and infinite other combinations of paths.)
I am stuck on how to start all this, hopefully someone brighter than me on the graph and cyclic theory could explain how I should start my code -thanks for your time!
You should be able to do this with a depth-first search on a directed graph structure. A basic outline would look like:
Create the graph. You'll need at a minimum a list of children and a visiting flag per node.
Call visit with the initial node. For your visit function, you'll need to know the destination node, have a reference to the result array, and keep track of the nodes visited along the current path in a stack.
If the current node is the final node, then add the path traveled (by going through the stack) to the result array.
If the node is being visited, then return.
If the node is not being visited, then
mark the node as being visited
call visit(recursion) with each child
unmark the node as being visited
If you need help with any of the steps, then please ask.

MySQL recursive-query replacement in PHP

Since it looks like recursive queries aren't possible in MySQL, I am wondering if there is a solution to get the same information that also limits the number of queries I make to the database. In my case I have what amounts to a tree and given a node, I make a path back to the root and save the name of the nodes as I go. Given a table like this:
id | parent
-------------
1 |
2 | 1
3 | 1
4 | 2
5 | 2
6 | 5
I want to select all ids on the path from 6 back to 1 (6,5,2,1). Since the total length of the path is unknown I would assume that the only way to do this is taking the results from one query and build a new query until I am back at the root. Then again it has been a couple years since I last used MySQL so it wouldn't surprise me if I am a little out of touch. Any help would be appreciated.
Since it looks like recursive queries aren't possible in mySQL
mySQL doesn't support the 'CONNECT BY' operator, true - but you can implement recursive procedures/functions using mysql and return result sets from them.

Categories