How to assign comment id? - php

I am making comment system and now I want to insert comments to database and I am confusing that on what basis to assign specific comment_id.
Suppose we have multiple dives of images with comment system.if someone comment on image then how can we assign a specific comment id on that specific image.and if other user comment on same image then how can he found that image comment_id so the comment save in right direction.
we have many images and comment system for that image.
My english is bad may be you understand what i want to say.

You need to create a number of different tables in the database
table: comments
|comment_id | userID | name | comment | (for example)
1 50 James test
2 50 James test
3 50 James test
table: images
|image_id | link |
1 example.com/images/image1.png
2 example.com/images/image2.png
3 example.com/images/image3.png
table: comments_on_images (to make the table's purpose clear)
|id | comment_id | image_id
1 1 2
2 2 2
3 3 1
Using this method you can assosciate any number of comments to any images. You have to query the database using JOINS to get all the information you need.

Related

Show Others Images if Available Places

I want to show others image based on limit.
Table group images
| id | Name | Max places |
1 Static 3
2 Rotate 2
Table images
| id | group_id | url |
1 1 https://ima.ges.png
2 2 https://ima.ges.png
3 2 https://ima.ges.png
I want to show images in my template based on Max available places on the groups. Let say I have 1 images to show in group static, then the groups will still have 2 more slot then show others images.
In conclusion, it still show 3 images in template.
1. images from https://ima.ges.png
2. images from default value
3. images from default value
The question is, how to do this based on mysql data?
thanks for helping

insert reference in comment table that joins two sessions (blog and news)

I am finalizing a comments system and was with a doubt.
I have a table for blogs and one for news, and they accept comments.
My comments table receives the text and the id.
I wonder if I need to (or should I) go through some sort of reference to know where the comment comes from.
table comment
id | id_content | text | ref
1 | 1 | test | blog
2 | 1 | test | news
thanks
depending on the number of comments you expect to receive there are two ways of doing this ...
1 - parent_tbl, parent_id - in one big comment table
2 - two tables for comments with a parent_id - one for each primary table
either way you need to index properly, the second will always work faster, but it doesn't expand well if you say add "press_releases" now you have to duplicate code, tables, what not.

Inserting and searching for multiple keywords

I am planning designing a PHP MySQL database for a local museum's photo gallery.
One thing that terrifies me now is how to implement saving multiple keywords for a single image for items where more than one technic or material was used to design them, e.g.
PAINTING TECHNICS:(liner, wiping, scumbling), MATERIAL USED: (oil, canvas,).
Ideally, I would have to use the select option lists to save them for each record. But if I want to save two or more of the words from the same list, I cant.
Do you have any suggestions I might consider?
joseph
You will need to implement a one-to-many relationship between your image table and your keyword table. One image can have many keywords. Each row in your keyword table will need to reference a row in the image table through a foreign key. For example:
images
id | asset
---------------
1 | image1.png
2 | image2.png
keywords
id | image_id | keyword | category
-----------------------------------------------
1 | 1 | liner | painting techniques
2 | 1 | wiping | painting techniques
3 | 1 | scumbling | painting techniques
3 | 2 | oil | material used
Or something along those lines. This is how you allow one row (an image) to be associated with many rows (keywords) in a different table.
For each keyword submitted, you would add another row to the keywords table, making sure to reference the associated image.

How do I save related data in CakePHP?

I have a 'websites' and 'products' table. The a product can be attached to different websites. So I created a new table called 'attached_products'. It has the fields id, website_id and product_id.
So I have two questions for now, the first one is how do I save a website that will appear something like this in the attached_products table:
id | website_id | product_id
1 | 4 | 3
2 | 4 | 5
3 | 4 | 6
4 | 4 | 10
the above means four products with ids 3,5,6,10 were attached to a website with an id of 4
my second question is, how do I modify the record for example I want to remove the two products attached to a website?
I'm new to CakePHP, many thanks for any help! :)
You have the right idea about what you're doing. Read about HABTM relationship in Cake book. You should learn more about Cake (from simple to complicated things). That one is pretty hard for a newcomer.

How do I make replies to comments? (PHP)

I want to create something like reddit where they have comments, then replies to the comment, then reply to the reply.
What type of database structure do they use so:
1. they keep track of all the comments to a posting
2. a reply to a comment
3. a reply to a reply
All I have right are is just a posting and a bunch of comments relating to it like..
POSTING TABLE
posting_id | title | author
COMMENTS TABLE
comment_id | posting_id | comment
REPLIES TABLE
????
How do I relate the comments to the replies?
What type of css do they use to give replies that indented space?
EDIT:
Thanks for the answers! Now my only question how do I indent the replies?
Such as..
you like food
yes I love italian
Yes i do like it too
chinese is best
You can add another column to your comments table specifying parent_comment_id where you populate it with the ID of the comment (or reply) the user is replying to. In the case where the comment is a direct reply to the post (not a reply to a comment) this column would be null.
To show replies inside replies, you'll have to do a recursive call to keep on generating the sub replies.
Something like
function get_comments($comment_id) {
print '<div class="comment_body">';
// print comment body or something?
if (comment_has_reply($comment_id)) {
foreach(comment_comments($comment_id) as $comment) {
get_comments($comment->id);
}
}
print '</div>';
}
To indent comments however, use css.
<style type="text/css">
.comment_body {
margin-left:10px;
}
</style>
This way sub replies are indented more than the parent, and their subs are indented even more, and so on.
I would do that by making a cross reference table.
Example:
Table: Posts
Columns: pstkey | userid | postMessage | etc...
pstkey is the key for the post body. userid is the person who created the post. postMessage is the actual post entry.
Table: Comments
Columns: comkey | pstkey | userid | commentMessage | etc...
comkey is the key for the comment made. referenced to the post using the pstkey. userid is the person who made the comment. and then commentMessage is the text body of the actual comment.
Table: xref_postComm
Columns: xrefkey | pstkey | comkey | comkey2 |
Now for the fun part. ALL posts go into post table. ALL comments go into comment table. The relationships are all defined in the Cross Reference Table.
I do all of my programming this way. I was privileged to work with one of the worlds bests database engineers who was retired and he taught me a few tricks.
How to use the Cross Reference table:
xrefkey | pstkey | comkey | comkey2
All that you look for is the population of a given field.
xref (Auto Incremented)
pstkey (Contains the pstkey for the post)
comkey (Contains the comkey for the comment post)
comkey2 (Contains the comkey for the comment post)
(but only populate comkey2 if comkey already has a value)
and of course you populate comkey2 with the key of the comment.
SEE, no reason for a 3rd tabel!
With this method you can add as many relationships as you want.
Now or in the future!
comkey2 is your reply to a reply. Where which this single row contains.... the key of the post, the key of the comment, and the key of the reply to the reply comment. All done by population of xref.
EXAMPLE:
PAGES.... Page table
POSTS
pstkey | pageid | user| Post
-------------------------------------
| 1 | 1 | 45 | Went to the store the....|
| 2 | 2 | 18 | Saw an apple on tv.....
COMMENTS
comkey | pstkey | user | Comment
-----------------------------------------------
| 1 | 1 | 9 | Wanted to say thanks...
| 2 | 1 | 7 | Cool I like tha.....
| 3 | 2 | 3 | Great seeing ya....
| 4 | 2 | 6 | Had a great....
| 5 | 2 | 2 | Don't sweat it man...
xref_PostCom
xrefkey | pageid | pstkey | comkey | comkey2 |
----------------------------------------------
| 1 | 1 | 1 | NULL | NULL | Post1 on Page1
| 2 | 1 | 1 | 1 | NULL | Comment1 under Post1
| 3 | 1 | 1 | 2 | NULL | Comment2 under Post1
| 4 | 2 | 2 | NULL | NULL | Post2 on Page2
| 5 | 2 | 2 | 3 | NULL | Comment3 under Post2 on Page2
| 6 | 2 | 2 | 4 | NULL | Comment4 under Post2 on Page2 (a second Comment)
| 7 | 2 | 2 | 4 | 5 | Explained below....
Comment key 5 is matched with comment key 4....under post2 on Page 2
If you know anything about join, left join, right join, inner/outer join creating SELECT's to get the data arrays using these relationships, your job becomes a whole lot easier.
I believe the engineer's call this basically "the data map" of defined relationships. The trick is now how you access them using these relationships. It seams hard at first, but know what I know it, I refuse to do it any other way.
What happens in the end is you end up writing 1 script that says, ok, go do uhh, everything and come back. You will end up with 1 function call that asks for page 1. It returns with page1, post 1, comment1&2&3 and the replies to the reply in 1 array. echo to output and done.
UPDATE FOR COMMENT
I said the same exact thing the first time it was shown to me. As a matter of fact it really was making me mad that the database programmer was forcing me to do it this way. But now I get it. The advantages are so many.
Advantage 1) 1 query can be written to pull it all out in 1 shot.
2) Answers in multiple queries can populate arrays in a structure that when printing the page a loop in a loop can display the page.
3) Upgrading your software that uses it can support any possible design change you can ever think of. Flawless expandability.
The guy who taught it to me was the hired gun who redesigned sears and jcpenny databases. Back when they has 9 books going to the same house because of duplicate records issues.
Cross reference tables prevent a lot of issues in design.
The heart to this theory is, a column can not only contain data but serve as a true or false statement at the same time. That in it's self saves space. Why search 20 tables when you can search one? 1 indexed cross reference table can tell you everything you need to know about the other 20 tables, it contents, what you need, what you don't need, and do you even need to open the other table at all.
IN SHORT:
1 Cross reference containing nothing but INT(2/11) that tells you everything thing you need to know before you ever open another table, not only contains flawless expandability but lighting speed results. Not to mention little possibility of duplicate records. To you and me duplicate records may not be an issue. But to Sears with 4 billion records at $11 a book, mistakes add up.
Add another field to your comments table which "reply_to" or some such, and store the id of the comment which it is in reply to there.
you could make the comments table generic like so :
COMMENTS TABLE
comment_id | posting_type | posting_id | comment
where posting_type is some sort of discriminator, eg a string 'POST' or 'COMMENT', or an integer for more efficiency (1 = POST, 2 = COMMENT, etc).
edit : admittedly this is more complicated but it means you can use the same comment table for comments on anything, not just posts and other comments.
You don't need the replies table. As others already correctly have pointed out, recursion is the way to go with an RDBMS. You could always consider using a nosql style DBMS, to avoid having to deal with recursion.

Categories