Tracking a conversation in PHP MYSQL - php

Background: I have been working with twitter API. Have it setup so that when someone mentions/messages me, it gets sent to a chatbot. Bot make reply, I tweet it out using API.
Each tweet has a unique status_id in addition to a field called rep_to_stat_id so a conversation looks like this:
person 'hi how are you?' stat_id = 1 rep_to_stat_id = blank
me 'ok, and you?' stat_id = 2 rep_to_stat_id = 1
person 'are you stalking me' stat_id = 3 rep_to_stat_id = 2
me 'no you are stalking me' stat_id = 4 rep_to_stat_id = 3
etc.
When I send to bot first time, make a random converstation ID (conv_id) so the bot can track the conversation.
Problem is tracking this, it's like a ladder, leading up to the original stat_id with no rep_to_stat_id, and has to include the common conv_id.
Up until now I have always had tables with a one to many type setup, I cannot get my head around what type of structure this is.
Is there an option in mysql to add to query to follow a ladder of each conversation to its first/last point?

There are many ways to store hierarchical data in relational databases.
One of the most common ways is to simply have a table with all the columns you mentioned (stat_id, rep_to_stat_id [nullable], and conv_id). The problem is, to retrieve the full tree up to the root node, you'd need a recursive function (in MySQL or your scripting language of choice), making several queries.
This Stack Overflow question should give you a nice overview of all the available options for modelling your tree structure.

Have one column tag the conversation_id, and another that tracks the iteration within that conversation.
person text conversation_id reply_num
------------------------------------------------------------------
0 "Hi" 0 0
1 "Hello, who are you" 0 1
0 "I am a bot" 0 2
1 "Goodbye" 0 3
0 "Hi" 1 0 //new conversation thread
1 "who is this?" 1 1
0 "This is a bot" 1 2
1 "leave me alone" 1 3

Related

Creating Matching bonus using PHP

How can I create a networking system where
User A registers person 1 and person 2 in a week and he gets a matching bonus and then when person 1 registers another two people User A gets matching bonus from those two people.
How can I structure the database???
I'm finding it difficult to structure the database. Please can anyone help me?
This is a diagram showing what I'm actually talking about
Diagram 1
Diagram 2
Normally I would assign any bonuses the moment a person get registered. The algorithm assigning the bonuses has to deal with the complexities, not the structure of the database.
Using an algorithm for this is also more flexible, because you can change, and test, the algorithm without changing the structure of the database.
A basic setup for the database could be:
Table USERS:
UserID
Name
RegisteredByUserID
1
person A
0
2
person 1
1
3
person 2
1
4
person X
3
5
person Y
3
Table BONUSES:
BonusID
RewardedToUserID
ForRegisteringUserID
Amount
1
1
2
2,500
2
1
3
2,500
3
3
4
2,500
4
3
5
2,500
This is a very crude example. Basically when a new person is registered an algorithm sorts out which bonuses have to be given to which users and these bonuses are stored in a table.
The 'BONUSES' table can then be used to compute the total bonuses given to each user, and the total of bonuses given out for registering an user.

How to update another row's column after update on a column

As the title says I want to automatically update another row's column after an update on a specific column.
I have this table
id username direct_referral indirect_referral total_referral referrer_id paid
---------------------------------------------------------------------------------
1 aj 100 56 156 1 1
2 john 100 40 140 1 1
3 michael 100 0 100 2 1
Now in this table refferer_id = id of referrer. For example aj has referred 'john' so he get 40% of the total_referral of john i.e.(140*40/100 = 56). john has referred michael so his indirect_referral will be 40% of michael i.e. (100*40/100 = 40).
Now I want to automatically increase indirect_referral of referrer by 40% of the total_referral of referral whenever a new user joins through his referral id and paid = 1.
Please tell me how can I do this process automatically and thanks in advance.
Your problem is tricky. But it might be solvable by a recursive SQL statement, also called CTE (common table expression).
There you have one initial query as an anchor member (the row you want to insert) and a recursive query that calls itself again and again.
Look at that link for an deeper explanation: https://www.mysqltutorial.org/mysql-recursive-cte/
Another way of solving your problem is to make a lot of SQL queries, that you manage by your PHP code.

How to get combination of related rows

