rearraging converstaions with lastest on top in php + mysql - php

we have building a chat application using php and mysql and our table structure looks like this
converstation_table_name
id
conversation_id
created_at
user_1
user_2
messages
sender
reciever
message
created_at
conversation_id
here are the values in both tables
conversation_table_name
id | con_id(shortname) | created_at | user_1 | user_2 |
1 | 123132 | now | 1 | 5 |
2 | 123133 | now | 1 | 3 |
3 | 123134 | now | 1 | 4 |
4 | 123135 | now | 1 | 2 |
messages
sender | reciever | message | created_at | conversation_id |
1 | 3 | abc | now | 123133 |
3 | 1 | cee | now | 123133 |
1 | 2 | 1411 | now | 123135 |
1 | 5 | 1-5 | now | 123132 |
now we want to arrange the output something like this
123132
between 1 - 5
123135
between 1 - 2
123133
between 1 - 3
how to go about it, in short we want to make the latest message in user conversation to appear on top, just like facebook does with it's messaging system, how to go about it, we are still looking for a working answer

Can be introduced new field viewed or seen with datetime field. On visiting message update the field. So can be used order by that field and you will have the viewed time.

Related

Do we need to create separate tables for diff types of recharges such as(mobilerech,dthrech,elecbill, waterbill, etc) or can done in 1 table

I am working on a project for recharge and bill payments. I am confused about whether to use a single table for all type of recharges like mobile recharge, dtn recharge, electricity bill, water bill, card recharges, etc, which is difficult or do I create separate tables for each type of recharge and work on them.
Table has colums
recharge_id PRIMARY KEY,
recharge_amount ,
recharge_status,
recharge_time,
user_id,
payment_id
The data has to be added into the table when there is any recharge process with status and other details.
Although, you didn't show anything you tried, i think this is a viable question.
A possible approach would be to create one table for your type and one for your recharges
Something like the following should work
create a table recharge_type like
+----+------------------+--------+
| id | name | active |
+----+------------------+--------+
| 1 | Mobile recharge | 1 |
| 2 | Dtn recharge | 1 |
| 3 | electricity bill | 1 |
| 4 | water bill | 1 |
| 5 | card recharge | 1 |
+----+------------------+--------+
and your table recharge
+----+------------------+---------+------------+--------+--------+------------+
| id | recharge_type_id | user_id | payment_id | amount | status | time |
+----+------------------+---------+------------+--------+--------+------------+
| 1 | 1 | 1 | 1 | 2.00 | 1 | 2019-03-05 |
| 2 | 2 | 1 | 2 | 3.00 | 3 | 2019-03-05 |
| 3 | 2 | 1 | 2 | 4.00 | 4 | 2019-03-05 |
+----+------------------+---------+------------+--------+--------+------------+
With this type of construction you are pretty flexible for nearly any approach.
If you want to understand why this is a good approach, you should read
some articles about first normal form. You can find an article
here on Wikipedia.

Create New Column In Mysql Database Row?

I have a database called form, in it a have a few columns.
+--------+----------+-----------+
| id | title | body |
+--------+----------+-----------+
| 1 | A | 10 |
+--------+----------+-----------+
| 1 | B | 3 |
+--------+----------+-----------+
| 2 | A | 9 |
+--------+----------+-----------+
| 2 | c | 40 |
Each row has an id representing a thread number. In the database when someone comments I have a php script that will pull the id from the database of the thread the user commented on and create a new column in that row within the database. Such as below:
+--------+----------+-----------+-------------+
| id | title | body | comment01 |
+--------+----------+-----------+-------------+
| 1 | A | 10 | zyx |
+--------+----------+-----------+-------------+
| 2 | B | 3 |
+--------+----------+-----------+
| 3 | A | 9 |
+--------+----------+-----------+
This way I will be able to extract the thread along with the comments and display them onto the page.
My question is how do I write the code to insert a new column in a thread id row when someone submits a new comment?

PHP Recursive Function base on single id export excel

I have a search page that allow user to key in the member ID and it will list out all the downline that belongs to the user.
I am using easyui treegrid to generate the downline.
Now i need to do an extra button to export out all the downline that belongs to the search id and export each line of information into excel file.
This is part of my data, and actually the real data had more column and about 4000++ of data.
Is there anyone can help me or some references? Please let me know if you need more info
+-------------+---------------+---------------------------+------------+
| MemberID | parent_id | Name | Age |
+-------------+---------------+---------------------------+------------+
| 1 | 0 | Cassy | 8 |
| 2 | 1 | Peter | 7 |
| 3 | 1 | Maide | 7 |
| 4 | 1 | Samda | 7 |
| 5 | 4 | Kinso | 7 |
| 6 | 4 | March | 7 |
| 7 | 2 | Sandy | 10 |
| 8 | 0 | Mandy | 12 |
+-------+---------------+----------------------------------------------+

