Reputation: 85
I've a class project that I am working on that I would like to add database functionality. My PHP skills are less than par and my deadline is short. I have a database I used elsewhere that I would like to use again but I ran into an issue. I have the HTML done for the site but have no idea how to use my DB with it. The old one ran from index.php
and my new page is in html.Is it possible to grab the old file and use it inside my html like with MVC views or just add it to the page itself? I don't mind doing the research but I am not really sure what I am looking for.
-Thanks.
Upvotes: 1
Views: 2545
Reputation: 98861
PHP
generates HTML
and not the other way around.
As a quick solution I would rename the .html
files to .php
and use the existing html with the php heredoc
function, ex:
<?php
echo <<< EOF
your html code here
EOF;
?>
if you need to connect to a database, you can append the db output to the existing html, like this:
<?php
$db = new mysqli('localhost', 'user', 'pass', 'demo');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
echo <<< EOF
first part of the html here
EOF;
//Queries the DB and outputs the results of all **employees**
$result = $db->query("SELECT id, name, salary FROM employees");
while (list($id, $name, $salary) = $result->fetch(PDO::FETCH_NUM)) {
echo <<< EOF
<tr>
<td><a href="info.php?id=$id">$name</a></td>
<td>$salary</td>
</tr>
EOF;
}
echo <<< EOF
second part of the html here
EOF;
//and so on...
?>
Make sure you run the code on a server that supports php.
Upvotes: 2
Reputation: 45124
According what I think It's all about code readability. But this could be vary depending on the situation. This has been already answered.
Escape HTML to PHP or Use Echo? Which is better?
If you have any questions regrading this don't hesitate to make a comment.
Upvotes: 1