PHP reminders from API and local database - php

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.

Related

Fetch results in group based on the occurrence

Ok, I have a single MySQL table with the name 'car' and 3 columns.
+----+--------+------------+
| ID | car_id | engine |
+----+--------+------------+
| 1 | 123 | on |
| 2 | 123 | on |
| 3 | 123 | off |
| 4 | 123 | on |
| 5 | 123 | on |
| 6 | 123 | on |
| 7 | 123 | off |
| 8 | 123 | on |
| 9 | 123 | off |
+----+--------+------------+
Now I want to show the trips this car did. The trips would be determined based on car engine start and stop. For example from the above example we can see that user has made 3 trips as total(From on to off). Now What I want is that if there is a query which gives me only 3 results from on to off meaning if somehow the query groups the records by considering a starting point on and ending point off. Is it possible in mysql? or the other way around is doing it manually by fetching all the records and working in arrays?
At the moment I am fetching all the records and doing it manually by looping all the data and doing accordingly But this process is slow.
Can you try it ?
SELECT * from cars WHERE `engine` = 'off' AND id IN(SELECT id+1 FROM `cars` WHERE `engine` = 'on')

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.

How to best structure a database while keeping track of dataset changes

I've got a small CRM and I'm trying to figure out the best way to designing the DB tables.
I've currently got a single table for users that got around 30 columns which I alter from time to time. Since I am storing two different information on that table (user + company information) I was thinking of splitting that table into 3 (user + company + connection between these 2) but I am also interested in keeping a copy of any changes that are being made in these rows.
So going from:
user_id | firstname | last_name | company_name | company_city | company_subject | rank | status
1 | John | Borrows | Boink INC | NY | Web dev | 1 | 1
2 | Mike | Smith | Smithin INC | OC | Laywer | 1 | 2
3 | Mary | Anton | Caffin | SJ | Moving | 2 | 1
to something like this
user_id | firstname | last_name | rank | status
1 | John | Borrows | 1 | 1
2 | Mike | Smith | 1 | 2
3 | Mary | Anton | 2 | 1
comp_id | company_name | company_city | company_subject
1 | Boink INC | NY | Web dev
2 | Smithin INC | OC | Laywer
3 | Caffin | SJ | Moving
con_id | user_id | comp_id
1 | 1 | 1
2 | 2 | 2
3 | 3 | 3
But I'm not sure how to track the changes when for example a user changes the company name or some other info on user's table etc.
Just follow the normalization rules for structuring your database tables. You will find anything you need for that by just searching for database normalization.
Regarding your "update-history" you could add a Timestamp to your datasets and/or a separate boolean field "outdated" to be able to filter out the latest information.
Would be the simplest solution that comes into my mind.

rearraging converstaions with lastest on top in php + mysql

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.

Assigned multiple users to a 'task'

Okay so I'm creating a task manager for my company. A user can assign assign a task to multiple other users. So I've though of 2 ways of implementing this.
This is my tasks table for option one (at least the columns that are important in this discussion ):
----------------------------------------------
| id | assigned_to | assigned_from |
---------------------------------------------
| 1 | 1,3,6 | 4 |
--------------------------------------------
| 2 | 1,4 | 2 |
---------------------------------------------
So here I pretty much just comma separate each user_id that is assigned to this particular task
Option 2:
----------------------------------------------------------
| id | task_id | assigned_to | assigned_from |
------------------------------------------------------------
| 1 | 335901 | 1 | 4 |
-----------------------------------------------------------
| 2 | 335901 | 3 | 4 |
-----------------------------------------------------------
| 3 | 335901 | 6 | 4 |
-----------------------------------------------------------
| 4 | 564520 | 1 | 2 |
-----------------------------------------------------------
| 4 | 564520 | 4 | 2 |
-----------------------------------------------------------
So as you can see here instead of putting the assiged_to is's here I just create a task id which is a random number and then I can groupBy 'task_id'. This is currently they way I have built it but for some reason it feels like it might screw me over in the future (not that option one doesn't give me the same feeling). So my question is which way do you guys recommend or is there maybe a different better way that I could be doing this?
Option 2 ist the better solution since you can acutally work with the table. You may e.g. create another table Tasks with
Task_id | Task_name | Budget | ...
Or a table with user-IDs for assigned_to and assigned_from. All these tables can be joined together if you use 2nd Option.
btw it is the correct normalization form
You can use Option 2 and normalize further if tasks are always assigned by/from the same person.
Tasks table:
task_id | assigned_from
1 | 4
2 | 2
The Assignees table then doesn't need to have the assigned_from since it's always the same for that task_id:
id | task_id | assigned_to
1 | 1 | 1
2 | 1 | 3
3 | 1 | 6
4 | 2 | 1
5 | 2 | 4

Categories