-1

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;

}
?>
Grant
  • 2,413
  • 2
  • 30
  • 41
Nawaz
  • 1
  • 1
  • **Your code is vulnerable to SQL injection and will be hacked** even if [you are escaping inputs!](https://stackoverflow.com/a/5741264/2595450) Use [Prepared Statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead. – Spoody Apr 10 '18 at 18:11
  • i am new at this forum. don't do this. – Nawaz Apr 10 '18 at 18:19
  • 1
    I'm not sure what you mean. – Spoody Apr 10 '18 at 18:19
  • What does "move down" mean? – yaakov Apr 10 '18 at 18:21
  • @YaakovAinspan , position of rating star automatically goes down. – Nawaz Apr 10 '18 at 18:25
  • Position in terms of what? Like where it is on the page? – yaakov Apr 10 '18 at 18:26
  • @Yaakiov Ainspan i am trying to fix it from previous 10 hour but can't . – Nawaz Apr 10 '18 at 18:27
  • What are you trying to fix? There is very little description of what the problem is in the question, so please explain exactly what the problem is. – yaakov Apr 10 '18 at 23:16
  • Welcome to Stack Overflow! Stack Overflow is not a discussion forum, it is a Question & Answer site where you can ask a **specific** programming question that **can be answered** rather than discussed. Questions seeking debugging help ("why isn't this code working?") must include the **desired behavior**, a **specific problem or error** and the shortest code necessary to reproduce it in the question itself. Questions without a **clear problem statement** are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – NightOwl888 Apr 11 '18 at 03:24

1 Answers1

1

If you want to calculate the AVG in php, change :

$total=0;
$avg=0;
$count=0;

while($row=mysqli_fetch_array($res1))
   {
     echo "<br>";
     $count+=1;
     $avg+=$row['Rating'];
     $result=$avg/$count;
   }

To :

$totalRate = 0;
$totalRows = $res1->num_rows;

while($row=mysqli_fetch_array($res1))
   $totalRate += $row['Rating'];

$avg = $totalRate / $totalRows;

You can also calculate directly the AVG by using the mysql AVG() function with one query https://www.w3schools.com/sql/func_mysql_avg.asp

Sébastien S.
  • 1,444
  • 8
  • 14
  • i am not want to calculate average. my problem is this when i call this funtion in comments.php file rating star move automatically down . i want to fix this postilion of this stars. @Sebastien S – Nawaz Apr 10 '18 at 18:21
  • Your function seems to wrongly calculate the AVG... If you're just looking to fix a problem of graphical position going down while calling this function, it's probably because of the `echo "
    ";` in the `while` loop. Then remove this line.
    – Sébastien S. Apr 10 '18 at 18:27
  • my function calculating average 100% true. i have many time test it. please focus on my problem not on average. – Nawaz Apr 10 '18 at 18:29
  • I told you for your problem... Remove `echo "
    "` and the position won't go down. Your method of average in another hand works only if you have just one result. And in this case you don't need the `while` loop.
    – Sébastien S. Apr 10 '18 at 18:34
  • Thanks dear @Sebastien S . I have founded my mistake. with the help you. your are great guys.. – Nawaz Apr 10 '18 at 18:39
  • You're welcome, feel free to accept my answer by upvoting it if it helped you. – Sébastien S. Apr 10 '18 at 18:41
  • i am new at this forum i don't know how to use this forum. tell me how i can up-voting you here. – Nawaz Apr 10 '18 at 18:44
  • S , how to tag some one at this forum and how highligted some word like you done in above comments. – Nawaz Apr 10 '18 at 18:46
  • This is where to click to upvote : https://imgur.com/a/UQ4ys and there you can find all the information about tagging, highlighting, bolding, etc : https://stackoverflow.com/editing-help – Sébastien S. Apr 10 '18 at 18:53