-1

I need fa fa-star class's mouseenter, mouseout, and onclick function work for each specific 'list' class/li tag(like list class[0, 1, 2, etc]). What I have now is working for every 'list' class. When I mouseenter on a star from different list class the 1st list class's star is changing.

If I mouseenter on a star from 1st list class/li tag the mouseenter function should work for that list class/li tag not for other 'list' class/li tag. if 2nd 'list' class then it should work for that 'list' class/li tag not for others. and for 3rd, 4th etc.

in rateclick function I can send the rating to server but can't fetch it. Can you look into it also?


Here is the code:

index.php and style.css

.fa{
    font-size:25px;
   }
   
 
   
li{
  list-style:none;
  padding-left:40px;
  padding-bottom:40px;
  border-bottom:2px solid #ccc;
  width:800px;
 }
 
li:last-child{
             border-bottom:none;
   }
   
  
<?php

require "conx.php";

$sql = "SELECT * FROM customers ORDER BY id ASC";

$query = $conx -> query($sql);

$numrows = $query -> num_rows;

?>

<!DOCTYPE html>

<html>

<link rel="stylesheet" href="css/font-awesome.min.css">

<link rel="stylesheet" href="style.css">

<script src="jquery.min.js"></script>

<body>

<div class="Insert-Into">

<ul>

<?php if($numrows > 0): ?>

<?php while($row = $query -> fetch_assoc()): ?>

<?php $rating = $row["rating"]; ?>

<?php $customerid = $row["id"]; ?>

<li data-rating=<?php echo "$rating"; ?> data-customerid=<?php echo $customerid; ?> class="list">

<br/>
<br/>

<span class="username"><?php echo $row["username"]; ?></span>

<br/>
<br/>

<?php

$i;

$color;

for($i = 1; $i <= 5; $i++){
 
 if($i <= $rating){
  
  $color = "color:orange";
  
 }
 else{
  
  $color = "color:#ccc";
  
 }
 
 echo "<i class='fa fa-star' style='$color;' data-rating='$i' onmouseenter='mouseenterrating($i)' onmouseout='clearstar($rating)' onclick='rateclick($customerid, $i)'></i>";
}

?>

<br/>
<br/>

<b>Description</b>

<br/>
<br/>

<div class="desc"><?php echo $row["description"]; ?></div>

</li>



<?php endwhile; ?>

<?php endif; ?>

</ul>

</div>

<script>
function mouseenterrating(count){
 
 var j;
 
 var fa = document.getElementsByClassName("fa");
 

 
 for(j = 0; j < fa.length; j++){
          
  if(j < count){
      
   fa[j].style.color = "orange";
      
  }
  else{
      
   fa[j].style.color = "#ccc";
      
  }   
    
 }
 

  
}

function clearstar(rating){
 
 var fa = document.getElementsByClassName("fa");
 
 var k;
 
 for(k = 0; k < fa.length; k++){
  
  if(k < rating){
   
   fa[k].style.color = "orange";
   
  }
  else{
   
   fa[k].style.color = "#ccc";
   
  }
  
 }
 
}

function rateclick(customerid, count){
 
 var xhttp = new XMLHttpRequest();
 
 xhttp.open("POST", "rating.php?id="+customerid+"&rate="+count);
 
 xhttp.send();
 
 fetch(customerid);
 
}

function fetch(e){
 
 var xhttp = new XMLHttpRequest();
 
 xhttp.open("GET", "fetch.php");
 
 xhttp.send();
 
}
</script>

</body>

</html>

rating.php

<?php

require "conx.php";

if(isset($_GET['id']) && isset($_GET['rate'])){

$customerid = $_GET['id'];

$rate = $_GET['rate'];

$sql = "UPDATE customers SET rating = '$rate' WHERE id = '$customerid'";

$result = $conx -> query($sql);

}

?>

fetch.php

<?php

require "conx.php";

$sql = "SELECT * FROM customers WHERE id = '".customerid."'";

$result = $conx -> query($sql);

$fetch = $result -> fetch_assoc();

