Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,151,469 members, 7,812,434 topics. Date: Monday, 29 April 2024 at 01:19 PM

How To Code A Basic Twitter Clone With PHP - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / How To Code A Basic Twitter Clone With PHP (1019 Views)

Is It A Crime To Clone A Website? / Create Png ID Card With Database Info With PHP (2) (3) (4)

(1) (Reply)

How To Code A Basic Twitter Clone With PHP by Ajistotle(m): 4:46pm On Feb 01, 2015
Twitter is a fun web application many of us enjoy using. Rudimentarily, it is a simple idea. Type whatever you want as long as it’s less than 140 characters and share it with the world.

For those who are interested in how an idea like this can be put together, I created this very basic bare bones tutorial of twitter as a demonstration. We will not be using any framework, just raw PHP, so that anyone, no matter your level of experience of choice of framework can follow. As you’ll see, it is easy.

Things you’ll need- A web server (I use Apache), MySQL, and PHP. Hopefully you have basic experience with all this via XAMPP, LAMP or WAMP. You’ll of course also need a web browser to test your work and a code editor(I use Sublime Text 3. Notepad++ is also a good alternative).

STEP 1
The first task involved in making a web app is creating the database that will hold the Information to be used by the app.

Basically, we need a database to hold the name of our users and their tweets. So we will have 2 tables in our database. One named users and another named posts. Each post will have a primary key (an incremental integer) and a foreign key back to the user who made the post.

So let us create a database in our MySQL server. I make use of phpMyAdmin, a graphical MySQL manager that comes with XAMPP but you can use any other one you are comfortable with. I named my database mytwitter. You can use anything you like but I suggest you use the same so that you’ll be able to follow more easily as we proceed.

Next let us proceed to create our users table. You can use your favorite MySQL graphical manager to input the following columns and their characteristics, this is what the raw SQL code looks like

CREATE TABLE `users` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 255 ) NOT NULL ,
`email` VARCHAR( 255 ) NOT NULL ,
`password` VARCHAR( 8 ) NOT NULL ,
`status` ENUM( 'active', 'inactive' ) NOT NULL
);

We have simply created a table called users with 5 columns. Firstly, we have the id which will auto increment and which is also our primary key, the username our users will use, the user’s email, password and a status field to monitor if our user is active or not. All our fields are also marked as NOT NULL to ensure they are not empty.

Next let us create our posts table. Again, you can use your favorite MySQL graphical manager, if you prefer to write the SQL code yourself, this is what the raw SQL code looks like

CREATE TABLE `posts` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`user_id` INT NOT NULL ,
`body` VARCHAR( 140 ) NOT NULL ,
`stamp` DATETIME NOT NULL
);

Here we have created the posts table with four fields. Firstly, we have the id which will auto increment and is also our primary key. We also have a field called user_id which is the user_id of the user that owns a particular post. The field called body holds the actual post/tweet and the field we named stamp will put a time stamp to the posts.

Okay, so far so good. Now we get to the tricky part. We have to create a database table that will hold which users are following each other. We can simply do this by just having a table that has just two fields as follows

CREATE TABLE `following` (
`user_id` INT NOT NULL ,
`follower_id` INT NOT NULL ,
PRIMARY KEY ( `user_id` , `follower_id` )
);

Our table is appropriately named following. We have a field called user_id and another field called follower_id.

The way this works is that this table will be populated by the id values from our user table to indicate if a certain user is following another.

user_id will correspond to an id value in the users table while follower_id will also correspond to an id value in the users table.

As we remember, the id values are actually the primary key for the users table so it is guaranteed that no two users will have the same primary key. If this sounds like greek at the moment, don’t bother, it will become crystal clear when we implement the functionality.

So, we are done with that. It’s time to get our hands dirty with some PHP coding.

This will come up in a second post. Comments, feedback and questions are welcome.
Re: How To Code A Basic Twitter Clone With PHP by Danyl(m): 9:48am On Feb 02, 2015
I will keep following this thread it sounds interesting

(1) (Reply)

How To Create Pages On Your Wordpress Site. / Webhostilla Gives Free Unlimited Webhosting For One Month / Facebook Fan Page For Sale

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 12
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.