471,593 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,593 software developers and data experts.

storing star ratings

Hi,

I need to store the star rated values in mysql. How will I do it. This is the code I have used to create the star rater,

<ul class="star-rating small-star">
<li class="current-rating" style="width:50%">Currently 2.5/5 Stars.</li>
<li><a href="#" title="1 star out of 5" class="one-star">1</a></li>
<li><a href="#" title="2 stars out of 5" class="two-stars">2</a></li>
<li><a href="#" title="3 stars out of 5" class="three-stars">3</a></li>
<li><a href="#" title="4 stars out of 5" class="four-stars">4</a></li>
<li><a href="#" title="5 stars out of 5" class="five-stars">5</a></li>
</ul>

and I need to get the rated values(like 1 star or 2 star) and store that in mysql using php. Once the user rates a picture for example 4 stars, how will I get that value into php variable and store it in mysql. I am totally new to this concept. Can anybody give me some idea.

With regards
Feb 13 '08 #1
5 2344
ronverdonk
4,258 Expert 4TB
See this little example named A.PHP.
It gives assigns a HREF link to each star rating. When the user clicks it will call itself passing the choosen rating number. In the second pass you can pick up that rating from the $_GET array and store it in the database.
NOTE!!! in order to prevent any attacks you must cleans and verify the validity of the input before storing it in the database!!
[php]<?php
// Script A.PHP

if (isset($_GET['stars'])) {
$stars=strip_tags(htmlentities($_GET['stars']));
// NOTE!!! You must cleanse and check the input before storing in the database
//
// From here you can store the value in your db
}
else {
?>
<ul class="star-rating small-star">
<li class="current-rating" style="width:50%">Currently 2.5/5 Stars.</li>
<li><a href="a.php?stars=1" title="1 star out of 5" class="one-star">1</a></li>
<li><a href="a.php?stars=2" title="2 stars out of 5" class="two-stars">2</a></li>
<li><a href="a.php?stars=3" title="3 stars out of 5" class="three-stars">3</a></li>
<li><a href="a.php?stars=4" title="4 stars out of 5" class="four-stars">4</a></li>
<li><a href="a.php?stars=5" title="5 stars out of 5" class="five-stars">5</a></li>
</ul>
<?php }
?>[/php]
Ronald
Feb 13 '08 #2
See this little example named A.PHP.
It gives assigns a HREF link to each star rating. When the user clicks it will call itself passing the choosen rating number. In the second pass you can pick up that rating from the $_GET array and store it in the database.
NOTE!!! in order to prevent any attacks you must cleans and verify the validity of the input before storing it in the database!!
[php]<?php
// Script A.PHP

if (isset($_GET['stars'])) {
$stars=strip_tags(htmlentities($_GET['stars']));
// NOTE!!! You must cleanse and check the input before storing in the database
//
// From here you can store the value in your db
}
else {
?>
<ul class="star-rating small-star">
<li class="current-rating" style="width:50%">Currently 2.5/5 Stars.</li>
<li><a href="a.php?stars=1" title="1 star out of 5" class="one-star">1</a></li>
<li><a href="a.php?stars=2" title="2 stars out of 5" class="two-stars">2</a></li>
<li><a href="a.php?stars=3" title="3 stars out of 5" class="three-stars">3</a></li>
<li><a href="a.php?stars=4" title="4 stars out of 5" class="four-stars">4</a></li>
<li><a href="a.php?stars=5" title="5 stars out of 5" class="five-stars">5</a></li>
</ul>
<?php }
?>[/php]
Ronald

Hi,
thanks for your solution. It is working, but there is some problem.
Since I am linking to same page a.php, when I click the stars it will take me to the blank page. I am using this rating star in a form where the user has to fill the next contents, but as soon as the star is clicked it directs to blank page.
How to solve this issue.

With regards
Feb 14 '08 #3
ronverdonk
4,258 Expert 4TB
Of course it gives you a blank page, that code was not complete, it was just a sample on how to do it!

You are supposed to complete the code yourself within this piece
[php]if (isset($_GET['stars'])) {
$stars=strip_tags(htmlentities($_GET['stars']));
// NOTE!!! You must cleanse and check the input before storing in the database
//
// From here you can store the value in your db
}[/php]
Ronald
Feb 14 '08 #4
Thank you

With Regards
Feb 14 '08 #5
ronverdonk
4,258 Expert 4TB
Thank you

With Regards
You are welcome any time.

Ronald
Feb 14 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Jeremy Posner | last post: by
14 posts views Thread by Stainless | last post: by
2 posts views Thread by Baron Samedi | last post: by
3 posts views Thread by R. Rajesh Jeba Anbiah | last post: by
reply views Thread by Ben Butschko | last post: by
1 post views Thread by webcm123 | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.