I have created a star rating system. Where the first function inserts data in databases, second function shows popup and extract word from comments area and then set specific rating, third function takes average of all rating store in database. My problem is this, when I call average function rating star automatically move down. Please help me in following code to find my mistake or coding weakness. I will be thankful if guys can help me.
<!Doctype html>
<html>
<head>
<title>comments systems</title>
</head>
<body>
<h1 >this is comments system</h1>
<?php
include 'comments1.php';
echo"<form method='post' action='".set()."'>
<label>Email</label>
<input type='email' name='email' placeholder='Your mail..' required >
<br>
<label for='subject'>Subject</label>
<textarea name='mess' placeholder='this is text' rows='5' cols='50' required></textarea>
<input type='submit' name='Submit'>
</form>";
get();//message and mail taking
rating();
// echo
$rating=average();
$resrat=round($rating);
echo"<div style='margin-left: 115px; margin-top: 2370px;'>";
echo"---";
for($i=1; $i<=$resrat; $i++) // simple for loop
{
echo '<img src="Star (Full).png" width="100"/>';
}
echo"</div>";
?>
</body>
</html>
//2nd file
<?php
session_start();
function set()
{
$Mail = isset($_POST['email']) ? $_POST['email'] : '';
$MESS=isset($_POST['mess']) ? $_POST['mess'] : '';
$conn=mysqli_connect('localhost','root');
mysqli_select_db($conn,'reg');
if(!empty($Mail && $MESS))
{
$sql="INSERT INTO Persons (email,message,curdate)VALUES ('$Mail', '$MESS',now())";//ID is auto increment due to this not mention.
if(mysqli_query($conn,$sql))
{
//echo "data submited";Nothing do here
}
else {
//echo "data not submited";
echo "<link rel='stylesheet' type='text/css' href='sweet-alert.css''>";
echo "<script src='sweet-alert.js'></script>";
echo '<script type="text/javascript">';
echo 'setTimeout(function () {swal("Data Not Submited!");';
echo '}, );</script>';
}
}
}
function get(){
$con=mysqli_connect('localhost','root');
mysqli_select_db($con,'reg');
$page=$_GET["page"]; //this page is that link where we click on
if($page==" "||$page=="1")
{
$pageget=0;
}
else
{
$pageget= ($page*5)-5;
}
if($con)
{
$sql="SELECT * FROM Persons LIMIT $pageget, 5";
$res=mysqli_query($con,$sql);
while($row=mysqli_fetch_array($res))
{
echo date("Y/m/d");
echo "<br>";
echo $row['message'];
echo "<br>";
echo "<hr>";
}
$sql="SELECT * FROM Persons";
$res1=mysqli_query($con,$sql) or die(mysqli_error($con));
$cnt=mysqli_num_rows($res1);//cnt=counting
echo "<br>";
$totalpages=floor($cnt/5);
//echo $totalpages;
$current_page = isset($_GET['page']) ? $_GET['page'] : 1;
if( $current_page > 1 )
{
$prev = '<a href="comments.php?page='.($current_page-1).'"><div style="border-style: groove; float:left " >Previous</div></a>';
echo $prev;
}
for($page=1;$page<=$totalpages;$page++)
{
?>
<a href="comments.php?page=<?php echo $page." ";?>" style="text-decoration:none text-align:center;"><?php echo '<div style="border-style: groove; width:25px;float:left" >'. $page." ".'</div>'; ?></a><?php
}
if( $current_page < $totalpages )
{
$next = '<a href="comments.php?page='.($current_page+1).'"><div style="border-style: groove;float:left" >Next</div></a>';
echo $next;
}
}
}
function rating()
{
$Mail=isset($_POST['email']) ? $_POST['email'] : '';
$MESS=isset($_POST['mess']) ? $_POST['mess'] : '';
$conn=mysqli_connect('localhost','root');
mysqli_select_db($conn,'reg');
//if(empty($Mail && $MESS)){echo "sorry";}
if(!empty($Mail && $MESS))
//else
{
echo "<br>";
$Email=$Mail;
// echo $Email;
$string=$MESS;
//FIRST
if (strpos($string, 'VERY GOOD') ||strpos($string, 'very good')||strpos($string, 'spacious')||strpos($string, 'Bravo') ||strpos($string, 'Very Good')||strpos($string, 'bravo')!== false)
{
//echo '3';
// echo
$rating=4;
$sql="UPDATE Persons SET Rating=$rating WHERE email='$Email';"; // if remove this '' from varible'$Email' it not work becacuse it was string.
if(mysqli_query($conn,$sql))
{
//echo "yes added rating";
echo "<link rel='stylesheet' type='text/css' href='sweet-alert.css''>";
echo "<script src='sweet-alert.js'></script>";
echo '<script type="text/javascript">';
echo 'setTimeout(function () {swal("Your Feedback Considered = 4 Star!");';
echo '}, );</script>';
}
else
{
// echo " not rating added".error;
echo "<link rel='stylesheet' type='text/css' href='sweet-alert.css''>";
echo "<script src='sweet-alert.js'></script>";
echo '<script type="text/javascript">';
echo 'setTimeout(function () {swal("Your Feedback Not Considerd!");';
echo '}, );</script>'.error;
}
}
//SECOND
else if(strpos($string, 'GOOD') ||strpos($string, 'good')||strpos($string, 'not bad')||strpos($string, 'Not Bad')!== false)
{
// echo "your feed back has no our required words! at least one is required";
//echo
$rating=3;
$sql="UPDATE Persons SET Rating=$rating WHERE email='$Email';"; // if remove this '' from varible'$Email' it not work becacuse it was string.
if(mysqli_query($conn,$sql))
{
echo "<link rel='stylesheet' type='text/css' href='sweet-alert.css''>";
echo "<script src='sweet-alert.js'></script>";
echo '<script type="text/javascript">';
echo 'setTimeout(function () {swal("Your Feedback Considered = 3 Star!");';
echo '}, );</script>';
}
else
{
echo "<link rel='stylesheet' type='text/css' href='sweet-alert.css''>";
echo "<script src='sweet-alert.js'></script>";
echo '<script type="text/javascript">';
echo 'setTimeout(function () {swal("Your Feedback Not Considerd!");';
echo '}, );</script>'.error;
}
}
//THIRD
else if(strpos($string, 'Bad') ||strpos($string, 'bad')||strpos($string, 'BAD')!== false)
{
// echo "your feed back has no our required words! at least one is required";
//echo
$rating=2;
$sql="UPDATE Persons SET Rating=$rating WHERE email='$Email';"; // if remove this '' from varible'$Email' it not work becacuse it was string.
if(mysqli_query($conn,$sql))
{
echo "<link rel='stylesheet' type='text/css' href='sweet-alert.css''>";
echo "<script src='sweet-alert.js'></script>";
echo '<script type="text/javascript">';
echo 'setTimeout(function () {swal("Your Feedback Considered = 2 Star!");';
echo '}, );</script>';
}
else
{
echo "<link rel='stylesheet' type='text/css' href='sweet-alert.css''>";
echo "<script src='sweet-alert.js'></script>";
echo '<script type="text/javascript">';
echo 'setTimeout(function () {swal("Your Feedback Not Considerd!");';
echo '}, );</script>'.error;
}
}
//FORTH
else if(strpos($string, 'very bad') ||strpos($string, 'VERY BAD')||strpos($string, 'so bad')||strpos($string, 'Very Bad')||strpos($string, 'So Bad')!== false) {
// echo "your feed back has no our required words! at least one is required";
//echo
$rating=1;
$sql="UPDATE Persons SET Rating=$rating WHERE email='$Email';"; // if remove this '' from varible'$Email' it not work becacuse it was string.
if(mysqli_query($conn,$sql))
{
echo "<link rel='stylesheet' type='text/css' href='sweet-alert.css''>";
echo "<script src='sweet-alert.js'></script>";
echo '<script type="text/javascript">';
echo 'setTimeout(function () {swal("Your Feedback Considered = 1 Star!");';
echo '}, );</script>';
}
else {
echo "<link rel='stylesheet' type='text/css' href='sweet-alert.css''>";
echo "<script src='sweet-alert.js'></script>";
echo '<script type="text/javascript">';
echo 'setTimeout(function () {swal("Your Feedback Not Considerd!");';
echo '}, );</script>'.error;
}
}
//FIFTH
else if(strpos($string, 'excellent') ||strpos($string, 'EXCELLENT')||strpos($string, 'Perfect')||strpos($string, 'Excellent')||strpos($string, 'perfect')!== false) {
// echo "your feed back has no our required words! at least one is required";
//echo
$rating=5;
$sql="UPDATE Persons SET Rating=$rating WHERE email='$Email';"; // if remove this '' from varible'$Email' it not work becacuse it was string.
if(mysqli_query($conn,$sql))
{
echo "<link rel='stylesheet' type='text/css' href='sweet-alert.css''>";
echo "<script src='sweet-alert.js'></script>";
echo '<script type="text/javascript">';
echo 'setTimeout(function () {swal("Your Feedback Considered = 5 Star!");';
echo '}, );</script>';
}
else {
echo "<link rel='stylesheet' type='text/css' href='sweet-alert.css''>";
echo "<script src='sweet-alert.js'></script>";
echo '<script type="text/javascript">';
echo 'setTimeout(function () {swal("Your Feedback Not Considerd!");';
echo '}, );</script>'.error;
}
}
//6th
else if(strpos($string, 'excellent') ||strpos($string, 'good')||strpos($string, 'Perfect')||strpos($string, 'bad')||strpos($string, 'perfect')!== true) {
echo $rating=1;
$sql="UPDATE Persons SET Rating=$rating WHERE email='$Email';"; // if remove this '' from varible'$Email' it not work becacuse it was string.
if(mysqli_query($conn,$sql))
{
echo "Rating Considered as low, Please use relevent words";
}
}
}
}
function average()
{
//average calculate
$con=mysqli_connect('localhost','root');
mysqli_select_db($con,'reg');
$sql="SELECT * FROM Persons";
$res1=mysqli_query($con,$sql);
$avg=0;
$count=0;
while($row=mysqli_fetch_array($res1))
{
echo "<br>";
$count+=1;
// echo $count
$avg+=$row['Rating'];
$result=$avg/$count;
}
return $result;
}
?>