Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [MySQL][AJAX][PHP]Ajax star rater
Forum PHP.pl > Forum > Przedszkole
djkrc
Witam używał ktoś ajax star rater?questionmark.gif? Obecnie zainteresowałem się umieszczeniem go na stronie. Przy oddawaniu głosy w bazie zapisuje się IP użytkownika, ja chciał bym zamienić je na nazwe użytkownika, aby uniknąć zmiennego IP, niestety po zmianie IP na nazwę użytkownika oddany głos nie dodaje się do bazy. jeśli potrzeba wkleje kod php dla wglądu. Nie wiem czy jasno to opisałem, wybaczcie jesli nie.
Z góry dziękuję.
Blame
  1. define('POTRZEBA', TRUE);

Czyli wklej kod winksmiley.jpg
djkrc
Całość jest niestety po angielsku nie chciało mi się tłumaczyć komentarzy

Plik _drawrating.php, który wyświetla ratong bar na stronie

  1. <?php
  2. /*
  3. Page: _drawrating.php
  4. Created: Aug 2006
  5. Last Mod: Mar 18 2007
  6. The function that draws the rating bar.
  7. ---------------------------------------------------------
  8. ryan masuga, masugadesign.com
  9. ryan@masugadesign.com
  10. Licensed under a Creative Commons Attribution 3.0 License.
  11. <a href="http://creativecommons.org/licenses/by/3.0/" target="_blank">http://creativecommons.org/licenses/by/3.0/</a>
  12. See readme.txt for full credit details.
  13. --------------------------------------------------------- */
  14. function rating_bar($id,$units='',$static='') {
  15.  
  16. require('_config-rating.php'); // get the db connection info
  17.  
  18. //set some variables
  19. $ip = $_SERVER['REMOTE_ADDR'];
  20. if (!$units) {$units = 10;}
  21. if (!$static) {$static = FALSE;}
  22.  
  23. // get votes, values, ips for the current rating bar
  24. $query=mysql_query("SELECT total_votes, total_value, used_ips FROM $rating_dbname.$rating_tableName WHERE id='$id' ")or die(" Error: ".mysql_error());
  25.  
  26.  
  27. // insert the id in the DB if it doesn't exist already
  28. // see: <a href="http://www.masugadesign.com/the-lab/scripts/unobtrusive-ajax-star-rating-bar/#comment-121" target="_blank">http://www.masugadesign.com/the-lab/script...ar/#comment-121</a>
  29. if (mysql_num_rows($query) == 0) {
  30. $sql = "INSERT INTO $rating_dbname.$rating_tableName (`id`,`total_votes`, `total_value`, `used_ips`) VALUES ('$id', '0', '0', '')";
  31. $result = mysql_query($sql);
  32. }
  33.  
  34. $numbers=mysql_fetch_assoc($query);
  35.  
  36.  
  37. if ($numbers['total_votes'] < 1) {
  38. $count = 0;
  39. } else {
  40. $count=$numbers['total_votes']; //how many votes total
  41. }
  42. $current_rating=$numbers['total_value']; //total number of rating added together and stored
  43. if($count == 1){
  44. $tense = "głos"; //plural form votes/vote
  45. }elseif($count == 2 OR 3 OR 4){
  46. $tense = "głosy"; //plural form
  47. }elseif($count >=5){
  48. $tense = "głosów"; //plural form
  49. }elseif($count == 0){
  50. $tense = "głosów"; //plural form
  51. }
  52. // determine whether the user has voted, so we know how to draw the ul/li
  53. $voted=mysql_num_rows(mysql_query("SELECT used_ips FROM $rating_dbname.$rating_tableName WHERE used_ips LIKE '%".$ip."%' AND id='".$id."' "));
  54.  
  55. // now draw the rating bar
  56. $rating_width = @number_format($current_rating/$count,2)*$rating_unitwidth;
  57. $rating1 = @number_format($current_rating/$count,1);
  58. $rating2 = @number_format($current_rating/$count,2);
  59.  
  60.  
  61. if ($static == 'static') {
  62.  
  63. $static_rater = array();
  64. $static_rater[] .= "\n".'<div class="ratingblock">';
  65. $static_rater[] .= '<div id="unit_long'.$id.'">';
  66. $static_rater[] .= '<ul id="unit_ul'.$id.'" class="unit-rating" style="width:'.$rating_unitwidth*$units.'px;">';
  67. $static_rater[] .= '<li class="current-rating" style="width:'.$rating_width.'px;">Aktualnie '.$rating2.'/'.$units.'</li>';
  68. $static_rater[] .= '</ul>';
  69. $static_rater[] .= '<p class="static">Ocena: <strong> '.$rating1.'</strong>/'.$units.' ('.$count.' '.$tense.') <em>This is \'static\'.</em></p>';
  70. $static_rater[] .= '</div>';
  71. $static_rater[] .= '</div>'."\n\n";
  72.  
  73. return join("\n", $static_rater);
  74.  
  75.  
  76. } else {
  77.  
  78. $rater ='';
  79. $rater.='<div class="ratingblock">';
  80.  
  81. $rater.='<div id="unit_long'.$id.'">';
  82. $rater.=' <ul id="unit_ul'.$id.'" class="unit-rating" style="width:'.$rating_unitwidth*$units.'px;">';
  83. $rater.=' <li class="current-rating" style="width:'.$rating_width.'px;">Aktualnie '.$rating2.'/'.$units.'</li>';
  84.  
  85. for ($ncount = 1; $ncount <= $units; $ncount++) { // loop from 1 to the number of units
  86. if(!$voted) { // if the user hasn't yet voted, draw the voting stars
  87. $rater.='<li><a href="db.php?j='.$ncount.'&amp;q='.$id.'&amp;t='.$ip.'&amp;c='.$units.'" title="'.$ncount.' na '.$units.'" class="r'.$ncount.'-unit rater" rel="nofollow">'.$ncount.'</a></li>';
  88. }
  89. }
  90. $ncount=0; // resets the count
  91.  
  92. $rater.=' </ul>';
  93. $rater.=' <p';
  94. if($voted){ $rater.=' class="voted"'; }
  95. $rater.='>Ocena: <strong> '.$rating1.'</strong>/'.$units.' ('.$count.' '.$tense.')';
  96. $rater.=' </p>';
  97. $rater.='</div>';
  98. $rater.='</div>';
  99. return $rater;
  100. }
  101. }
  102. ?>


