
I'm building a HN clone in PHP, and I need help - wocp
Hi, well as the title says, I&#x27;m programming a HN clone. I choose PHP because is more simple to find a hosting which provides us PHP.<p>And I have some questions, the first is about HN comments, in its structure a comment is an item. I&#x27;m using MySQL, and my table &#x27;items&#x27; can store comments too. My question is: HN has a limit deep in comments? Because is complicate get comments using recursivity.<p>That&#x27;s the reason because I prefer use SQL and a limit of comments.<p>I have no time to finish quickly this project, but I&#x27;ll try to do it fast.<p>There are lots of broken links, only works the login&#x2F;signup and an item views.<p>URL: http:&#x2F;&#x2F;news.sourcezilla.com<p>Thanks in advance.
======
krapp
I am too.

I've found a few methods for rendering trees from SQL, and I find that Stack
Overflow is your friend here. The method that seems to work best to me is
closure tables - although it's complex, you'll find implementations available.
Essentially you need two tables, one for items (in my implementation, which is
not online), this is currently only storing name, date, comment, etc -
essentially, find the depth of the parent and ++) and another table separately
to handle the relationships and build the tree.

If you want to limit comment depth, one way to do that is to keep track of the
depth of comments by incrementing a value for each comment (the root comment
has depth 0, replies have depth 1, replies to replies have depth 2, etc.)
Although this method may lead to anomalies in the data.

You shouldn't need extremely complex recursion, however, as far as I know,
apart from self-joins in the database itself. Before using closure tables, I
was using a basic method where every item stored its own parent, and a
reference to the root post, and this worked except getting an entire tree took
multiple queries. (and to be fair, I am not exactly a rocket scientist when it
comes to SQL)

Also - ignore whoever downthread tries to criticize you for using PHP. This is
about as basic a CRUD app as you can make, as long as you're not doing
anything crazy like using the mysql_* functions and concatenating strings,
then it mostly shouldn't matter.

------
company
You need to do self joins. You can have one comments table but there can be
parent_id for each comment.

[http://stackoverflow.com/questions/3362038/what-is-self-
join...](http://stackoverflow.com/questions/3362038/what-is-self-join-and-
when-would-you-use-it) this might help.

~~~
wocp
Yes, my table structure has a parent_id field. The problem is that I need know
the depth, or use recursivity to get all parents.

Using hierarchy and recursivity like this:
[http://stackoverflow.com/questions/4216875/php-reorder-
array...](http://stackoverflow.com/questions/4216875/php-reorder-array-to-
reflect-parent-id-hierarchy)

~~~
company
[http://www.jongales.com/blog/2009/01/27/php-class-for-
thread...](http://www.jongales.com/blog/2009/01/27/php-class-for-threaded-
comments/)

see if that helps

~~~
wocp
I'm going to check out. Thanks!

------
dosh
Just out of curiosity, is hacker news built in similar fashion to reddit?
Considering reddit is a YC invested company. reddit is open source so...

~~~
wocp
Hi dosh, I don't think that HN is build or is inspired in Reddit code/design.
Both of them are open source, but Reddit is build in Python and HN in Lisp.

------
ijovanovic
I'm up for it. Shoot me an email.

~~~
wocp
What's your email? :-)

------
vithlani
PHP...Yes, you need serious help.

~~~
wocp
I did choose PHP because lots of servers support the language, so more people
will can use the script.

