Static page numbering?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
QuickSnail
Forum Commoner
Posts: 46
Joined: Fri Dec 21, 2007 11:13 am

Static page numbering?

Post by QuickSnail »

Ok I can't talk completely about the project because it is still under wrap.

But. I do need a little help so here we go.
Here is the info you need to know to help..

There is a user, the user has friends the ID's of his friends are stored in mysql database under a colum 'Friends' stored as '1, 2, 3, 4,' etc.
Php uses strtok() to grab each friendID how would I create a page to see how many friends there are then display them every 50 friends or so.

I know I can use strtok() to split the up every ', ' comma and space but setting it up so php prints the amount of strtok()'s I need and sets it up per page.

Right now I would only like say the numbers to be displayed.


If strtok() is not the right way to approach this please let me know how then.
Oh and a tip. I suck with object oriented.

All help is appreciated :drunk:
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Static page numbering?

Post by Benjamin »

You're going to want to store each user_id, friend_user_id entry in a separate row. Placing them all into a single row is going to be nothing but trouble when you need to start accessing the data from different angles.

Once you have done this, pagination will be quite easy. You can order them by date or the order added.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Static page numbering?

Post by califdon »

Putting it another way, you are violating the First Normal Form of relational database theory, which states that every column (field) must be "atomic", that is, it cannot be separated into smaller pieces of data. What astions described is the proper structure for this one-to-many relationship.
QuickSnail
Forum Commoner
Posts: 46
Joined: Fri Dec 21, 2007 11:13 am

Re: Static page numbering?

Post by QuickSnail »

Mmm. I read that. I have been studying like crazy for the last 7-9 months. I had to many questions so I went out and bought tons of books .. Spent more on books than the site. Shesh. But anyway Thats another story.

Alright The project hasn't been launched Yet so all is changeable.

How would I go by doing this?
Alright Here I will make a simple table diagram to show what I have now just on this feature.

+----+----------------+----------+----------+
| ID | Top_friends | friends | blocked |
+----+----------------+----------+----------+

ID is int not null primary key and auto_increment,
Top_friends is varchar(250),
friends is varchar(50,000),
Blocked is varchar(1,000).

I can't make 50 million rows, one for each friend.


I guess my question is what would be the ideal way to set up the database?
In Colums? ID, Friend_1, Friend_2, Friend_3, etc...

Wouldn't that be a little irrational?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Static page numbering?

Post by califdon »

QuickSnail wrote:+----+----------------+----------+----------+
| ID | Top_friends | friends | blocked |
+----+----------------+----------+----------+

I can't make 50 million rows, one for each friend.
Only if you will have 50 million friends. :P Seriously, your new one-to-many table will indeed have a lot of rows, but that's precisely how relational databases are built to handle this.
I guess my question is what would be the ideal way to set up the database?
In Colums? ID, Friend_1, Friend_2, Friend_3, etc...

Wouldn't that be a little irrational?
Yes, that certainly would, and it would break another rule of normalization.

So here's what you need:

Code: Select all

[b]tblUsers[/b]:
    UserID
    UserName
    ... other User fields ...
 
[b]tblFriends[/b]:
    UserID
    FriendID
    Blocked
The data in these tables would look like this:

Code: Select all

 
[b]tblUsers[/b]:
1001  BeetleSnap  2008-01-09
1002  Whimsey     2008-02-12
1003  Blechk      2008-02-22
1004  Grrrrrrrr   2008-03-15
 ...
 
[b]tblFriends[/b]:
1001  1003  0    ( = BeetleSnap declares Blechk a friend, and isn't blocking him)
1003  1001  0    ( = likewise, Blechk declares BeetleSnap a friend)
1001  1002  0    ( = BeetleSnap declares Whimsey a friend)
1004  1003  0    ( = Grrrrrrr declares Blechk a friend)
1003  1004  1    ( = Blechk blocks Grrrrrrr)
 ...
You might want to read up on some relational database theory, like:
http://www.phlonx.com/resources/nf3/
http://www.surfermall.com/relational/lesson_1.htm
http://www.databasedev.co.uk/database_n ... asics.html
http://support.microsoft.com/kb/100139
QuickSnail
Forum Commoner
Posts: 46
Joined: Fri Dec 21, 2007 11:13 am

Re: Static page numbering?

Post by QuickSnail »

Ahhhh. I see what you mean.
Ok.
You have each user define the other as a friend and/or if Blocked.

I get that. Thats easy. But the main problem is users are going to have more than one friend.
bob defines john as a friend
and
john defines bob as a friend
but
what about steve?
You can't leave steve out!
How would
John define steve as a friend
and
steve define john as a friend.



1001 = bob 1002 = john 1003 = steve

1001 1002 0
1002 1001 0
1003 1002 0


Now that defines john and bob friends and steve a friend of john but not john a friend of steve.
How would I define multiple friends with out having the same userID appear multiple times? speaking ID is auto_increment.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Static page numbering?

Post by califdon »

I actually made examples of that in my previous email. Look again. BeetleSnap has 2 friends, Blechk and Whimsey. If a user has 25 friends, there will be 25 records in tblFriends with that user's ID in the first column. Obviously it can't be an auto-increment field in that table; it wouldn't work if it was! It's just an integer that matches the size of the auto-increment field in tblUsers. This is the standard PrimaryKey/ForeignKey relationship of every relational table. You would probably want to declare the PrimaryKey of tblFriends as the combination of UserID and FriendID, which has the added benefit that it won't allow duplicate records for the same UserID and FriendID.

Don't be put off by the number of records this requires in tblFriends. Relational database engines are designed just for this. Until you get many millions of records, MySQL will handle them with ease.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Static page numbering?

Post by Benjamin »

50 million rows would cause the table to be about 650mb with an index, which MySQL can handle quite easily. I'm pretty certain that you won't end up with near that many rows anyway.
QuickSnail
Forum Commoner
Posts: 46
Joined: Fri Dec 21, 2007 11:13 am

Re: Static page numbering?

Post by QuickSnail »

Ah I see where my eyes skipped over now. Ok. Alright. I will take this in to consideration. Thank you.
The php code set up for that will be Simple.

Thanks again you guys are ok in my book. If I need any more help I will know where to go. :D
Post Reply