简体   繁体   English

完善星级评定系统

[英]Improving a star rating system

I made a star rating system for a school project and need some help improving it a bit. 我为一个学校项目制作了星级评分系统,需要一些帮助对其进行一些改进。 It's a fairly simple setup: index.php contains list-items (content is fetched from a DB) with an image and a div holding the stars for rating. 这是一个非常简单的设置:index.php包含列表项(内容是从数据库中获取的),其中包含一个图像和一个div,其中包含用于评级的星号。 Each star is a link that triggers a function which saves the rating in a database. 每个星号都是触发一个函数的链接,该函数将评级保存在数据库中。

Here's the link ! 这是链接 for starters. 对于初学者。

If you click on any star, the first click will result in a green check mark on the first list-item. 如果单击任何星号,则第一次单击将在第一个列表项上显示一个绿色的复选标记。 The idea is that this check mark will appear on each list-item when rated. 想法是,此复选标记在评级时将出现在每个列表项上。 That's where I need you guys to point me in the right direction. 那就是我需要你们为我指出正确方向的地方。 First of all I know I can't echo out multiple divs with the same id, but I had to in order for the ajax function to work (document.getElementById("rated")). 首先,我知道我无法回显具有相同id的多个div,但是为了使ajax函数正常工作,我不得不这样做(document.getElementById(“ rated”))。 Any ideas on how to solve this? 关于如何解决这个问题的任何想法?

CODE

insert_receive.php: insert_receive.php:

<?php
  session_start();
  $sess = session_id();

  $mysqli = new mysqli("", "", "", "");
  if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
 }

$stmt = $mysqli->prepare("REPLACE INTO Ratings (projId_fkey, sessionId, rvalue) VALUES ('".$_GET['projId']."','".$sess."','".$_GET['rating']."')");
$stmt->execute();

printf("✔");
?>

ajax_framework.js: ajax_framework.js:

function saveClick(rating,projId)
{

var xmlhttp;

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("rated").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","insert_receive.php?rating=" + rating + "&projId=" + projId,true);
xmlhttp.send("");
}

index.php: (the part that matters) index.php :(重要部分)

<?php

$select = "SELECT id, projName, location FROM Projects";

if($result = $mysqli->query($select))
{
    while($row = $result->fetch_assoc())
    {
        echo '<li id="'.$row['id'].'">';
        echo '<h1 class="header">'.$row['projName']."</h1>";
        echo '<img src="'.$row['location'].'" alt=""/>';
        echo '<div class="rating">';
        echo '<a href="#" onclick="saveClick(5, '.$row['id'].')">'.★★★★★."</a>";
        echo '<a href="#" onclick="saveClick(4, '.$row['id'].')">'.★★★★."</a>";
        echo '<a href="#" onclick="saveClick(3, '.$row['id'].')">'.★★★."</a>";
        echo '<a href="#" onclick="saveClick(2, '.$row['id'].')">'.★★."</a>";
        echo '<a href="#" onclick="saveClick(1, '.$row['id'].')">'.★."</a>";
        echo '<div id="rated">'.""."</div>";
        echo "</div>";
        echo "</li>";
    }
}
?>

You can use an iterator to give each element a different ID, after that, you can indeed dynamically get the element by the correct ID. 您可以使用迭代器为每个元素赋予不同的ID,之后,您实际上可以通过正确的ID动态获取元素。 For example: 例如:

echo '<div id="rated'.$row['id'].'">

and then: 接着:

document.getElementById("rated"+projId).innerHTML=xmlhttp.responseText;

This will dynamically select the element you want. 这将动态选择所需的元素。

Regarding your code in general, I simply must point you in the direction of http://jquery.com/ It will make element selection much simpler, and will also normalize your use of ajax. 一般而言,关于您的代码,我只是必须将您指向http://jquery.com/的方向,这将使元素选择更加简单,并且还将规范您对ajax的使用。

Change your php to: 将您的PHP更改为:

while($row = $result->fetch_assoc())
{
    echo '<li id="'.$row['id'].'">';
    echo '<h1 class="header">'.$row['projName']."</h1>";
    echo '<img src="'.$row['location'].'" alt=""/>';
    echo '<div class="rating">';
    echo '<a href="#" onclick="saveClick(5, '.$row['id'].')">'.★★★★★."</a>";
    echo '<a href="#" onclick="saveClick(4, '.$row['id'].')">'.★★★★."</a>";
    echo '<a href="#" onclick="saveClick(3, '.$row['id'].')">'.★★★."</a>";
    echo '<a href="#" onclick="saveClick(2, '.$row['id'].')">'.★★."</a>";
    echo '<a href="#" onclick="saveClick(1, '.$row['id'].')">'.★."</a>";
    echo '<div id="rated'.$row['id'].'">'.""."</div>";//updated this line
    echo "</div>";
    echo "</li>";
}

And the line in your javascript to: 并在您的JavaScript中执行以下操作:

document.getElementById("rated"+projId).innerHTML=xmlhttp.responseText;

Use a counter $id to increment li id 使用计数器$id递增li id

<?php

$select = "SELECT id, projName, location FROM Projects";

if($result = $mysqli->query($select))
{
$id = 0;
while($row = $result->fetch_assoc())
{
    echo '<li id="'.$id.'">';
    echo '<h1 class="header">'.$row['projName']."</h1>";
    echo '<img src="'.$row['location'].'" alt=""/>';
    echo '<div class="rating">';
    echo '<a href="#" onclick="saveClick(5, '.$row['id'].')">'.★★★★★."</a>";
    echo '<a href="#" onclick="saveClick(4, '.$row['id'].')">'.★★★★."</a>";
    echo '<a href="#" onclick="saveClick(3, '.$row['id'].')">'.★★★."</a>";
    echo '<a href="#" onclick="saveClick(2, '.$row['id'].')">'.★★."</a>";
    echo '<a href="#" onclick="saveClick(1, '.$row['id'].')">'.★."</a>";
    echo '<div id="rated">'.""."</div>";
    echo "</div>";
    echo "</li>";
    $id++;
}
}
?>

First, use a class instead, obviously. 首先,显然要使用类。

echo '<div class="rated">'.""."</div>";

Second, in your JavaScript, instead of this line 其次,在您的JavaScript中,而不是这一行

document.getElementById("rated").innerHTML=xmlhttp.responseText;

Use this 用这个

var proj = document.getElementById(projId);
var rated = proj.getElementsByClassName('rated')[0];
rated.innerHTML=xmlhttp.responseText;

What we're doing here is that we find the element with an id matching projId . 我们在这里所做的是找到一个ID匹配projId的元素。 Then we find the elements with the class rated INSIDE the projID we found earlier. 然后,我们找到类别rated INSIDE的类,这些元素projID我们之前发现的projID Now notice that getElementsByClassName returns an array regardless of the number of elements, but since we only have a single one here we just select the item at the first index [0] . 现在请注意,不管元素的数量如何, getElementsByClassName返回一个数组,但是由于这里只有一个,所以我们只选择第一个索引[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM