Create New Column In Mysql Database Row? - php

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?

Related

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 |
+-------+---------------+----------------------------------------------+

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.

Sql Select data pick id from string

Category Table
+----+-----------------------+
| id | category_name |
+----+-----------------------+
| 1 | Buy Book |
| 2 | Buy other thinks |
+----+-----------------------+
Buy Table
+----+-----------------------+----------+-------------+----------+--------+-------+
| id | identity | name | description | per_rate | bought | costs |
+----+-----------------------+----------+-------------+----------+--------+-------+
| 1 | PROJECT[1]CATEGORY[1] | BOOK | PHP BOOK | 10 | 50 | 5000 |
| 2 | PROJECT[1]CATEGORY[1] | BOOK | PHP BOOK | 10 | 40 | 4000 |
| 3 | PROJECT[2]CATEGORY[1] | BOOK | JS BOOK | 2 | 50 | 100 |
+----+-----------------------+----------+-------------+----------+--------+-------+
I Want to Select category name from Other table when I select this table.
identity: PROJECT[project_id]CATEGORY[category_id]
So There are any way to pick the category id and select category name from other table
I Want Like This Table
+----+---------------+-----------------------+----------+-------------+----------+--------+-------+
| id | category_name | identity | name | description | per_rate | bought | costs |
+----+---------------+-----------------------+----------+-------------+----------+--------+-------+
| 1 | Buy Book | PROJECT[1]CATEGORY[1] | BOOK | PHP BOOK | 10 | 50 | 5000 |
| 2 | Buy Book | PROJECT[1]CATEGORY[1] | BOOK | PHP BOOK | 10 | 40 | 4000 |
| 3 | Buy Book | PROJECT[2]CATEGORY[1] | BOOK | JS BOOK | 2 | 50 | 100 |
+----+---------------+-----------------------+----------+-------------+----------+--------+-------+
You have a really bad data structure. The project and category should be in their own columns, with numbers stored properly as numbers, and proper foreign key relationships. In MySQL, doing this might require a trigger, but it is worth it.
Sometimes, we are stuck with other people's bad decisions. You can do what you want using like:
select b.*, c.category_name
from buy b join
category c
on b.identity like concat('%CATEGORY[', c.id, ']');
However, you should probably put effort into fixing the broken data structure.

update mysql records with mapping tables in multiple rows

I have create a simple blog system, the following three tables in my mysql database.
when the author update the article the new tag names are passed dynamically. for example the tags is not predefined. author create new tag when the article written i have completed successfully, but update the article the new tags and existing tags i want update appropriate articles in article table, tag is new then create new tag in tag table and mapping update with tag_hook table
Table 1: article
-----------------------------------
| article_id | title | desc |
-----------------------------------
| 1 | php mysql | lorem |
-----------------------------------
| 2 | java oops | lorem |
-----------------------------------
Table 2: tag
--------------------------
| tag_id | tag_name |
--------------------------
| 1 | sql |
--------------------------
| 2 | java |
--------------------------
| 3 | python |
--------------------------
| 4 | ruby |
--------------------------
Tbale 3: tag_hook
-----------------------------------------------
| hook_id | tag_id | article_id | type |
-----------------------------------------------
| 1 | 1 | 1 | article |
-----------------------------------------------
| 2 | 1 | 2 | article |
-----------------------------------------------
| 3 | 4 | 2 | article |
-----------------------------------------------
| 4 | 4 | 1 | article |
-----------------------------------------------
If i want to update article 1 in tag_hook table add two new tags (2,3 tag_id)
Help me thanks

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