php - 元素位置固定在 PHP 中

标签 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;

}
?>

最佳答案

如果你想在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

关于php - 元素位置固定在 PHP 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49760360/

相关文章:

javascript - 使用本地 AJAX 请求解析 JSON 数据

javascript - 如何在javascript中搜索列表中的元素?

html - 是否有一篇文章详细介绍外行的表格与 css 设计?

html - 滚动问题滚动不起作用

css - 内联样式无法与侧边菜单一起使用

css - 多级响应导航工具/教程

html - 容器和文章元素之间的神秘差距

php - 无法让 WordPress 显示多站点中的所有类别

php - 是否有用于交换两个变量值的 PHP 函数?

php - 内容未发布到数据库但收到成功消息