Jump to content

Integrating rating system into MySql table


blessidunionchick

Recommended Posts

I have two sets of codes that work perfectly.  One is for the table and the other is for the rating system.  I can't seem to figure out how to combine the two codes so that the rating system is displayed INSIDE the table instead of after it.  I'm not sure if it's possible, but I've seen it used on sites like Netflix.  If you can tell me how to fix it or give me a different suggestion I would be eternally thankful.

 

What I am trying to do ultimately is have a table that has a star rating associated with each entry without linking to a full page.

 

 

 

 

<link rel="stylesheet" type="text/css" href="style.css"><script type="text/javascript" src="/jquery_star.js"></script><script type="text/javascript" src="/script.js"></script>

 

 

<?php$con = mysql_connect("localhost","myhowd5_undrdg","indie500");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("myhowd5_jo151", $con);$result = mysql_query("SELECT * FROM jos_easytables_table_data_1");echo "<table border='1'><tr><th>Artist</th><th>Genre</th><th>Your Rating</th><th>Average Rating</th><th>Total Votes</th></tr>";while($row = mysql_fetch_array($result))  {  echo "<tr>";  echo "<td>" . $row['column0'] . "</td>";  echo "<td>" . $row['column1'] . "</td>";    echo "<td>" . $row['rating'] . "</td>";      echo "<td>" . $row['total_rating'] . "</td>";        echo "<td>" . $row['total_ratings'] . "</td>";  echo "</tr>";  }echo "</table>";mysql_close($con);?> 

 

 

 

<?phpmysql_connect("localhost", "myhowd5_undrdg", "indie500") or die(mysql_error());mysql_select_db("myhowd5_jo151") or die(mysql_error());$query = mysql_query("SELECT * FROM jos_easytables_table_data_1");while($row = mysql_fetch_array($query)) {    $rating = (int)$row[rating];    ?>    <div class="floatleft">        <div id="rating_<?php echo $row[id]; ?>">            <span class="star_1"><img src="/star_blank.png" alt="" <?php if($rating > 0) { echo"class='hover'"; } ?> /></span>            <span class="star_2"><img src="/star_blank.png" alt="" <?php if($rating > 1.5) { echo"class='hover'"; } ?> /></span>            <span class="star_3"><img src="/star_blank.png" alt="" <?php if($rating > 2.5) { echo"class='hover'"; } ?> /></span>            <span class="star_4"><img src="/star_blank.png" alt="" <?php if($rating > 3.5) { echo"class='hover'"; } ?> /></span>            <span class="star_5"><img src="/star_blank.png" alt="" <?php if($rating > 4.5) { echo"class='hover'"; } ?> /></span>        </div>    </div>    <div class="star_rating">        (Rated <strong><?php echo $rating; ?></strong> Stars)    </div>    <div class="clearleft"> </div>    <?php    }?>

 

Link to comment
Share on other sites

What's your databas structure? and CSS?

 

A problem I can see is you're casting the rating as an integer here:

$rating = (int)$row[rating]; // don't use $row[rating] use $row['rating'] which is faster.

You need to remove that cast (int) because it will be rounding down your rating e.g. 4.4 will be casted as 4.

 

I'd put the ratings in a seperate MySQL table and have: rating, id (id would be the id of the thing that you're rating, e.g. an article)

 

Then use SELECT AVG(rating) WHERE id = $id to find the average and COUNT() to find the total.

Link to comment
Share on other sites

Thank you for your reply but I don't think that is the problem.  In the javascript code it has a way of averaging the votes together.  The reason it is calling an integer is so it knows how many stars to highlight (as seen in the if/echo statements).

 

I have found a way to integrate the two codes together but I am having trouble now with the echo 'class="hover"'; lines.  They are calling a javascript class defined "hover" but I don't think it is executing properly with this code.  The stars show up in the table but they do not follow the javascript definitions and they are followed by " class="hover" ".

 

 

<link rel="stylesheet" type="text/css" href="style.css"><script type="text/javascript" src="/jquery_star.js"></script><script type="text/javascript" src="/script.js"></script><?php$con = mysql_connect("localhost","myhowd5_undrdg","indie500");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("myhowd5_jo151", $con);$result = mysql_query("SELECT * FROM jos_easytables_table_data_1");echo "<table border='1'><tr><th>Artist</th><th>Genre</th><th>Your Rating</th><th>Average Rating</th><th>Total Votes</th></tr>";while($row = mysql_fetch_array($result)) {    $rating = (int)$row[rating];  echo "<tr>";  echo "<td>" . $row['column0'] . "</td>";  echo "<td>" . $row['column1'] . "</td>"; echo "<td>" . $rating['rating'];echo "<img src='/images/star_blank.png'>";if($rating > 0); echo 'class="hover"';echo "<img src='/images/star_blank.png'>";if($rating > 1.5);echo 'class="hover"';echo "<img src='/images/star_blank.png'>";if($rating > 2.5);echo 'class="hover"';echo "<img src='/images/star_blank.png'>";if($rating > 3.5);  echo 'class="hover"';echo "<img src='/images/star_blank.png'>";if($rating > 4.5);echo  'class="hover"';echo "</td>";      echo "<td>" . $row['total_rating'] . " Stars" . "</td>";        echo "<td>" . $row['total_ratings'] . "</td>";  echo "</tr>";  }echo "</table>";?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.