Plik db.php

  1. <?php
  2. /*
  3. Page: db.php
  4. Created: Aug 2006
  5. Last Mod: Mar 18 2007
  6. This page handles the database update if the user
  7. does NOT have Javascript enabled.
  8. ---------------------------------------------------------
  9. ryan masuga, masugadesign.com
  10. ryan@masugadesign.com
  11. Licensed under a Creative Commons Attribution 3.0 License.
  12. <a href="http://creativecommons.org/licenses/by/3.0/" target="_blank">http://creativecommons.org/licenses/by/3.0/</a>
  13. See readme.txt for full credit details.
  14. --------------------------------------------------------- */
  15. header("Cache-Control: no-cache");
  16. header("Pragma: nocache");
  17. require('_config-rating.php'); // get the db connection info
  18.  
  19. //getting the values
  20. $vote_sent = preg_replace("/[^0-9]/","",$_REQUEST['j']);
  21. $id_sent = preg_replace("/[^0-9a-zA-Z]/","",$_REQUEST['q']);
  22. $ip_num = preg_replace("/[^0-9\.]/","",$_REQUEST['t']);
  23. $units = preg_replace("/[^0-9]/","",$_REQUEST['c']);
  24. $ip = $_SERVER['REMOTE_ADDR'];
  25. $referer = $_SERVER['HTTP_REFERER'];
  26.  
  27. if ($vote_sent > $units) die("Sorry, vote appears to be invalid."); // kill the script because normal users will never see this.
  28.  
  29. //connecting to the database to get some information
  30. $query = mysql_query("SELECT total_votes, total_value, used_ips FROM $rating_dbname.$rating_tableName WHERE id='$id_sent' ")or die(" Error: ".mysql_error());
  31. $numbers = mysql_fetch_assoc($query);
  32. $checkIP = unserialize($numbers['used_ips']);
  33. $count = $numbers['total_votes']; //how many votes total
  34. $current_rating = $numbers['total_value']; //total number of rating added together and stored
  35. $sum = $vote_sent+$current_rating; // add together the current vote value and the total vote value
  36. if($count == 1){
  37. $tense = "głos"; //plural form votes/vote
  38. }elseif($count == 2 OR 3 OR 4){
  39. $tense = "głosy"; //plural form
  40. }elseif($count >=5){
  41. $tense = "głosów"; //plural form
  42. }elseif($count == 0){
  43. $tense = "głosów"; //plural form
  44. }
  45.  
  46. // checking to see if the first vote has been tallied
  47. // or increment the current number of votes
  48. ($sum==0 ? $added=0 : $added=$count+1);
  49.  
  50. // if it is an array i.e. already has entries the push in another value
  51. ((is_array($checkIP)) ? array_push($checkIP,$ip_num) : $checkIP=array($ip_num));
  52. $insertip=serialize($checkIP);
  53.  
  54. //IP check when voting
  55. $voted=mysql_num_rows(mysql_query("SELECT used_ips FROM $rating_dbname.$rating_tableName WHERE used_ips LIKE '%".$ip."%' AND id='".$id_sent."' "));
  56. if(!$voted) { //if the user hasn't yet voted, then vote normally...
  57.  
  58.  
  59. if (($vote_sent >= 1 && $vote_sent <= $units) && ($ip == $ip_num)) { // keep votes within range
  60. $update = "UPDATE $rating_dbname.$rating_tableName SET total_votes='".$added."', total_value='".$sum."', used_ips='".$insertip."' WHERE id='$id_sent'";
  61. $result = mysql_query($update);
  62. }
  63. header("Location: $referer"); // go back to the page we came from
  64. } //end for the "if(!$voted)"
  65. ?>


Próbowałem zrobić tak
w pierwszym pliku
  1. $ip = $user_name;

w drugim
  1. $vote_sent = preg_replace("/[^0-9]/","",$_REQUEST['j']);
  2. $id_sent = preg_replace("/[^0-9a-zA-Z]/","",$_REQUEST['q']);
  3. //odbieram nazwę użytkownika
  4. $ip_num = preg_replace("/[^0-9a-zA-Z]/","",$_REQUEST['t']);
  5. $units = preg_replace("/[^0-9]/","",$_REQUEST['c']);
  6. //pobieram nazwę użytkownika z wcześniej przypisanej funkcji $user_name = $_SESSION['user_name']
  7. $ip = $user_name;
  8. $referer = $_SERVER['HTTP_REFERER'];


Lecz niestety to nie wystarczy, do tabel nic się nie dodaje to samo jak zamieniam user_name na user_id, dziala tylko na ip nie mam pojecia czemu

całość pobrałem stąd
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.