Design for Digital Media Environments – Post 8 – PHP and PHPMyAdmin

The main backbone of the website will be coded in PHP. “PHP  is a server scripting language, and a powerful tool for making dynamic and interactive Web pages” (http://www.w3schools.com/php/). This language combined with PHPMyAdmin which is a server-side plug-in will allow me to create databases and fetch, add and remove add from the database on the webpage. This scripting language will only control the back-end of the site and not change any of the visual aspect of the site, this will remain HTML and CSS.

One huge benefit of using PHP is that it allows me to include and require other files to display on the page. This means that it will clean up my code and allow me to create individual parts in there own file making it easier to read and edit the code at a later data.

At the moment from workshops that I have had on PHP I have learnt how to;

Create a connect feature to connect to a database

<?php
define(“DB_SERVER”, “localhost”); – Database server locations, can be hosted else where
define(“DB_USER”, “DB_Username”); – Database login user name
define(“DB_PASS”, “DB_Password”); – Database password
define(“DB_NAME”, “DB_Name”); – Name of the databse

$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME); – Connect function using all of the information above
if(mysqli_connect_errno()) { – Opening connect if statement checking to see if theres any login errors 
die(“Database connection failed: ” . – Error found so kill the site
mysqli_connect_error() .
” (” . mysqli_connect_errno() . “)”
);
}
?>

 

Include different files like the connect, header and footer, also can be used to create if statements to have 2 different index pages depending on a statement.

<?php require_once(“../includes/session.php”); ?> – loads in the Session file, if not found website will not load as it requires it
<?php require_once(“../includes/connect.php”); ?> – loads in the Connect file, if not found website will not load as it requires it
<?php require_once(“../includes/functions.php”); ?> – loads in the Functions file, if not found website will not load as it requires it
<?php include_once(“../includes/templates/header.php”); ?> – loads in the Header file, However if not found the site will still load and display the rest of the code
<?php if(isset($_SESSION[“user”])){ ?> – opening to the if statement checking whether the users session has been set, if so it will loads the code before the else statement
<?php include_once(“../includes/templates/nav.php”); ?> – loads in the Nav file, However if not found the site will still load and display the rest of the code
<?php } else { ?> – The Else statement will only load if the if statement is false
<?php include_once(“../includes/templates/footer.php”); ?> – loads in the Footer file, However if not found the site will still load and display the rest of the code

By doing this, it saves a lot of space with-in the index code making it easier to read and work with.

 

Fetch data from database

<?php
$_POST = “SELECT post_user_username, post_club, post_title, post_body, post_timestamp FROM post_posts ORDER BY post_id DESC”; – The Fetch function defining what from where
// database columom rows name —– database name
$result = mysqli_query($connection, $_POST); – Creating the $result function Query
// result query code
?>

<div class=”post” >
<?php while($row = mysqli_fetch_assoc($result)) { ?> – Display in row function (This is object orientated programming because I couldn’t work out how not to make it OOP)
<div id=”post-header”>
<span id=”name”><?php echo $row[“post_user_username”]; ?></span> – Echoing the Results from the DB
<span id=”club”><?php echo $row[“post_club”]; ?></span> – Echoing the Results from the DB
</div>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s