`Retrieve a page based on a predefined path using nested set - php

I am currently building a CMS and intend to use Joe Celko's nest tree set (MPTT) to retrieve hierarchical data such as a nested deep page based on a defined path. e.g.
example.com/products/phone/specs.
I have a categories tables
cat_id name lft rgt
1 products 1 18
2 phone 2 9
3 specs 3 4
4 gallery 5 6
5 price 7 8
6 laptop 10 17
7 specs 11 12
8 gallery 13 14
9 price 15 16
10
and a pages table
page_id name content
1 specs Here is the specification of the chosen phone
2 price the price of chosen phone
3 specs laptop spec
4 price laptop price
This schema can be found on slqfiddle
Unfortunately, i have no idea on how to do this, the closest idea i got from searching online examples even celko's book does assume you only want to "retrieve the single path" to the parent of a node. Whereas, my use case demands that i retrieve the page (and display it) based on the path set by the url as above.
So, i can i achieve this? All valuable suggestions are welcome. thanks.

Related

Badge System with Variant Criteria, DB Design

Working on the DB design of a Badge System which will work synchronously(request/response) for variant criteria for more than 1000+ badges I am wondering how the badges will be checked when a user does a request.
A first DB Schema I am thinking is:
Badge
id name
1 Check-in 10 Beers, of X Manufacturer the last 30 days
2 Check-in 5 Organic Beers from England
3 Comment on 5 different check-ins
Rules
id key
1 count_of
2 manufacturer_of
3 last_x_days
4 comment_x_diff_checkins
5 from_country_x
BadgeRules
badge_id rule_id value
1 1 10 (check-in 10 beers)
1 2 122 (manufacturer_id)
1 3 30 (days)
The think is that the User makes a check-in and the system should check the different variants for different badges.
As an example need to check on the above DB data that the User made 10 check-ins of Manufacturer with ID 122 the last 30 days. Obviously makes no sense to check 1000+ badges for each request but somehow can be checked only the Badges that the User can win for that check-in.
I was thinking to save for user's state in a different db table but I dont think that this can work for all the rules like last_x_days
UserState
id user_id key value
1 1 checkins 8
2 1 comments 12
P.S. I have seen the Untappd app which has thousands of Badges and it assign them on users request.
I would be grateful for a proposal.

Get next and previous records sorting by non-unique field

I was not able to find an answer while reading similar questions, so I'll ask my question here, appreciate any help on this matter.
In MySQL DB have a table with articles:
id date is_active title text
3 2017-01-20 1 New payment system goes live some articl
5 2017-01-21 1 Library v.2.5 released some articl
6 2017-01-22 1 New skins and themes some articl
7 2017-01-25 0 Terms and Conditions updated some articl
8 2017-01-26 1 Don't forget to subscribe some articl
10 2017-01-22 0 Support Chat beta release some articl
11 2017-01-30 1 Maintenance window next Sunday some articl
12 2017-01-28 1 Refer a friend and get a bonus some articl
13 2017-01-26 1 Follow us in social networks some articl
14 2017-01-22 1 Video sharing feature is now live some articl
I have 2 web pages:
a list of all active articles ordered by date (important: it is possible that several articles can have same date).
This works fine, no issue here.
single article read page, additionally I have "Next" and "Previous" links on that page
This is an issue.
I would like to show 'next' and 'prev' links as a href="article.php?id=8". So I would like to get next record ID from DB according to the query like this:
SELECT id FROM articles WHERE date > (SELECT date FROM news WHERE id = 6) and isactive = 1 ORDER BY date ASC LIMIT 1;
However my query does not work properly when it comes to defining next article id with the same date. When user stays on the article id=3 and clicks 'next' multiple times I would expect the following order of loading articles:
id title date is_active
5 Library v.2 2017-01-21 1
6 New skins a 2017-01-22 1
14 Video shari 2017-01-22 1
8 Don't forge 2017-01-26 1
13 Follow us i 2017-01-26 1
12 Refer a fri 2017-01-28 1
11 Maintenance 2017-01-30 1
But what I get is: 5, 6, 8, 12, 11. So 2 records are missing in this sequence (ids: 14 and 13) because they have same date.
I tried playing with different queries, but all results are not 100% right (in some cases it keeps returning same numbers, ex.: 5, 6, 14, 6, 14....., etc. )
Is is possible to get the 'next' IDs based on current ID and desired order via MySQL query(ies)? I am not against doing several queries or working with nested queries.
As a workaround, of course I can just retrieve an ordered array of all ids like this:
SELECT id FROM articles WHERE isactive=1 ORDER BY date
and then define 'next' and 'previous' using this array, however I don't like this solution.
If you can point me to some similar topics or other possible ways to solve this, please do.
Thank you.
SELECT id
FROM articles
WHERE date >= (SELECT date -- changed
FROM news
WHERE id = 6)
AND isactive = 1
AND id NOT IN(6) -- added, maybe id!=6 or id<>6
ORDER BY date ASC
LIMIT 1
-- (all id date >= date) - (id=6)
Why you do this, why not use
SELECT id FROM news ORDER BY date ASC LIMIT 6,1 -- possition, count
If SQL query caching, this is may be faster.

Mysql return only items that have n number of property matches

I have a database of items and each item has various number of properties. Is it possible for MySql only to return items that have a certain number of matches (not properties) when a search is run?
Example: I am searching for any item with a wheel that is red and has a tire.
This would return all items with these three matches even if they have more properties and would automatically exclude anything that has less than 3 matches.
I have tried playing with the COUNT + GROUP BY + HAVING but I was unable to put together a meaningful working code. Before I spend more time on this I would like to know if it is possible at all.
TABLE DESIGN
ID ITEM PROPERTY
1 1 red
2 1 wheel
3 1 tire
4 2 red
5 2 wheel
6 2 tire
7 2 lamp
8 3 red
9 3 wheel
10 4 red
I would like it to return ITEM 1 and 2
You would do this with a group by and having. You really provide no information about your data structure, but the basic idea is:
select ip.item
from design ip
where ip.property in ('wheel', 'red', 'tire')
group by ip.item
having count(distinct ip.property) = 3;

Detecting columns in a document (table) via php - Algorithm

Given a table like the one below, what would be the best way to detect the two columns separately?
So what I would need the total colspans for the first column.
What is important to remember is that the nr of columns can change.
In the case of this example, the second column starts at "10 euro" (second row). The first section is equal to 2 colspans. The other section is 5 colspans.
Any (abstract) ideas on how to do this?
You must consider the gaps in between the table cells and mark their positions, like this::
0 1 2 3 4 7
0 2 3 4 5 6 7
0 1 2 4 5 7
...
0 2 7
Once you have built an array with above information, you iterate over them and mark the common gap locations:
0 2 7
Since 0 and 7 are both at the edges of your table, you can strip those off. Then you're left with position 2 as the common gap between your rows.
Done :)

8 Puzzle, graph tree paths generator

I'm looking for 8 Puzzle graphs tree generator, preferable in (php+) html+css+javascript. What i need is something like
3 2 1
6 8
7 5 4
will generate all the possible tree, like
3 1 3 2 1
6 2 8 6 8
7 5 4 7 5 4
and so on, until meet its goal.
Any link or algorithm is appreciated. thanks.
See http://www.8puzzle.com/8_puzzle_algorithm.html for the explanation of the algoritm.
Creating such an efficient application is time-consuming, and requires skills in mathematics (field theory) and JS/PHP.
Even if you lack the mathematical background you can define a set of successor states from a certain state. Then throw all that into a search tree and add a little Zobrist hashing to avoid cycles.

Categories