How to Create a Social Network Using PHP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and they're down here and in this video I'm going to show you how to create a basic social network or give you the concept behind some of the major elements that go into a social network so just to kind of kind of wrap this up and give you the overview of it to start of course there's lots of things you can do with social networks there's all kinds of different social networks MySpace Facebook Google+ even Twitter because someone considered a social network and of course plenty of others that are out there so there's lots of different ways that you can put them together but there's some key elements that most social networks have that we're going to cover and we're going to talk about so a couple things that you'll notice that aren't on here that I want to cover right off the bat is that when a lot of people think about social network one of the things they think about our groups and they think about forums and now in that maybe you and that was me for a long time but now it kind of it's for me it's when I hear people talk about forms and groups as a part of social networks it kind of I don't really buy it because it it is a little bit irritating to me I guess because if you look at the social network landscape MySpace when it first came out had forums and groups and I think that's where it kind of all comes from but if you look most social networks really don't have the forms and the groups anymore they have somewhat versions of that but it's not the same as what a group was in myspace with the discussion forum and so on Facebook has lists but that's more to be like Google+ Circles than it would be myspace groups forums really don't see those much anymore in fact I'm pretty sure Facebook got rid of most of its discussion forms here lately Google+ doesn't really have them they have hangouts but that's not really that for him so anyway we're not going to cover forms and groups here because it's really not a part of the current social network landscape okay plus there's a lot of form scripts and stuff out there if you really want to look at it but we're going to cover what are the nuts and bolts and as you go through this I think you're going to see why we cover exactly what we cover because you have to get to a certain point where all of the social stuff you're doing becomes useful for your the people that are a member of your social network and we're going to get to the point where to that point where it starts to become useful and all the things that we going to we do up to that point are just really there to start making it useful and then when you get the point where it becomes useful that's where you can really start to build a valuable social network and you know you can kind of tweak it whatever way that you want to tweak it in terms of of you know if you want to be more like Facebook or Google+ or Twitter or whatever the case may be so alright so let's get through all that let's go ahead and dive in so you can see here I'm on this is really basic homepage again I haven't done a ton with styling here simply because if I go through all the styling and do all that stuff then it's going to take this videos going to be a heck of a lot longer so trying to keep everything concise and give you really the PHP parts that you need to know okay so here we have our hug ugly homepage here you can see that we have just a number of things going on here so these are the things that we're going to cover now of course this menu up here you would make this a lot prettier than this but I left it like this so you can see the exact things that we're going to cover so in this video these are this is everything we're going to cover and it's really the same links down here as well so we're going to have of course our home page and we're going to have viewing our profile editing our profile we're going to have a members directory we're going to have a friends list we're going to have a feed we're going to be able to post statuses and we're gonna have inboxes an inbox so you can send and receive messages okay and if we come back to our database over here then you can see we have a number of tables back here we have our s friends for social again we're in our social database so we're just saying s friends and then we have our s messages we have our s status and we have our s users okay so we'll talk a little bit about the database structure here in just a minute but I want to cover it just kind of exactly how this works so what this is assuming at this point then this is I guess another thing to kind of preface this is that you are a logged in user okay so we talked about in the login script system how you would create this elaborate login system to be able to verify members and have them logged in and then be able to do something with that well one of the things that you could do is build CMS one of the things you could do is build a social network if you remember from the login video remember that we had a user object essentially we had once we got the the user all logged in we had their information that we could store as an object and we can access that information if we wanted to and do different things with it now in the form of a CMS you might use that for if they have permissions to write posts or edit post kind of like WordPress does with its roles where you know you have an administrator and editor and author subscriber or whatever then you could use your user information to do that but in that system the Steyr system is more focused on content creation here in a social network we're going to be focusing more on that user object and the the user information that we're going to collect and we're going to do some things to make that information useful so we're assuming that we're a logged in user and let me go ahead and just come over to this code real quick I'm just going to go into one of these pages so again you remember from the login video we had our our protected page and we had essentially a logged in user we added four Meishan about them in this instance I'm just simulating that by setting the logged in user ID to an ID of one okay because essentially that's what we were getting in the the the login tutorial we were getting a little bit other information but the big thing that we had is we had the you the ID of the logged in user that's what we need when we have that when we have their ID we can access all the information that we have about them so I'm essentially simulating that by having this logged in user ID now you could very simply take this and you could integrate it with the login system and use that user object you get in the login system and really do the same thing that we're going to do here okay so that's what we're assuming that's how we're creating that simulation here I've done that because one I want to keep this as simple as possible you'll notice there's some other things I did that we'll cover as we go through to keep this as simple as possible I did want to over code it for the tutorial meaning I have to go through you know 17 steps to teach you one that's the important one so I kind of dumb this down a little bit just so it's a little bit easier to go through and explain it's really just for me but again you could take this you could really make this a lot fancier you could theme this out and you there's a lot you could do with this but this is going to give you the basic concept so if we come back over there site where a logged in user so when we click on view profile then we're going to be viewing our profile so if we click on View profile then you're going to see we are on that view profile page we have our name and we have our email address now I'm just putting in some basic information here again trying not to over code this but this information could be absolutely anything so you can actually create the fields that you want to collect data for and then you could collect all the data you want to collect so if your Facebook you collect your name address email you let them upload a picture their interests so on and so forth there's all these different thing the data that you can collect you can collect that data too and you would collect it in the exact same way that you collect this information so you'd have a form which is obviously on the edit profile page and on the registration page when they register your collect a certain amount of information and then you'll have an edit profile page where they can add more information to their profile and ultimately what a social network really is is a system that's trying to get people to fill out more and more information about themselves to make friends and let the system know who their friends are to identify their interest through liking and sharing and commenting ultimately about getting as much information about an individual user as possible to build a essentially an interest profile and a social profile or an interest graph in a social graph the reason they want to do that is because then they turn around they advertise those people based off that and it allows them to make the advertising a lot more effective if you have ever advertised on Facebook which I've done some on it's actually works really really well and the reason it works so well is because you can hyper target you can hyper target who you advertise to and so that's what really a social network is designed to do so again this information could be whatever information you want to collect so again you can you know set that up how you want add as many fields as you want have clicked whatever information you want as long as you feel like you can get your users to fill it out and it's going to be important for you for your goal of your social network then you know by all means you can put whatever you want here so that's view profile if we go to edit profile again of course we're editing our profile and so you can see the information that was on the previous page is now here and of course we have our password as well so if I want to change my password I could do that here so if I come here let's just show you if I just delete so it's John Moore now and I submit that you can see now this is saved to John Moore and if I go to view profile you can see it's John Moore here and come back to edit profile and we'll put this back and submit and now it's back to John Morris okay so again these would be all of the fields of information that you have of course when they register you'll have certain information you collect right off the bat like you know their name their email their password that's usually so I think Facebook might collect well maybe a little bit more than that but I'm not sure but anyway at least not right now but um whatever information you want to collect so this is all how you collect information about them okay next we have our members directory so this is just a directory of all of the members on the site if I click through to one then I go to the view profile page except you'll notice that I'm passing in a user ID here and it's giving me that user ID so here's how this works by go click on John Doe I'm getting user ID too and now I have John does information John Doe at fake comm so I have it if I pass a user ID this view profile page is going to use that user ID if not it's just going to take me to mine okay so we'll talk about how that stood up here in a little bit but this is essentially just a members directory of all the different members have a creative three just for our purposes we have a friends list so this is going to be my friends list so you can see I'm friends with John Doe here and of course remember Jane Doe is a part of the site but she's not on my friends list because I haven't added her to it as a friend if I come over to Jane Doe you can see I have a button that says add his friend if I click add his friend now it changes to remove friend if I go to friends list now she's on my friends list I go back go Jane Doe remove friend come back to my friends list then you could see she's gone again okay so allows me to create a friends list next we have our feed now our feed is based off of our friends list so just like in Facebook you only see posts from people that you're friends with or stuff that they shared of other peoples or whatever you only see your friends stuff you don't see everybody on Facebook stuff that's essentially exactly how this works so you can see I have a post here in my feed these are all mine but then I have one here from John Doe friends list john doe's on my friends list if I go to the members directory and I go to Jane Doe and I add her as a friend and I go to the feed now you can see I have a post from Jane Doe here as well and then I let me actually go here Jane Doe a remover is trying to go back to the feed she's gone this is the point that I'm talking about that we get to - making a social network useful because now the social network allows me to see information about people who I'm friends with who've I've chosen to connect with on the site that's what starts to make it useful okay so this is what's making it useful for the user then you can do stuff like analytics on the back end to track you know what posts get comment on what get liked what gets shared and you can have pages for different businesses or brands or products or whatever that people can like and so on and so forth to figure out what they what people are interested in but that's making it useful for you down the line as a someone wanting to advertise first and foremost you need to make it useful for your users that's actually why Facebook for a lot of years didn't advertise it wasn't because they you know they're it's kind of portrayed like they had this you know maybe self-righteous view that they weren't going to have ads on their site ultimately that really wasn't it they have ads on their site so now and I mean that's where it was headed the reason they didn't do it at first is because they wanted to focus on making it useful making it something that was cool for people to use that got a whole bunch of people using it and then at that point when and in the background you're building the systems to be able to track all the information and you're getting good at tracking it and then once you have all that in place then you can start advertising start advertising from the beginning becomes less cool your systems aren't as efficient in the backend and you you don't have as many people on the site and your advertising doesn't work as well and so advertisers are turned off it makes a lot more sense to make the site really cool first make your systems for in the back end for your analytics for figuring out your interest graph in your social graph for making those efficient second and then third start actually advertising and then of course you know make the entire thing better and better as you go along so again this is the point where it gets useful for your users because they can see information from only their friends they can also post statuses that only their friends can see so now you're allowing your members to communicate back and forth one another so if I type in something here now I hit submit now if I go to the feed of course once I get on videos have an issue but I think I know what it is so let's go back over here and let's try this again we are on a video hit submit and we head back over to our feed okay so we are on a video I typed the contraction I typed a word w possibly re I don't have any in the back end here I don't have any kind of data validation or preparation or anything like that so it throws off the my sequel statement when you have that extra apostrophe in there that's why that's doing that but again with your data I mean this very bare-bones so it would be a lot easier to follow the code you see it's a lot of code as it is so again with yours you're gonna want to prepare the statement so you want to check for all that stuff okay so we can see when we post the status now we have it here so now other people can see what I'm posting again if you've been on Facebook or something like that you know that's a lot of what people do is they sit there and stare at their newsfeed to see what their friends are - so this right here is a very big part of being able to do this and it's based off of our friends list as you saw so I'm going to show you how to do that then of course next we have our inbox we can send messages back and forth between private messages and we have a compose area here to compose a message okay so all fairly straightforward stuff but we're going to dive in and show you how it works now let's go ahead and take a look more in depth at the database on the backend here so let's start off with our users database and you can see we just have three users here so we have their user IDs we have the user email as an email for each one we have their password which you can see I'm not even hashing the password for this demo this is all stuff that's covered in the login tutorial so really all of this pretty much most of this users stuff is going to be covered in the login tutorial and when you're registering your users and then all you would simply do is allow users to update this information on your edit profile page in this tutorial in the in the social network okay so again this is very very very basic have their nice name which is essentially their actual full name you could break that down to first-name lastname how do you want to do that and of course you're going to have a lot more fields here you may even have another table that would be like a user meta table or something like that to help expand this sort of the case maybe so you're going to really flesh this out then if we come to our friends you can see our friends table here really what this is is this is a relationships table so we've talked about relationships tables and how you create a relationship between one object and another object so your users you know your user would be one object and let's say a CMS your user would be one object in your post would be another object and you would have ways for those to interact with each other so for example you could say this post which is the object was written by this user or that's the author that's one way that you allow those two objects to interact with each other in a CMS and you have a whole bunch of other objects here what we're actually doing is we're kind of using the same object so we have our main user are logged in user and then we have friends which are other users so we're essentially allowing the same object to kind of interact with itself but in a very specific way so we're allowing people to friend each other so essentially what you can see is you have a user ID and you have a friend ID so this is going to be the ID of a user who initiates the friend request and then this is going to be the ID of the user who accepts it or whatever so if we come over here you can see I have user ID one and friend ID two so if I come back over here and I go to the members directory and let's go to Jane Doe and let's add her as a friend now you can see up here her user ID is three so if we come back to the friends you can see that now we have a user ID of one which is me now I have a relationship with friend ID 3 or user ID 3 and user ID - ok so that allows us to create that relationship all we're doing in a relationships table is we're saying this thing is connected to this thing somehow we don't necessarily know how in terms of the database doesn't know how but we know how and we can write or then write or logic based off of that so again it fits into the overall context you don't necessarily have to specify a whole lot in a relationships table you just essentially this ID is the ID of the relationship this is the user ID of one user and this is the user idea of another user and we're saying they're connected now when we query this database we can query for all of the relationships that have a user ID set to one so that would be all of my friends and so I can query for all my friends and get their user IDs so I would get those two user IDs and if we come to my friends list you can see that that's what I have I have those two friends okay and then if I go to Jane Doe and I remove her and we go back all we're doing is deleting the relationship okay so it's pretty straightforward to do the friending in terms of how it works with the database of course we'll get into the code of how that works but it's a pretty straightforward concept now there would be some things in terms of how you write this that you need to look at because for example in the query I'm going to show you when I get my friends list I only query for my ID my user ID which is one in this user ID field here I only query for it there now it's entirely possible that John Doe could add me as a friend and his user ID would be here and mine would be over here in the friend ID because he initiated the request he would still meet my friend so I need to run the query actually on both fields in this particular example I didn't but that would be one example of something you would need to do to kind of take this further and account for different things that you might have within your particular social network okay but again the concept of creating friend relationships is really that simple you can see it's three it's three fields here and really there's not a ton more that you would necessarily need for this now you may come up with something and you know you may be able to figure out why you would need more fields but it's really just to create the relationship this is what you need now if you want to specify a bunch of information about that relationship then you might need more fields or another table or whatever but to create it this is really all you need okay so we get pretty straightforward then one's going to the status you can see again this is kind of like a CMS essentially because we're allowing them to create content so each one of these is like would be like a post in the CMS just with a lot less in for me so it's really kind of a simplified CMS so again we have the ID of the post we have the user ID who posted it we have the timestamp of when it was posted and we have the content so again you know at this point you should know when we want to list those then all we need to do is query for those this database or this table for our posts and then we do some filtering based off of who's on our friends list and I'll show you that but again it's just a simple query to get this data ok and last but not least are the messages so again messages are essentially like creating posts so again this is a lot like a post status it's a lot like a CMS the only difference is that you're sending it to someone specific so you can see here we have the ID we have the time we have the content just like we would we have the subject would be which would be like a title in a CMS we have the sender ID which would be a lot like a author in the CMS the one thing that's new here is the recipient ID so we're specifying specifically who this goes to okay so that's what allows this essentially makes this that one extra field makes this a messaging system so when they create a message if you come here and we go to compose then we select who it's going to be sick the subject in the message and then in the logic I populate the the time and who it's coming from based off the logged in user okay you can see I've create a couple messages from me to looks like John Doe and then from John Doe back to me okay so a couple messages we'll take a look at that so that's the database as you can see this is really straightforward and simple but having those friend relationships that's really the big point that you're trying to get to is being able to create those friend relationships because then based off of that we can start to filter our feed we can start to be able to private messages so on and so forth okay so again that's basically the social network one thing I want to show you now just to kind of drive this home a little bit if you remember when we looked at the members directory and we looked at John Doe for example you can see where our profile view PHP user ID equals to if I go to view profile I'm on profile view PHP so again if no user ID has passed we go to mine if it user ID is passed then we go to whatever user ID is passed now that may seem fairly simplistic to you but what I want to show you so we come over here to Facebook I'm going to show you what I want you to look at is this right up here you see this URL and we're on profile PHP and we're passing in an ID that is this user's ID okay so this is exactly how Facebook works okay now what's different about Facebook if we come over to this one as you'll notice that we have this custom URL here well what that is is essentially Facebook is rewriting the URL but this user still has a user ID and all of the information for this user is being populated based off of that ID where Facebook is just hiding it from the URL up here using you are who are we writing but it's still based off of the user ID just like this and you're still on the profile dot PHP page again it's just being hidden by URL rewriting okay so what I'm showing you here is the basis for how these things work so I just want to show you that so you know that what we're talking about is legitimately how this stuff works all right all that out of the way now let's go ahead and dive into the code so I want to cover a couple quick things we have our index page here we have profile edit profile view friends directory friends list feed post feed view messages compose messages inbox then we have a database class we have an insert we have a query and we have a style all right so you'll notice I have I've done what I tell you not to do which is I've hard coded the navigation and the header and all this stuff into each one of these pages I did that so that I didn't have to explain that first and so I could just keep this as simple as possible without having to show you the different includes and all that stuff so that's why I did that but you would definitely want to theme this out have that header dot php' and use like what I showed you with the website template use that to theme all of this out and then just add custom code that you need to each particular page for whatever it happens to be doing okay so that's the first thing now what where I want to start is I actually want to go into the database class because the database class is really pretty much the same as what we've covered so I just want to cover it real quick again straightforward database class this is actually pretty much the exact same one that you saw on the CMS lesson except we've added this update here I believe is the new one so we've added this update function here essentially it just it just runs the query that is sent and in the query we do the update but again you can make this a lot you know you could flesh this out you could use something like easy SQL which is a class you can Google it's a class that has a lot of stuff that's done for you so you can connect to my sequel really easily and so on so forth so again you could flush this out and make this really robust and I would suggest you do that if you're going to build a social network because you'll need it then on top of this class essentially we have two classes we have the insert class which is for inserting data into the database and then or updating or deleting whatever and then we have our query class which is for querying for data so it's for selecting data okay we do that just to keep it clean you would actually probably have even more have even more classes than this because you would probably have you would probably have a class just for users you have a class just for you know statuses you'd have a class just for messaging you'd have a status your class just for your feed okay so you'd have a whole bunch of different classes that would do all of the big logic behind the scenes so when you got to these individual pages here it's really straightforward and easy for you to do and I did do some of that but you could definitely do a lot more okay so these two classes sit on top basically of the DB the database class pre-notice require class DB PHP in this one and same thing in this one alright we have a style sheet this is just for basic styling we're really not going to cover that at all and then we have our actual content pages alright so the first thing I'm going to look at is edit the profile alright so we include our query and our insert class we have our logged in user again our user object what we would get from our login system and then what we're doing is we're doing two things this is the edit profile page one down here we are querying for the existing data for this user case we're on the edit profile page so we know that this user exists we want to get their existing information and we want to use it to pre populate the form so good notice on the edit profile page this information is pre-populated with what's in the database so they can just simply change it okay so that's why we query for the user and recording for the log and user ID appear okay and then we're populating that information down here so that's all that's for next what we're doing is if there's a post information so if the form has been submitted then we're going to run an update and so update equals and then we're insert this is the variable for our in insert class so if you come down here see all the way down here this insert class okay so this is the variable for that so we're just calling a method in our insert class we'll called update user and to that we're passing our logged in user ID and we're passing our post information what got submitted in the or so we know what user to update and we know what that it updated with all right so that's getting sent to the update user method in the query class so let's come go ahead and come over here and if we scroll down or our insert class I'm sorry if we go here we can see we have our update user right here takes the user ID in the post data we globalize our database variable we set our table to s users and then we run our query so update our set table s users and we want to set the user email to our post data user email so post data here is what was passed in the post variable we want to set it to the the email to the past email the password to the past password and the user nice name to the past user nice team so we're just updating information and we're doing it where the ID equals the user ID was passed so the user that we are that is logged in then we're just running this and remember from database class this just essentially runs the query for us so there's no need to go to that database class but we're querying we're actually running the query here and returning the data and that is what's going to update our user and so it's going to either return true or false based on whether or not this query went through successfully okay so over here you could actually we save that as updating this would be true or false you could actually run some logic off off the top of that to set a status message that says settings were saved or settings were not saved or something like that if you wanted to okay so that's pretty straightforward that's how we edit the profile that is you know it's straightforward stuff stuff that we've been doing for a while now if we go into profile view then you can see we have a few things going on here okay again at the very basic what we have is we have a set up to view a profile okay so this is the logic that makes it work so if we click on on the view profile page and no user ID is passed then it's going to take us to our user ID if it's if a user ID is passed then it's going to take it to that user that user's profile page okay so remember that was passed as a query parameter in the URL so we're going to use get to get it and we're going to see if it's if it actually exists if it's been passed but hasn't been passed then you can see we're going to go down to this else statement and we're going to load the user object for the logged in user okay so if no user ID has passed this is a query parameter and we're just going to load the logged in user and we're going to set this variable mine to true essentially what that variable is saying is that we're on my profile page as in I'm the logged in user so we're on I'm on my own profile page and we're going to use that for logic down below here okay so that's if no user ID is passed in the query parameter if it is passed then we're going to set the variable user ID to whatever got passed and we're going to query we're going to load the user object for that user okay and then we're going to see if okay here's an example of this if I go to the members directory and I click on my own name you'll see I'm getting a user ID passed so it's possible that a user ID could get passed but I still be on my own profile page so that's what this check essentially is here for so it's saying okay user ID was passed but is that user ID the same as whoever the logged in user is if it is we're still going to set mine to true okay so we can run our logic down here all right so that's what this little section is doing right down here now we so we either load the user object that was passed or we load the logged in users information and we display that here again you can see we're saving that as a variable called user that's the user object then down here we're using that to echo out the nice name the email and so on and so forth and we're so again that's pretty straightforward stuff here what's different is that now we are we have this variable called mine equals true and on top of that we're going to run a conditional so that we can create this if we go to the members directory and go to Jane Doe we can create this button for adding as a friend because if I'm on my own profile I don't need to add myself as a friend okay so we don't want that to display well we need a way to know if we're on our own profile same thing if I go to the remembers directory and I click on my own profile yes the users your ideas pass but I'm still on my own profile because my user ID that was passed so that's why we need to use this mine equals true we need to know if we're on the logged in users profile page right so now we know that we can come down here and you can see we're doing a check right here if mine if if not mine essentially so if I'm not on my own profile page then I'm going to display this form and it's going to allow me to either add a friend or if they're already my friend remove them as a friend and then this check right here is running a query to see if they're on my friends list so we're creating that relationships database to see if the profile the person whose profile I'm viewing if they are a friend of mine okay and then we can run a check so if they are not a friend of mine then we're going to create this form that allows me to add them as a friend and in that form we have some hidden fields here so we're going to have the user ID which is my user ID we're going to have the friend ID which is the person whose profile page I'm viewing and I want to add as a friend and have that here and then we're going to have the type which is we want to add this person and then we have the submit button which is add his friend now if they are not are they if they already are on my friends that's what comes after this else statement then we're going to display button that would allow me to remove them so again we need my user ID which is here we need their user ID which is here and this time instead of typing passing a type of ad we're going to pass a type as of remove we're going to have a button it says remove Fred so essentially this is just checking to see if they're all my friends list or not if they are it's going to allow me show me a button to remove them if they're not is going to show me a button to add them okay so then I'll click one of these buttons and what happens is we're going to get post data so if we have post data then we're first thing we're going to do is we're going to check and see what type of post data we have is in an ad or is it remove if it's an ad then we're going to run in our insert class we're going to run the addfriend method and we're going to pass in the user ID which is my user ID we're going to pass in the friend ID which is the person I'm trying to add if it's removed then we're going to remove them and we're going to call the remove method in the insert class and again we're going to pass in my user ID or the logged in user ID we're going to pass in person I want removed so that's what this is doing right here so we have two methods in the insert class add friend and remove friend so if we go to the insert class and you can see we have add friend right here and we have remove friend right here these are actually really similar so if we have add friend user ID friend ID globalize our database set our table which is the s friends table and in this case we're going to insert into our s friends table which is our relationships table and we're going to insert the into the filled user ID into the filled friend ID and in those we're going to create we're going to pass into use right my user ID of logged in user ID and the person who I'm friend and then we're just going to send it to the database class to run the query okay so what that looks like is if I come over here and I go to the members directory and I go to a hit ad friend it's going to do all that stuff if I go to the relationships table it just created that record that's all it's doing is just creating that record so that is it's it's really straightforward with removing the friend it's really similar user ID friend ID globalize database the table but instead in our query we're just going to delete the record so we're going to delete from the table where the user ID equals the user ID that was passed which is logged in user and the friend ID equals the friend these are ID of the friend that we're removing okay so again we're just essentially we're querying this to see where we get a match so when these user ID the user ID that's passed of the logged in user matches here and we have a match with the friend ID we're going to remove this record so if I come over to my friends list and I go to Jane Doe and I remove her as a friend she's user ID 3 then we refreshed this friends list to his friends table you can see that that record is gone because this would have had a user ID of 1 and this would have user ID of 3 there was a match and so it removed it okay so that's essentially what the remove friends function is doing and that's what we're doing in our view profile page in order to add friends ok and this is where we're querying here to see our friends list who's on our friends list so if we go we're running the getfriends method in the query class so we go to the query class we go to get friends right here you can see that we're passing in the user ID which is the logged in user so I want to get my friends globalizing database setting the table running the query which is select the ID and the friend ID from the table that we set where the user ID equals user ID was passed which is mine so essentially query the friends table for my friends and this is going to return us than an object of different friends and what we essentially are doing is in this particular instance all I need is the friend IDs okay so I'm going to run a for each loop where I create an array that's only the friend ID so I'm going to strip off all the other data essentially so that's good and it's going to take it from being a multi-dimensional array to just a single dimension array so it's a little bit easier to run logic off of okay so what gets returned is an array an indexed array of user IDs and those user IDs are people who are on my friends list so if we go to the profile view that's what we get right here is that array and then down here we're running this check-in array and then my user ID and the array this friends or the user idea of the this is the user ID of the whoever whoever's page we're on okay so that's going to see if and this is the friends array the list of me is logged in use from my friend so it's going to basically run a match and see if the page that I'm on viewing the profile I'm viewing is on my friends list and then if it's not because I have this exclamation point then it's going to display the add friend button and if it is this else statement it's going to display the remove friend but okay so that's how that works all right so that is the first two pages now like to go to the friends directory this is a really straightforward one because we're just grabbing all of the users in the database essentially so this is a member directory you can see it's really straightforward we include our query class and then we run our load all user objects this is essentially querying the database for all of the members getting and getting their object information so getting all the information about them then down here we're running our do user directories we're actually creating our directory down here so if we go to our query class and we go to do user directory then you can see an X look at this I can just get rid of this because that must have been for when I was testing because the only thing I really need is this do user directory here because in do user directory I'm calling the load all user objects method here and I'm loading it into this users array here so that's going to give me an array of objects and those objects are going to tain all the information I have in the database about each user then we're simply looping through those objects and we're creating an output so we're going to have we have a div here we have an h3 we have a paragraph tag and we're echoing out the for the link we're using the user ID for we're using the user nice name for the display name and then we're showing their email as well you probably wouldn't show their email but I'm just doing it to show you you can access all of their different information okay so if we go back to the members directory then you can see for the user ID or for the link we need the user ID so when I click on this the user ID gets passed and I go to that particular user's profile page ok so that's what we're doing there and then of course we're just displaying their display name and their email address again a lot of the same stuff that we've been doing so that's what's going on in this do user directory so the friends directory page is pretty straightforward which is listing it and a lot giving links so that they can link to to a profile then they can add them as a friend or whatever the case may be the friends list is pretty similar the only difference is instead of querying for all of the members we're going to query for only the members that are on our friends list so you can see we're including our query class in our insert class we're loading our user object and then down here we're running query do friends list here ok so if we go back to our class query and we go to do friends list then you can see that here we're passing in a friends array okay now this is a little bit different way that you can do this so you see here I have friend set and I'm querying calling get friends and getting the logged in user ID okay so if we go to query and we go to get friends you can see up here what I'm doing is I'm passing in the user ID so I'm passing in mind user ID or the logged in user ID so those are the persons that's the friends I want globalizing database setting the table and then we're selecting the ID and friend ID from the friends table our relationships table where the user ID equals my user ID so again just querying for my friends okay so we're going to grab that and then we're looping through to get the friend ids and we're returning the friend ids okay same as before so that's going to populate that into this friends list then we're going to pass that to this do friends list function here now in the query class instead of doing it that way you could do it where you actually in do friends list you just call the get friends function or method inside do friends list instead of having to call it in the header of your site which is the better way to do it I'm not exactly sure why I did that that way on that one but I was laying down a bunch of code so anyway so again it's really the similar is the directory once we have our friends array here again we're going to do a for each loop we're going to loop through front our friends array but this time what we need to do is we need to actually create that array and we need to turn those IDs into objects you remember that the get friends just returns their ID so but we need all the information about them so we're going to loop through the IDS and for each ID we're going to call the load user object method and we're going to pass in the friend ID and we're going to save that into an array of objects and so if we go to load user object you can see load user object takes a user ID it's going to query the users table and it's going to get all the information where the ID equals the user ID so the user ID we want it's going to query the database for a user and return all the information about that user it's going to run through and it's going to do all of its routine here to get that information essentially into an array and it's going to return that information for that particular user and then we are down here we're saving for each ID we're saving that user information into the into a new array so again this is essentially going to create us an array of objects much like up here where we load all user objects and gives us an array of objects here we're just kind of creating it ourselves and we're loading all the user objects okay once we have those then you'll notice this code is actually identical to the code above because it's really the display is the same now for you it may be different for your social network but this is identical to the code above okay so we're just displaying it out the information about that user okay so that is the members directory and then the friends list both straightforward so now we've allowed to edit the profile we've allowed to view the profile and add friends which we've covered got our friends directory now we have our friends list now that we have all that we can get into doing something useful for our users and that useful thing is allowing them to communicate with each other both in public and in private because ultimately that's what draws people to a social network is the ability to communicate with their friends in a way that's a lot easier than having to drive to their house or whatever the case may be that's the value okay so the way we do that is we do it through Status Messages and we do it through an inbox so if we go to feed post you can see again this is really straightforward so we're including our in class we're setting our user object 1 and then we are running some stuff if we have post data ok so again for posting the status it's just a form so down here is our form you can see it's a really simple form method equals post our input name or our input here is the status time so the time this status was posted it's a hidden value because we're just going to get it in PHP and then we're setting you see we're setting the value to time the time function in PHP so that's just going to get the current unit unix timestamp and it's going to pass it in our post our post array as status time here we have our label this is what's on your mind then we have a text area for the status content and so this is where they can actually post the content of their status message and then we have a submit button very straightforward form and then when this gets submitted then we're going to grab that post data if there's post data we're going to grab it and we're going to pass it to we're going to pass it along with a logged in user ID to the function reading method in the insert class called add status so if we go to the insert class and we find add status down here user ID it takes the post variable that we're going to globalize the database we're going to set our table and then we're going to run our query you can see for all of this stuff the only things that are really changing are what table we're working with and what query were running so that's why you have a query class or an insert class to run all of this stuff and kind of keep it separate so then over time you can expand this and it's really easy so again add status we're going to insert into the status table and we're going to pass in a user ID the status time and the status content and go to the status table user ID status times TAS content that's what it requires so that's what we're going to pass it and we're going to the values that we're going to passed are the user ID so with the user ID that was passed with the logged in user so I'm posting the status as a logged in user it's going to pass my ID the status time which was that hidden field and then the status content which was the text area so that's what's going to get passed to this query variable and then of course we're going to pass that to our database class to actually run the query and then that's going to come back to false and we have our again you could run logic on top of this to see if it actually you know for a status message or whatever the case may be okay so that's how we allow our users to post status updates to the site so that's again very straightforward it's a lot like a CMS adding data except a very simplified version where we can start to get a little more cute or have a little more fun with this is when we go into the feed view okay so this is going to again you can see there's not a lot of code here but this still allow us to display feed information based off who's on our friends list ok so again we're including our query class we are our logged in user object and then we're doing in our query class we're running a function called do newsfeed and we're passing in the logged in user all right so if we go to the query class and we go to do newsfeed right here you can see that the first thing we're doing is we're getting the status objects and so we're calling the method called get status objects user ID so if we go to get status objects and you notice we're passing it a user ID so if we go to get status objects then get globalizing database setting the table now but before you run the query we're going to query for our friend the IDS so we passed in the logged in user ID now we want to know well who are this logged in users friends because we only want to get status up dates for this particular users friends we don't want to get all of them across the site so we're getting the friend IDs we're running get friends again to do that and you can see now we're starting to layer on top of our own code making things easier for us okay so we're passing in the user ID and we're getting an array of friend IDs now if it's this if when we do that query it's not empty then we're going to do an array push and what that is essentially doing is its appending a user ID it's are pending an element onto the end of array what element re appending will we're appending our own user ID because we want to allow our own posts to show up in our newsfeed as well when you run this get friends it's only going to get the user ID of your friends it's not going to get your user ID as well so we're just doing an array push to push our own user ID on the end of it so that we can query then for our posts as well now if this friend ID friends ID array is empty then we're just going to set it to our own user ID so if essentially what that's saying is if we don't have any friends on our friends list then we're just going to query for our own user ID because we can still see our own post okay so now this gives us essentially an array of IDs user IDs that we are that we are authorized to see the status posts for okay so then what we're going to do that do essentially is put that into a my sequel friendly format because it doesn't accept PHP arrays so we're going to explode it which is going to break apart the array and it's going to turn it into a string and it's just basically going to create a comma separated list so it'll be like look something like this so if you get user ID 1 3 5 7 that's what this is turning the friend IDs array into a something like this okay and that's more my sequel can deal with that they can't deal with an actual array alright so once we've done that then we're going to actually run our query we're going to select all information from the status table where the user ID again if we go to our status the user ID of the user that posted that status if that user ID is in our accepted ID comma-separated list so it's if it's one of these IDs then we're going to accept it and we're going to order these by the time and we're going to do it descending now it's important because if you just run this without the order by it's going to actually put them in ascending order so you need to set this specifically to order by the time and put it in well actually that's not true if you don't put order by all it's going to return them in record order so it's going to return them how they show up in the database here now if you've ever done a thing with the database you know that these can actually get mixed up in the database so just because the they get added in a certain sequence doesn't mean they get stored that way so you want to just order them by the status time so that's something to keep in mind and you want to do it in descending order so that the newest post goes on top okay once we have that then we essentially just run a query and then we return our status objects and this is going to give us an object that we can an object of status or an array of status objects so it's an array of objects that have all the information about each status that fit our criteria in our query all right and we're going to return that to the do newsfeed method down here so for our status objects then we're going to loop through the status objects and guess what this little chunk of code right here is eerily similar to this up here now it is a little bit different because we're not returning a list of users we're returning a list of statuses but it's really the same thing once we have our status objects then we're going to loop through and we're going to have for each status object we're gonna one thing we do is we're going to query we're going to load our user objects we have all the information about users that posted this specific specific status post and then we're going to display their name and a link to their profile and we're going to step display the status content okay so again pretty similar to what we got going up on here with a few changes but that's what's going to create our newsfeed so again that was actually fairly straightforward the big piece of logic that happens is when we get our status objects before we run our query we essentially do this part here to get a comma-separated list of user IDs that we are friends with whose posts we want to see now this is where you get added stuff like you see on Facebook where you can ignore posts from a certain user so then you could have a flag that says yes you're friends with this person but you've you've said that you want to ignore their posts so then you could run a check here that says okay are any of these users then on your ignore list found your nor list then we're going to take them out of the accepted IDs as well and then we're only going to query for what's left so essentially you could add a bunch of filters for different things you might want to do an ignore list or you know whatever a thing you might come up with you could filter this array down to only query for the status updates for those particularly users also on top of that you know with sites like Facebook you can do comments and likes and shares and things like that and the way that works is for example you could add this in there for comments is essentially a comment would be another table one could be s comments and it would you would have actually you'd have two tables you'd have a relationships table that tied a status to a comment so just like we have user IDs tied together in our friends table or in ICMS you have post type two categories here you could in the create a relationship table that ties a status or a comment to a status and then you would have another table for the actual comments that would have all your information about that comment so would have who posted it it would have you know it would have the the content that was posted half the time it was posted and so on and so forth so he could create that and then you could have comments on there you could have like so you could do something similar with a likes table and you'd have shares and so on and so forth so if all this different stuff that you could add in here to this particular part to just make it a lot more robust and you know a lot better social network of course the thing to keep in mind is when Facebook first came out it didn't have any of that stuff so you don't have to write that stuff day one I mean it took them how many years before they had likes and before they had you know all that different stuff that they had took them awhile to get there so again you don't have to write all that stuff right up front all right so that's the newsfeed the next thing we're going to go to is we're going to go to the messages so we go to the message compose here and again you can see we're including our two classes getting our logged in user we're going to do some post stuff here in a minute then one thing that we're doing is we're getting our friend IDs because we're going to pre-populate the to field with a drop-down box of our friends now if we go to the site and we go to our compose you can see we have a drop-down box populated with our friends if I go to the directory go to Jane Doe here add her as a friend and go to compose now you see she shows up there as well okay so you can do something like that of course if you have a site like Facebook that has billions of users trying to populate someone who has 500 friends and a drop-down box may not be the best way to go about it so they use something they use more of an AJAX type thing where you start typing in the to field and it'll start to fill we'll start it'll go through your friends list let's still query your friends list but it's going to go through and use Ajax to as you type filter it based on what you're typing to to where then you can just select one I'll pre populate it with that information so that's a little bit more efficient way of doing it or probably the biggest way people write messages is they just go to someone's profile and it has a thing that says send message on there when you click it it takes you to the forum and it just prepopulates the two filled with that users information okay so there's number of ways you could do that but ultimately this is just the way I'm showing you here so I select Jane Doe and then have my subject and my message in my submit so that's what this is doing here so we're getting our friend IDs again who am i friends with and then we're creating an array of objects of information about each one of those users really similar to what we did just a little bit ago so that's essentially giving me information about who my friends are this is something when you're running a social network by the way loading a user's friends and their friends information on a page probably going to do on almost every single page you have if you go to Facebook there's pretty much something about your friends on nearly every single page that you see so this is something that you're going to probably do on every page so it would be a good idea to just have a function that does all everything that you need to do in order to load friend information and just have a single function call now I don't have that here because I didn't do that but that would be a good idea because you're always going to be calling friend information so why constantly rewrite code you don't need to turn it into one function and then you can just call it and you'll be good to go and of course if you're doing this the smart way and you're theming this you can actually just call that in your header and then it'll be there in all of the pages someone visits so that would be an instance where some putting something like that in the header makes sense because you're going to use it on pretty much every page now when you have a system when there's certain other things where you might not want to load on every single page because you're not going to need it for the majority of your pages you only need it for specific pages so loading into the header time doesn't make sense you would want to load it on that individual page but friends most pages you probably load it so loading in the header would probably be a good idea again you could look at that for your social network all right so we're getting our friends list here our ray of friend objects and then down here we're creating a form again a pretty simple form you're going to see we have two hidden fields that's the message time which we're going to calculate again and then the message sender ID which is the logged in user ID because we already know I'm the one sending the message because I'm logged in so we don't need to have someone input it we can just put it into a hidden field and try to do that as much as you can try to get information that doesn't need to be set by the user just have an auto populated in a hidden field like this because then that makes less clicks for your users all right then here essentially this is our drop-down box for our our two field that's the big logic that's going on in this page we have our friend objects we're doing a select box called message recipient ID and we have an initial value called select friend which is a default value you do that you don't necessarily have to do that but you can do that if you do just leave it blank and then in your post variable just make sure that this has a value set if it doesn't have a value set then you can run some logic to throw an error or whatever however you want to handle it but that's how you can do that then down here we're doing it for each loop and we're Auto creating the rest of the options for the select box so we're looping through our friends and we're creating an option with the value of our friend ID we want the ID because remember in our database we need for our messages we need the recipient ID okay so that's what we want you want to always try and do all of this stuff by IEDs okay so you set the IDS for every table because we go to users if I don't set this ID field then I don't have a user ID and I can't use it in here but I've said it and created it when I created my database now I can use it in here alright so so we need the user IDs that's what we're sitting as the value and then for the display the what the user is actually going to see when using it is they're going to see the nice name so we're going to see the name of their friend okay so this is for the computers this is for the people that's essentially how it works and the loop and our select box and then these down here just pretty straightforward it's a simple text box of the subject and then we have a text area for the content and then we have a submit button so all straightforward form stuff when that gets posted the form gets submitted we're going to check our post data it's not empty so then we're going to run this send a message method in the insert the insert class and we're going to pass in the post data so if we go to the insert class and we go to send a message which is our last function down here again we globalize the database set our table to s messages and then we're just inserting data again so we're going to insert into the table these are all the fields that are in our database in this table so we're just going to insert that into each field and the data we're going to insert is the data that was submitted in our post array that was submitted by the form time was the hidden field sender ID was a hidden field recipient with the drop-down box message subject was the text input and the message content was the text area okay so that's what's getting submitted and then we're going to send this to our database class to actually run the query and that's going to create our message and so we're going to have a record now in our messages table it's going to have that information finally in our inbox then again we're going to include our classes we're going to have our logged in user ID and we're going to do a method in the query class called do inbox so essentially all we're going to do in this do inbox is to query the database for the messages table for any messages that have a recipient ID that are my ID so have I received any messages as anybody created a message that had me as the recipient okay so message objects equals this get message object so it's a method in this class up here you can see this is that method passing in the user ID because this is again I'm the logged in user so I want to know what messages were sent to me gets passed here globalize database at the table to our messages table select all information from that table where the message recipient ID equals my user ID so any message that were created with me is the recipient we're going to actually run the query and we're going to return those objects back down to our do inbox function and then for each we're going to loop through that array of objects and for each one we're going to create an output to output that information this again will look eerily similar to what you see up here for the newsfeed again we're going to get the user information so we have all the user information for who sent us that the message and then we have our link for their name and their their display name we have the message subject and the message content that we're going to echo out so if we go to the Inbox that's what we get it was from we have the information about this user link to their profile the subject and the content okay so that's how that works so that is a basic social network that gives you an idea of how some of the really you know fundamental things of a social network works as you can see it's actually conceptually fairly straightforward now again you want to get to the point where you make it useful for your users and that's what really this gets you there of course you could flesh this out a lot more you can make it look pretty you can do all the requisite checks that you need to do you can prepare data before it's inserted you can handle different situations etc etc you could add in things like a WYSIWYG ad and all those things on top of this but this is really kind of the fundamental of what what you need to do and then of course you could develop this further to do different things to add in more things to your social network and so on and so forth and it's just a matter of working with this within this framework to do it okay so that is how to build a basic social network again conceptually it's fairly straightforward so hopefully it came through again if it doesn't click right away I would say maybe go through this again look at the look at the source code and just try to get your head around as best as possible so again hopefully you found that helpful and I'll talk to you later you
Info
Channel: John Morris
Views: 330,630
Rating: undefined out of 5
Keywords: PHP (Programming Language), social network php
Id: Wgbmgf84ZOo
Channel Id: undefined
Length: 76min 3sec (4563 seconds)
Published: Sun Apr 28 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.