PHP reminders from API and local database

i am contacting an API in PHP that returns a loop of items with this information:
[domain_name] => domain.co.uk
[expiry_date] => 2016-06-18T02:27:04
i then have a table that contains columns for domain, reminder1, reminder2, reminder3
i am going to poll this URL throughout the day, every day and i want to send 'renewal reminders' for 30, 7 and 1 days before the expiry_date - each reminder will be stored in my database (reminder1, reminder2 and reminder3)
What is the best way to check if the reminders have already been sent?
I suggest you to change the database structure a bit to fit with this solution.
So for example you have the following tables:
Table domains:
+----+----------------------+
| id | domain_name |
+----+----------------------+
| 1 | domain1.com |
+----+----------------------+
| 2 | domain2.com |
+----+----------------------+
| 3 | domain3.com |
+----+----------------------+
Table domain_expiry
+----+--------------+----------------------+
| id | domain_id | expiry_date |
+----+--------------+----------------------+
| 1 | 1 | 2016-07-01 11:22:24 |
+----+--------------+----------------------+
| 2 | 1 | 2017-07-01 11:22:24 |
+----+--------------+----------------------+
| 3 | 2 | 2016-08-01 11:22:24 |
+----+--------------+----------------------+
Table reminders:
+----+-----------+---------------+------------+--------+
| id | expiry_id | reminder_type | date_sent | status |
+----+-----------+---------------+------------+--------+
| 1 | 1 | 1 | 2016-06-01 | 1 |
+----+-----------+---------------+---------------------+
| 2 | 1 | 2 | 2016-06-23 | 1 |
+----+-----------+---------------+---------------------+
| 3 | 2 | 1 | 2016-08-01 | 0 |
+----+-----------+---------------+---------------------+
Now, you should have a cron which runs daily/nightly ( say at 12:00 AM ), this cron shall do:
1- Pulls the domains and their latest expiry_date
2- Checks if the expiry_date of the domain matches the record in domain_expiry table, if matches do nothing, if doesn't match add a new records with domain_id and new expiry_date
3- Sends reminder according to domain latest expiry_date
4- Inserts the result of reminder sending process into reminders table
expiry_id,
reminder_type (1, 2, or 3),
the date when that reminder was sent, date_sent
and status (1 = success and 0 = failure)
Update
I updated my answer to allow the solution to keep history logs.

Junction tables in MySQL

(Sorry, my english isn't very good)
Hi, I am trying to learn how to work with junction tables in MySQL and I can't figure how to do something. I know the basics of MySQL but I have never worked with "JOIN".
In this test project, I would like to be able to show on a page the app of a given category (you click on "Games", only the apps that are in the "Games" category will be displayed on the page). I would like to know what the SQL request should look like.
Second question, let's say that an App could fit 2 different categories, how can I manage to give that app 2 different Category_ID in my database ?
Here is what my Database looks like at the moment :
Table name: APPS
+------------+-------------------+
| App_ID (pk)| App_Name |
+------------+-------------------+
| 1 | Weather Network |
| 2 | Is it sunny 2.0 |
| 3 | The Weather App |
| 4 | Zelda |
| 5 | Megaman |
| 6 | Doom 3 |
+------------+-------------------+
Table name : CATEGORY
+-----------------+-----------------+
| Category_ID (pk)| Category_Name |
+-----------------+-----------------+
| 1 | Games |
| 2 | Weather |
+-----------------+-----------------+
Table name : JUNCTION_APP_CATEGORY
+----------------+--------------------+
| APP_ID (pk) | Category_ID (pk) |
+----------------+--------------------+
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 4 | 1 |
| 5 | 1 |
| 6 | 1 |
+----------------+--------------------+
For your first question, the answer is
SELECT a.*, c.*
FROM APPS a, CATEGORY c, JUNCTION_APP_CATEGORY ac
WHERE a.App_ID=ac.APP_ID
AND c.Category_ID=ac.Category_ID
AND ac.Category_ID=<category_id for category "Games">
For your second question, you can use both APP_ID and Categor_ID as the primary key of table JUNCTION_APP_CATEGORY(note NOT TWO pks, but use the two columns together as ONE pk). So that you can put data like this:
+----------------+--------------------+
| APP_ID (pk) | Category_ID (pk) |
+----------------+--------------------+
| 1 | 1 | <-- APP_ID=1 belongs to both cat 1 & 2
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 4 | 1 |
| 5 | 1 |
| 6 | 1 |
+----------------+--------------------+

Categories