I'm trying to create a nested comment system using PHP and MySQL. But I'm stuck
My Database structure is id, body, time, reply and depth.
A regular comment's reply field will be '0'. If it's replying to another it will correspond to the id of the comment it's replying to.
depth means how deep it is from the highest parent
So if this is the contents of my table...
+------+-------------+--------+---------+---------+
| id | body | time | reply | depth |
+------+-------------+--------+---------+---------+
| 1 | Some msg1 | 1 | 0 | 0 |
| 2 | Some msg2 | 2 | 0 | 0 |
| 3 | aReply1 | 3 | 1 | 1 |
| 4 | aReply2 | 4 | 1 | 1 |
| 5 | aReply21 | 5 | 3 | 2 |
+------+-------------+--------+---------+---------+
It would appear as something like this...
- (1) Some msg1
-- (3) aReply1
--- (5) aReply21
-- (4) aReply2
- (2) Some msg2
I hope it's possible using this method, it kind of goes beyond my logic.
If you can't change table structure you can just get all rows (select * from table order by time) then generate tree using PHP.
For tree storage I recommend to use Nested Sets algorithm.
Related
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')
i am trying to build something like an order management tool for a car garage.
The main function of the tool is to manage repair orders and inspection orders as well
Therefore i have created this following three tables.
In the first one you can see the cars and the state of the repair and inspection.
The second one shows the information about the single repair orders the relation between the first and this is 1:n.
The last one shows the automaticly created inspection orders for each car out of the first table. This is an 1:n relation as well.
So what i try to do is to show all the open repair and inspection orders for the cars in one table. But only the open ones.
I tried it with some where statements but i got totaly confused.
My question is, how i can realise it?
+------+--------------------------+----------+------------+
| IDWZ | wz_name | wz_stand | wz_vistand |
+------+--------------------------+----------+------------+
| 1 | Querbr?cke vorn | 0 | 0 |
| 2 | Front Lateral Support | 0 | 1 |
| 3 | Rear Support | 1 | 1 |
| 4 | MID-X-Member Upper Shell | 1 | 1 |
| 5 | Front Lateral Support | 1 | 1 |
+------+--------------------------+----------+------------+
+---------+-----------------+--------------+
| IDWZTBL | rep_wzrepstatus | rep_wzfehler |
+---------+-----------------+--------------+
| 2 | 1 | REP 1 |
| 1 | 1 | REp2 |
| 1 | 1 | REp 3 MASS |
| 1 | 0 | 444 |
| 2 | 0 | |
+---------+-----------------+--------------+
+--------+-------------+
| VIWZID | vi_repstand |
+--------+-------------+
| 1 | 0 |
+--------+-------------+
Sry for that!
So the IDWZ is the foreign KEY in the second table(IDWZTBL) and in the third (VIWZID).
I tried it with
SELECT wz_name, wz_stand, wz_vistand, rep_wzrepstatus, vi_repstand FROM tbl_wz LEFT JOIN tbl_orders ON tbl_wz.IDWZ = tbl_orders.IDWZTBL LEFT JOIN tbl_vi ON tbl_wz.IDWZ = tbl_vi.VIWZID WHERE wz_stand='0' AND rep_wzrepstatus='0' ...
Only for the first table cars to the second one repair orders, that WHERE staement (WHERE wz_stand='0' AND rep_wzrepstatus='0') works fine.
But if i try to add the third table (VI) doing the same, i could fetch the result i wanna have.
What i wanna see in the Overview table is only the last open repair order and the last open inspection order.
First of all I am currently trying to learn php and I thought I would build a basic maintenance management app to better grasp everything I see in tutorials, and I hit a roadblock. I've been trying to get this to work following various tutorials online but I had no success so far, so I thought I would ask for help here. I really hope you can help me.
What I would like to know is how can I write the php & mysql to write the data from the first 3 table to the desired_table from below ?
Clients
------------------------------
client_id | client_name
------------------------------
1 | client_1
------------------------------
2 | client_2
------------------------------
3 | client_3
Equipments
------------------------------
eq_id | eq_name
------------------------------
1 | pc
------------------------------
2 | laptop
------------------------------
3 | printer
Operations
------------------------------------------------
op_id | op_desc
------------------------------------------------
1 | dust cleaning
------------------------------------------------
2 | changing processor cooling paste
------------------------------------------------
3 | cpu replacement
------------------------------------------------
4 | display replacement
------------------------------------------------
5 | dvd-rom replacement
------------------------------------------------
6 | ram replacement
------------------------------------------------
7 | cartrige replacement
Desired_table
-------------------------------------
id | client_id | eq_id | op_id |
-------------------------------------
1 | 1 | 1 | 1 |
-------------------------------------
2 | 1 | 1 | 2 |
-------------------------------------
3 | 1 | 1 | 3 |
-------------------------------------
4 | 1 | 1 | 5 |
-------------------------------------
5 | 1 | 2 | 1 |
-------------------------------------
6 | 1 | 2 | 2 |
-------------------------------------
7 | 1 | 2 | 4 |
-------------------------------------
8 | 2 | 1 | 1 |
-------------------------------------
9 | 2 | 1 | 2 |
-------------------------------------
10| 2 | 1 | 3 |
-------------------------------------
11| 2 | 1 | 5 |
-------------------------------------
12| 2 | 3 | 1 |
-------------------------------------
13| 2 | 3 | 7 |
I thought I would have a form with input fields for the client data and the equipment data and operations data I would have in dropdowns. When I would select an equipment a new dropdown would appear with the operations, and then submit it.
Hope this doesn't get marked as too broad subject :)
Using the code from your bitbucket this should do it:
$page = $db->prepare("
INSERT INTO new_table_name (column_name1, column_name2, column_name3)
VALUES (:col1, :col2, :col3)
");
$page->bindparam(':col1', $value1);
$page->bindparam(':col2', $value2);
$page->bindparam(':col3', $value3);
$page->execute();
if($page->rowcount() > 0){
echo "Inserted a new row";
}
Here is a example, replace 'new_table_name' to the name off the new table and change the column names.
After that simiply change $value1, 2 and 3 to the variables that contain the value you want to insert and you're good to go.
If you want to know if the insert was succesful just check with $page->rowcount(). If the rowcount is more than 0 it was succesful.
And just to clarify, this is called a prepared statement if you don't know this yet. What the code does is this:
Prepare tells the server what you're planning to do. This adds a really strong layer off protection since basic sql injection are close to impossible to do.
After that you bind the variables to the given parameters (Other syntax is possible, I just prefer this one). The code takes care off quotations so just put the parameter in there.
Execute the actual query
And just for good measure, you can add 2 extra parameters to bindparam.
In the third you can define a variable type, PDO::PARAM_STR and PDO::PARAM_INT block any other type off variable.
The 4th lets you set a max length, if the variable is longer it will get blocked.
This will look like this:
$page->bindparam(':col1', $value1, PDO::PARAM_STR, 100);
//block anything which isn't a string or longer than 100 characters
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
i have a table in following format:
id | title
---+----------------------------
1 | php jobs, usa
3 | usa, php, jobs
4 | ca, mysql developer
5 | developer
i want to get the most popular keywords in title field, please guide.
If you have a list of keywords, you can do the following:
select kw.keyword, count(*)
from t cross join
keywords kw
on concat(', ', t.title, ',') like concat(', ', kw.keyword, ',')
As others have mentioned, though, you have a non-relational database design. The keywords in the title should be stored in separate rows, rather than as a comma separated list.
If your data is small (a few hundred thousand rows or less), you can put it into Excel, use the text-to-columns function, rearrange the keywords, and create a new, better table in the database.
SELECT title 1, COUNT(*) FROM table GROUP BY title 1
EDIT
Since you've edited and presented a non-normalized table, I would recommend you normalize it.
Have a read of: http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/
You need to modify your database. You should have something like this:
items
+----+---------------+
| id | title |
+----+---------------+
| 1 | something |
| 3 | another thing |
| 4 | yet another |
| 5 | one last one |
+----+---------------+
keywords
+----+-----------------+
| id | keyword |
+----+-----------------+
| 1 | php jobs |
| 2 | usa |
| 3 | php |
| 4 | jobs |
| 5 | ca |
| 6 | mysql developer |
| 7 | developer |
+----+-----------------+
items_to_keywords
+---------+------------+
| item_id | keyword_id |
+---------+------------+
| 1 | 1 |
| 1 | 2 |
| 3 | 2 |
| 3 | 3 |
| 3 | 4 |
| 4 | 5 |
| 4 | 6 |
| 5 | 7 |
+---------+------------+
Do you see the advantage? The ability to make relations is what you should be leveraging here.