I've a large table of articles, and want to get related articles based on a similarity of some kind - for example i've got 2 articles; The first article's title is "Article Part 1" and the second article is "Article Part 2".
What I want effectively is an output of the most likely matches for an article based on title either if its a variation of the current article or whether a continuation of the article or a modification.
Example:
article_title friendly_url id
Article 1 article-1 013
Article 2 article-2 023
Example 1 example-1 034
Example 3 example-3 016
Example 2 example-2 015
So I would ideally like a kind of string match based on the title so then, the "Article 1" related article(s) would be "Article 2" or "Example 3" related article(s) would be "Example 1" and "Example 2".
Note: The title can be more than 2 words and they may include special characters, for example, a title may be "How to: tutorial part 1" and then it will be related to "How to: tutorial part 2".
My idea is that I can use a MySQL query to perform this, but looking around PHP may have some better functions. The table is 1300+ articles, so making this manually by selecting from a list what article it will be related is not a viable solution.
Cheers in advance.
Normalize your database, it will make this kind of stuff much easier. By this I mean make an own row where you write the id of the linked articles. Like:
article_group article_title friendly_url id
0 Article 1 article-1 013
0 Article 2 article-2 023
1 Example 1 example-1 034
1 Example 3 example-3 016
1 Example 2 example-2 015
Then you can have an own table for the name of each article group as well and other info on it. If the title is always the same, but it just have a number that differs them, then you can put that into the group and just keep the number in the above table, and is there any point to the friendly_url then? The latter questions are up to you though.

Need assistance with design of website search using sql query

I have been trying to come up with a way design a webpage to allow users to make selections on a webpage and then query those selections in a database and returned to the user.
For example, my database is a list of houses. The data base table is set up something like this:
Country Region State City House
1 1 1 1 A
1 1 1 1 B
1 1 1 2 C
1 1 1 2 D
1 1 1 3 E
1 2 2 4 G
1 2 3 5 H
1 3 4 6 I
1 4 5 7 J
From the search bar, I want the user to be able to say show me any and all houses in region 2, any houses in state 3, and any houses in city 1.
In this example, the data base query would come back with houses G,H,A, and B. The order of the search and results don't really matter right now.
Is something like this even possible? I have considered using check boxes on the website but ideally I would rather just have one search bar using autocomplete (jquery) where each entry has specific values tied to it. These values are what would be passed to the search query as variables.
you could use comma separator as when you use google maps, then split the content of the search box using the comma delimiter and then you will have the 3 parameters that you need.
Your best bet would be <select> elements in a <form method="GET" action="somefile.php">, and then within the file somefile.php retrieving the $_GET values that the user posted on the form in the <select> elements and then generating SQL queries appropriately to retrieve the data.
The answer is yes, it is possible, but you're asking a lot if you're asking for someone to do it for you.
As #khanahk said, you could use select boxes.
I couldn't figure out how to search for more than just one term at a time.
You could have multiple select boxes, such as four or five on one line, with a submit button to search using the paramters selected by those select boxes.
Or you could do something really cool with a search box that uses keywords to separate the different type of search, ex.) House-a IN region-1
I'd take a look at the following link:
http://www.arroyocode.com/client-side-keyword-search-with-jquery-ui-autocomplete-and-asp-net-mvc-4

How to Store Multiple Options selected by User in a Table

So I want my users to be able to restrict who may contact them.
There are several factors they should be able to filter, including Age (e.g. Must be between 18 - 29), income (must earn between $25,000 - $60,000), what they're looking for (e.g. Friendship, Hang out, etc.), what drugs they do (Marijuana, Meth, Cocaine, etc), etc.
The problem is, I want them to be able to select and store multiple choices for some of the criteria (e.g. drugs), but I do not know how I should store that in the DB or how I should structure the table to best accomplish that.
For example, how would I store a user's row that for "drugs" chose "Marijuana", "Cocaine", and "Heroin" within this context? Would I simply store those as comma-separated values in the "Drugs" column? Or should I do it in a completely different way?
What would be the best way to do this (considering I will obviously have to retrieve and check this information every time a user wants to contact another user) and why?
No, don't store the values in CSV format in the database. Instead create a join table called user_drug and store one row for each user/drug combination:
user
id name income
1 Foo 10000
2 Bar 20000
3 Baz 30000
drug
id name
1 Marijuana
2 Cocaine
3 Heroin
user_drug
user_id drug_id
1 1
1 2
2 1
2 3
3 3
A DB column (at least theorethically) should NOT hold multiple values. Unfortunately, there are some programmers that store multiple values in a single column (values separated by comma for examples) - those programmers (in most cases) destroy the concept of DB and SQL.
I suggest you to read about Database Normalization to get a start in organizing your tables. And, do your best to achieve the Codd's Third Normal Form
You can try with this:
criterium
------------
user_id type value
1 AGE_MIN 18
1 AGE_MAX 29
1 INCOME_MIN 25000
1 INCOME_MAX 60000
1 DRUGS Marijuana
1 DRUGS Meth

Categories