Welcome to news channel

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Create a simple news site with PHP and MySQL using PDO

zooboole/How-to-create-a-simple-news-web-site

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

#Create a simple news site with PHP and MySQL using PDO

This tutorial is an update of the existing tutorial on how to create a very simple news site using php and mysql. The aim of this tutorial is to help you understand some basic concepts when using php and mysql. It’s also a way for beginner to learn how to put together a full project using dynamic programming concepts and databases.

For this project you will simply need a web server with a database to run the site. By web server I mean you should have Apache Server, MySql Server, and PHP. For those who are on windows you can easily install WAMP, or XAMP.

And those on Linux Systems can use LAMP or MAMP for Mac OS users. Another way to have all this is to have a virtual web server. You can install one yourself using VMware or Oracle VM VirtualBox. For those who are advanced in web servers stuffs, I believe Vagrant will be the best for you.

At the end of the day, what matters is for you to have a running server that has php and a database that you can connect to.

Since PHP 5.5.0, the mysql extension is deprecated I will be using PDO. So if you don’t have PDO extension activated you can do it now. Or if could also use Mysqli instead. I have decided to use PDO to make the tutorial more universal because in the first version of it, many people had problems with mysql and were encounters a lot of errors.

Here I will be creating a very simple website that will display a list of our recent news posts ordered by date.

For each post we’ll be displaying its title, short description, the date of publication and the author’s name. The title will be a URL that links us to another page that will display the full content of the news(See image bellow).

list news

On the page displays the full artile, we’ll also list other articles so that the user can easily read another news without going page to the home page. We will also provide a link to go back to the home page.

Create a new database and name it news or something else. We will at this level have only one table in our database. The table will contain all our news. I named the table info_news; you can name anything you want what matters is the structures.

So create your database and create following table in it.

CREATE TABLE IF NOT EXISTS 'info_news' ( 'news_id' int(11) NOT NULL AUTO_INCREMENT, 'news_title' varchar(255) NOT NULL, 'news_short_description' text NOT NULL, 'news_full_content' text NOT NULL, 'news_author' varchar(120) NOT NULL, 'news_published_on' int(46) NOT NULL, PRIMARY KEY ('news_id') ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; 

Table design

You may be wondering how I got the structure of this table. The site structure helped me understand different information I need on each news article

For this project I will be using two main files: index.php and read-news.php.

Since I want to make the project as simple as possible just to show you the concept, I won’t be using a PHP framework or adopt an MVC design pattern.

But, I want to keep my code clean; so I will be using a different file that will contain my database connection (dbconnect.php) and another file(functions.php) that will contain some set of functions we will create and use them in the project.

At the end, our folder structure should be like follow:

  • News/
  • config/ — dbconnect.php
  • includes/ — functions.php
  • design/ — style.css
  • index.php
  • read-news.php

News project folder

Check out my own folder bellow:

To start, we’ll require the db.connect.php in functions.php because we will the database instance in our functions. Then in index.php and read-news.php we’ll require the functions.php because we will need those functions in them.

  catch (PDOException $e)< $e->getMessage(); > > 
  • File includes/functions.php This file contains all function we will need in the project. The number of functions may increase as the project grows.
 prepare(" SELECT news_id, news_title, news_short_description, news_author, news_published_on FROM info_news ORDER BY news_published_on DESC "); return $request->execute() ? $request->fetchAll() : false; > function getAnArticle( $id_article, $conn ) < $request = $conn->prepare(" SELECT news_id, news_title, news_full_content, news_author, news_published_on FROM info_news WHERE news_id = ? "); return $request->execute(array($id_article)) ? $request->fetchAll() : false; > function getOtherArticles( $differ_id, $conn ) < $request = $conn->prepare(" SELECT news_id, news_title, news_short_description, news_full_content, news_author, news_published_on FROM info_news WHERE news_id != ? "); return $request->execute(array($differ_id)) ? $request->fetchAll() : false; > 
      

Latest news

Welcome to the demo news site. We never stop until you are aware.

First news title here

This news short description will be displayed at this particular place. This news short description will be displayed at this particular place.

published on Jan, 12th 2015 by zooboole

Second news title here

This news short description will be displayed at this particular place. This news short description will be displayed at this particular place.

published on Jan, 12th 2015 by zooboole

Thirst news title here

This news short description will be displayed at this particular place. This news short description will be displayed at this particular place.

published on Jan, 12th 2015 by zooboole

Fourth news title here

This news short description will be displayed at this particular place. This news short description will be displayed at this particular place.

published on Jan, 12th 2015 by zooboole
phpocean.com © - all rights reserved.

Note: Each news has a specific URL that links it to the read-news.php page like this:

The x represent the unique id of that particular article. So the read-news.php?newsid=x tells the read-news.php page to display a news that has the id x.

Now in this file we want the news to be fetched and displayed from the database dynamically. Let call the function fetchNews() . To do that let’s replace every thing in

$article) :?>

news_id ?>»>news_title) ?>

