I am trying to build a website and i have my html with a menu and a scroll down content.
I am trying to add a "star review" part and I have an example in php connected to a database that i imported in my localhost. The thing is, i know php does not work in html but is there any way I can call in my html the php file and the content of the php file will appear in my website?
I tried to put in my html the php content, and everything is displayed in my website but it is not working. I created a .htaccess file with the line AddType application/x-httpd-php .htm .html in it. I think the config.php which is connectig to my database is not working. I hope I made myself clear. I will put some code here now, to make a clear image of my project
<!DOCTYPE html>
<! This is the star rating code in a php file which i am trying to implement in my html website>
<html>
<head>
<meta charset="utf-8" />
<title>Star Rating</title>
<link type="text/css" rel="stylesheet" href="style.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<h1 style="text-align:center; color:#fff; margin:50px; margin-bottom:10px; font-size:36px">Star Rating System Using PHP,Ajax</h1>
<p style="text-align:center; color:#fff;font-size:20px; margin:0px">Including Update Rating</p>
<?php
include('config.php');
$post_id = '1'; // yor page ID or Article ID
?>
<div class="container">
<div class="rate">
<div id="1" class="btn-1 rate-btn"></div>
<div id="2" class="btn-2 rate-btn"></div>
<div id="3" class="btn-3 rate-btn"></div>
<div id="4" class="btn-4 rate-btn"></div>
<div id="5" class="btn-5 rate-btn"></div>
</div>
<br>
<div class="box-result">
<?php
$query = mysql_query("SELECT * FROM star");
while($data = mysql_fetch_assoc($query)){
$rate_db[] = $data;
$sum_rates[] = $data['rate'];
}
if(@count($rate_db)){
$rate_times = count($rate_db);
$sum_rates = array_sum($sum_rates);
$rate_value = $sum_rates/$rate_times;
$rate_bg = (($rate_value)/5)*100;
}else{
$rate_times = 0;
$rate_value = 0;
$rate_bg = 0;
}
?>
<div class="result-container">
<div class="rate-bg" style="width:<?php echo $rate_bg; ?>%"></div>
<div class="rate-stars"></div>
</div>
<p style="margin:5px 0px; font-size:16px; text-align:center">Rated <strong><?php echo substr($rate_value,0,3); ?></strong> out of <?php echo $rate_times; ?> Review(s)</p>
</div>
</div>
<script>
$(function(){
$('.rate-btn').hover(function(){
$('.rate-btn').removeClass('rate-btn-hover');
var therate = $(this).attr('id');
for (var i = therate; i >= 0; i--) {
$('.btn-'+i).addClass('rate-btn-hover');
};
});
$('.rate-btn').click(function(){
var therate = $(this).attr('id');
var dataRate = 'act=rate&post_id=<?php echo $post_id; ?>&rate='+therate; //
$('.rate-btn').removeClass('rate-btn-active');
for (var i = therate; i >= 0; i--) {
$('.btn-'+i).addClass('rate-btn-active');
};
$.ajax({
type : "POST",
url : "ajax.php",
data: dataRate,
success:function(){}
});
});
});
</script>
</body>
</html>
<!This is the config.php - the connection to my database that is not working>
<?php
//change the values with your own hosting setting
$mysql_host = "localhost";
$mysql_database = "test";
$mysql_user = "root";
$mysql_password = "";
$db = mysql_connect($mysql_host,$mysql_user,$mysql_password);
mysql_connect($mysql_host,$mysql_user,$mysql_password);
mysql_select_db($mysql_database);
?>
<!ajax.php>
<?php
require_once 'config.php';
if($_POST['act'] == 'rate'){
$ip = $_SERVER["REMOTE_ADDR"];
$therate = $_POST['rate'];
$thepost = $_POST['post_id'];
$query = mysql_query("SELECT * FROM star where ip= '$ip' ");
while($data = mysql_fetch_assoc($query)){
$rate_db[] = $data;
}
if(@count($rate_db) == 0 ){
mysql_query("INSERT INTO star (id_post, ip, rate)VALUES('$thepost', '$ip', '$therate')");
}else{
mysql_query("UPDATE star SET rate= '$therate' WHERE ip = '$ip'");
}
}
?>
Now for the database I am using XAMPP
I tried to copy the content of the star rating php file in my html file, everything is showing properly but it does not calculate the star rating and I think the problem is either it is not connected to my database or the problem is that I am using php in my html file.
Thanks a lot