元素位置用PHP修复

时间:2018-04-10 18:09:12

标签: php html css

我创建了一个星级评分系统。第一个函数在数据库中插入数据时,第二个函数显示弹出并从注释区域中提取单词然后设置特定的评级,第三个函数取数据库中所有评级存储的平均值。我的问题是这个,当我打电话给平均功能评级星时自动向下移动。请帮我跟踪代码,找出我的错误或编码缺陷。如果有人能帮助我,我将感激不尽。

<!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>

//第二个文件

<?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;

}
?>

1 个答案:

答案 0 :(得分:1)

如果你想在php中计算AVG,请更改:

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

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

致:

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

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

$avg = $totalRate / $totalRows;

您还可以通过一个查询使用mysql AVG()函数直接计算AVG https://www.w3schools.com/sql/func_mysql_avg.asp