I am creating a blog and I need to create a php file which retrieves data from a database (which I have done). This php file then outputs the data to html(where I need help on). The php and html must be separate files.
The two files are: - index.php : redirects to index.html for displaying blog entries. - index.html: displays blog entries stored in entry files, and redirect the user to login.html if there is not entry.
The echo on the php files outputs the data from the database on the index.php which I don't want. I want to somehow out the data from the database to index.html.
I'm a newbie at coding, so excuse the bad coding.
Thanks in advance.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "addentry";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, title, content, date FROM posts";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
header('location: index.html');
echo " " . $row["date"]. "<br>";
echo " " . $row["title"]."<br>";
echo " ". $row["content"];
}
}
else {
echo "0 results";
}
$conn->close();
?>
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- image.html
A trivial document
-->
<html xmlns = "http://www.w3.org/1999/xhtml">
<head><title> My Blog </title>
<style type ="text/css">
body{
position: fixed;
overflow:overlay;
width: 100%;
top: -20px;
left: -20px;
right: -40px;
bottom: -40px;
height: auto;
background-image:url(image.jpg);
background-size: cover;
}
.container{
background-color: #ecdad6;
padding: 30px;
width:920px;
margin-left: 25%;
padding-bottom:1000px;
padding-left:0px;
border: 2px solid black;
}
.links{
position: absolute;
padding-right: 135px;
padding-bottom: 800px;
margin-left: 680px;
margin-right: 100px;
font-size: 20px;
word-wrap: break-word;
top:-3px;
}
.blog{
position: absolute;
width:678px;
padding-bottom: 920px;
margin-left: 10px;
font-size: 20px;
text-align: left;
word-wrap: break-word;
}
ul li { margin-top: -10px; }
}
}
</style>
<body>
<!--Logo & hyperlinked -->
<p align = "center"><a href="index.html"><img src = "Logo.jpg" alt="My logo" width="10%" height="10%"/></a></p>
<br/>
<hr width="50%">
<div class="container">
<div class="blog">
<form action='index.php' method='get'></form>
<div class="links">
<a href="index.html"> <ul><li>home</li></ul></a>
<a href="login.html"> <ul><li>logIn</li></ul></a>
<a href="entry.html"> <ul><li>add_entry</li></ul></a>
</div>
</div>
</div>
</body>
</html>
<!-- end snippet -->