news_short_description) ?>

published on news_published_on) ?> by news_author) ?>

       

Latest news

Welcome to the demo news site. We never stop until you are aware.

return to home page
0) < // Fecth news $article = getAnArticle( $id_article, $dbh ); $article = $article[0]; >else< $article = false; echo "Wrong article!"; > $other_articles = getOtherArticles( $id_article, $dbh ); ?>

news_title) ?>

published on news_published_on) ?> by news_author) ?>
news_full_content) ?>

Other articles

$article) :?>

news_id ?>">news_title) ?>

news_short_description) ?>

published on news_published_on) ?> by news_author) ?>
phpocean.com © - all rights reserved.
 html, body < font-family: verdana; font-size: 16px; font-size: 100%; font-size: 1em; height: 100%; width: 100%; margin: 0; padding: 0; background-color: #4DDEDF; >* < box-sizing: border-box; >a < text-decoration: none; color: #4DDED0; >.welcome < width: 800px; margin: 2em auto; padding: 10px 30px; background-color: #ffffff; >.welcome a < display: inline-block; width: 200px; border: 2px solid #0DDED0; padding: 0.5em; text-align: center; >.welcome h1 < margin: 0; color: #555; >.news-box < width: 800px; margin: 0.5em auto; padding: 30px; background-color: #ffffff; >.news-box h2 < font-size: 1.3em; padding: 0; margin-bottom: 0; color: #e45; >.news-box p < font-size: 12px; padding: 0; margin-bottom: 0.3em; color: #555; >.news-box span < font-size: 10px; color: #aaa; >.footer

Indeed, this is a very basic way of making a news website. It doesn’t have any professional aspect like serious news sites do. But, we should know that this is a great basement to start with. The point here is mostly to show you how to retrieve data from a database and display it.

So, to make this tutorial complete, these are some functionalities one could add:

  • an admin panel to manage news (add, edit, delete,etc),
  • categorize your news,
  • inhence the design,
  • add comments system under each article we are reading,
  • news scheduling
  • etc.

There is lot one can do to make such system complete. It’s left with you to decide of what to add or how you can use it for other goals.

Voila. We are at the end of this little tutorial. We have created a simple news system that displays a list of articles and when we click on an article’s title we are taken to a reading page that uses the article’s id to retrieve dynamically the whole content of that article.

It’s a very simple system, but with it you can improve your skills in PHP/MYSQL applications. It was also an opportunity to introduce a bit how to use PDO.

So, if you have any question or you are meeting some errors, just comment down here. Check out the demo here, and you can also download the zip file of the source code with comments

About

Create a simple news site with PHP and MySQL using PDO

Источник

News system with comments in PHP

In this tutorial we will add a comment system to the basic news tutorial.

mysql_connect('localhost','username','password'); mysql_select_db('db'); $query = mysql_query('SELECT * FROM news ORDER BY id DESC'); while($output = mysql_fetch_assoc($query)) < $numberComments = mysql_num_rows(mysql_query("SELECT id FROM newscomments WHERE link = '".$output['id']."'")); echo $output['subject'].'
'; echo $output['date'].'
'; echo $output['news'].'
'; echo 'View Comments - ['.$numberComments.']
'; >

We will include the number of comments alongisde the news items. A link will take the user to a second page that will display the comments.

We will use a query string to pass the id of the news item. If the id is not set then we will redirect the user to the main page.

$id = $_GET['id']; if(empty($id)) header('Location: news.php');

If the id variable is set then then we need to check to see if any comments have been posted.

$query = mysql_query("SELECT * FROM newscomments WHERE link = '$id' ORDER BY id DESC"); if (mysql_num_rows($query) == 0) echo 'Be the first to add a comment.';

Otherwise if we have comments then we will output them.

while($output = mysql_fetch_assoc($query)) < echo $output['subject'].'
'; echo date('d-M-Y', $output['date']).'
'; echo $output['comment'].'
'; echo 'Posted by ~ '.$output['name'].'
'; >

Now we need to output a form to allow a user to add a comment.

When the user has added a comment into the form we need to enter it into the database.

$id = $_GET['id']; if(empty($id)) header('Location: news.php');

We will again use query strings to pass the id variable, if one is not set then we will redirect the user to the main news page.

Checks are made to make sure that there are not empty fields otherwise an error message is output.

$postedby = clear($_POST['name']); $subject = clear($_POST['subject']); $comment = clear($_POST['comment']); $date = mktime(); mysql_connect('localhost','username','password'); mysql_select_db('db'); if(mysql_query("INSERT INTO newscomments (id , link, name , subject , comment , date) VALUES ('', '$id', '$postedby', '$subject', '$comment', '$date')")) echo 'Comment Entered.';

We now check the input using the clear function and enter it into the database.

This is very similar to the simple news tutorial however we need one extra query to delete the comments as well as the news.

mysql_query("DELETE FROM newscomments WHERE link = $id");

Источник

Читайте также:  Python opencv распознавание цифр
Оцените статью