?>

conx.php

<?php

$conx = mysqli_connect("localhost", "root", "");

if(!$conx){
 
 echo "Not connected with localhost";
 
}

$sql = "CREATE DATABASE rating";

$query = $conx -> query($sql);

$dbselect = $conx -> select_db("rating");

if(!$dbselect){
 
 echo "No database selected";
 
}

$sql = "CREATE TABLE customers(id INT(255) NOT NULL AUTO_INCREMENT UNIQUE KEY, username VARCHAR(200) NOT NULL, description VARCHAR(1000) NOT NULL, email VARCHAR(200) NOT NULL PRIMARY KEY, rating INT(5) NOT NULL)";

$query = $conx -> query($sql);

?>
AL-RAHAT
  • 23
  • 6
  • Have you tried to implement it yourself? And maybe you could try and eliminate irrelevant code and just focus on the actual features you need help for? – HelgeFox Sep 30 '18 at 10:37
  • And also you should try googling your problem before posting on stackoverflow. Hint: try googling `css change color when mouse over` – HelgeFox Sep 30 '18 at 10:39
  • @ HelgeFox, I tried but could not able to do it. Can you help me on this? – AL-RAHAT Sep 30 '18 at 10:42
  • Well, your question is not very clear. It sounds like you need a simple `:hover` effect that could easily be solved by CSS. But you seem ademant that someone else should implement the two functions you have defined. I think you should at least show your effort, before demanding a solution to be implemented a certain way. – HelgeFox Sep 30 '18 at 10:48
  • @HelgeFox, You can use css. – AL-RAHAT Sep 30 '18 at 10:55
  • Here is another stackoverflow question that I think is perfect for your problem: https://stackoverflow.com/questions/29530680/how-can-i-do-the-hover-effect-like-a-rating-star-in-this-code-css-html – HelgeFox Sep 30 '18 at 11:07
  • @HelgeFox, Also I have added the code what I had in two functions at past. In the mouseenterrating function, When I mouseenter it shows me stars as I wanted but it is working for every list/li tag. and the code is not properly correct for both functions. currentstar function is at variance with what I want and You can say it's totally wrong. You may get some idea by seeing the code. – AL-RAHAT Sep 30 '18 at 11:13
  • Have you looked at my answer yet? – HelgeFox Sep 30 '18 at 13:21
  • If my answer helped could you please mark it as 'accepted'? – HelgeFox Oct 01 '18 at 13:02
  • @HelgeFox, I will mark it soon but need some few changes. I have updated my code and information. Please read the question again. – AL-RAHAT Oct 02 '18 at 11:22
  • I'm sorry, this is not how stackoverflow is supposed to be used. You ask one question, preferably as specific as possible, then you get an answer for that question. Then you create another question and get answers on that. – HelgeFox Oct 02 '18 at 11:43
  • Your question was not very clear from the beginning and now it is even more messy with additional PHP code that I can not answer for you. – HelgeFox Oct 02 '18 at 11:45

1 Answers1

0

Building on your initial attempt I have this solution for you:

function onMouseEnter(index){
   var fa = document.getElementsByClassName("fa");
   var j;
   for(j = 0; j < fa.length; j++){
       if(j < index){
           fa[j].style.color = "orange";
       }
       else{
           fa[j].style.color = "#ccc";
       }
   }
}
function onMouseOut(){
    var fa = document.getElementsByClassName("fa");
    var k;
    for(k = 0; k < fa.length; k++){
        fa[k].style.color = "#ccc";
    }
}

Demo

HelgeFox
  • 349
  • 2
  • 13
  • @ HelgeFox, Thank you for your answer but I need working the code for each 'list' class/li tag. Also I can't fetch the rating from rateclick function. Please have a look into it. – AL-RAHAT Oct 02 '18 at 11:28
  • Okay. Somehow I solved this but can't fetch the rating from server with ajax code. Anyway, this answer was also helpful and I am marking this answer as accepted. – AL-RAHAT Oct 03 '18 at 04:06