Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Dokładka shortcode do skryptu w Wordpress
kedzier11
post
Post #1





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 13.01.2014

Ostrzeżenie: (0%)
-----


Panowie pytanie dotyczy shortcode. Mam taki skrypt jak widać przez echo mogę wywoływać tę funkcję i zmienić parametry name, procenty itp. Chciał bym móc to zmieniać przy wykorzystaniu shortcode w wordpressie. Ale nie wiem jak. Czy ktoś pomoże? Dodam iż jestem początkującym programistom więc łopatologicznie proszę (IMG:style_emoticons/default/biggrin.gif)
  1. function pullRating($name,$show5 = true, $showPerc = false, $showVotes = true, $static = NULL){
  2.  
  3. // Check if they have already voted...
  4. $text = '';
  5.  
  6. $sel = mysql_query("SELECT id FROM ratings WHERE IP = '".$_SERVER['REMOTE_ADDR']."' AND rating_id = '$name'");
  7. if(mysql_num_rows($sel) > 0 || $static == 'novote' || isset($_COOKIE['has_voted_'.$name])){
  8.  
  9.  
  10.  
  11. if($show5 || $showPerc || $showVotes){
  12.  
  13. $text .= '<div class="rated_text">';
  14.  
  15. }
  16.  
  17. if($show5){
  18. $text .= 'Rated <span id="outOfFive_'.$name.'" class="out5Class">'.outOfFive($name).'</span>/5';
  19. }
  20. if($showPerc){
  21. $text .= ' (<span id="percentage_'.$name.'" class="percentClass">'.getRating($name).'</span>)';
  22. }
  23. if($showVotes){
  24. $text .= ' (<span id="showvotes_'.$name.'" class="votesClass">'.getVotes($name).'</span>)';
  25. }
  26.  
  27. if($show5 || $showPerc || $showVotes){
  28.  
  29. $text .= '</div>';
  30.  
  31. }
  32.  
  33.  
  34. return $text.'
  35. <ul class="star-rating2" id="rater_'.$name.'">
  36. <li class="current-rating" style="width:'.getRating($name).';" id="ul_'.$name.'"></li>
  37. <li><a onclick="return false;" href="#" title="1 star out of 5" class="one-star" >1</a></li>
  38. <li><a onclick="return false;" href="#" title="2 stars out of 5" class="two-stars">2</a></li>
  39. <li><a onclick="return false;" href="#" title="3 stars out of 5" class="three-stars">3</a></li>
  40. <li><a onclick="return false;" href="#" title="4 stars out of 5" class="four-stars">4</a></li>
  41. <li><a onclick="return false;" href="#" title="5 stars out of 5" class="five-stars">5</a></li>
  42. </ul>
  43. <div id="loading_'.$name.'"></div>';
  44.  
  45.  
  46. } else {
  47.  
  48. if($show5 || $showPerc || $showVotes){
  49.  
  50. $text .= '<div class="rated_text">';
  51.  
  52. }
  53. if($show5){
  54. $show5bool = 'true';
  55. $text .= 'Rated <span id="outOfFive_'.$name.'" class="out5Class">'.outOfFive($name).'</span>/5';
  56. } else {
  57. $show5bool = 'false';
  58. }
  59. if($showPerc){
  60. $showPercbool = 'true';
  61. $text .= ' (<span id="percentage_'.$name.'" class="percentClass">'.getRating($name).'</span>)';
  62. } else {
  63. $showPercbool = 'false';
  64. }
  65. if($showVotes){
  66. $showVotesbool = 'true';
  67. $text .= ' (<span id="showvotes_'.$name.'" class="votesClass">'.getVotes($name).'</span>)';
  68. } else {
  69. $showVotesbool = 'false';
  70. }
  71.  
  72. if($show5 || $showPerc || $showVotes){
  73.  
  74. $text .= '</div>';
  75.  
  76. }
  77.  
  78. return $text.'
  79. <ul class="star-rating" id="rater_'.$name.'">
  80. <li class="current-rating" style="width:'.getRating($name).';" id="ul_'.$name.'"></li>
  81. <li><a onclick="rate(\'1\',\''.$name.'\','.$show5bool.','.$showPercbool.','.$showVotesbool.'); return false;" href="includes/rating_process.php?id='.$name.'&rating=1" title="1 star out of 5" class="one-star" >1</a></li>
  82. <li><a onclick="rate(\'2\',\''.$name.'\','.$show5bool.','.$showPercbool.','.$showVotesbool.'); return false;" href="includes/rating_process.php?id='.$name.'&rating=2" title="2 stars out of 5" class="two-stars">2</a></li>
  83. <li><a onclick="rate(\'3\',\''.$name.'\','.$show5bool.','.$showPercbool.','.$showVotesbool.'); return false;" href="includes/rating_process.php?id='.$name.'&rating=3" title="3 stars out of 5" class="three-stars">3</a></li>
  84. <li><a onclick="rate(\'4\',\''.$name.'\','.$show5bool.','.$showPercbool.','.$showVotesbool.'); return false;" href="includes/rating_process.php?id='.$name.'&rating=4" title="4 stars out of 5" class="four-stars">4</a></li>
  85. <li><a onclick="rate(\'5\',\''.$name.'\','.$show5bool.','.$showPercbool.','.$showVotesbool.'); return false;" href="includes/rating_process.php?id='.$name.'&rating=5" title="5 stars out of 5" class="five-stars">5</a></li>
  86. </ul>
  87. <div id="loading_'.$name.'"></div>';
  88.  
  89. }
  90. }add_shortcode('rater', 'pullRating');


Ten post edytował kedzier11 25.03.2014, 15:54:04
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
kedzier11
post
Post #2





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 13.01.2014

Ostrzeżenie: (0%)
-----


ok działa. To jeszcze ostatnie pytanie. Jak zrobić aby cały ten skrypt działał:
  1. <?
  2.  
  3.  
  4. function getRating($name){
  5.  
  6.  
  7. $total = 0;
  8. $rows = 0;
  9.  
  10. $sel = mysql_query("SELECT rating_num FROM ratings WHERE rating_id = '$name'");
  11. if(mysql_num_rows($sel) > 0){
  12.  
  13. while($data = mysql_fetch_assoc($sel)){
  14.  
  15. $total = $total + $data['rating_num'];
  16. $rows++;
  17. }
  18.  
  19. $perc = ($total/$rows) * 20;
  20.  
  21. //$newPerc = round($perc/5)*5;
  22. //return $newPerc.'%';
  23.  
  24. $newPerc = round($perc,2);
  25. return $newPerc.'%';
  26.  
  27. } else {
  28.  
  29. return '0%';
  30.  
  31. }
  32. }
  33.  
  34. function outOfFive($name){
  35. $name = $_SERVER['REQUEST_URI'];
  36. $total = 0;
  37. $rows = 0;
  38.  
  39. $sel = mysql_query("SELECT rating_num FROM ratings WHERE rating_id = '$name'");
  40. if(mysql_num_rows($sel) > 0){
  41.  
  42. while($data = mysql_fetch_assoc($sel)){
  43.  
  44. $total = $total + $data['rating_num'];
  45. $rows++;
  46. }
  47.  
  48. $perc = ($total/$rows);
  49.  
  50. return round($perc,2);
  51. //return round(($perc*2), 0)/2; // 3.5
  52.  
  53. } else {
  54.  
  55. return '0';
  56.  
  57. }
  58.  
  59.  
  60. }
  61.  
  62. function getVotes($name){
  63. $name = $_SERVER['REQUEST_URI'];
  64. $sel = mysql_query("SELECT rating_num FROM ratings WHERE rating_id = '$name'");
  65. $rows = mysql_num_rows($sel);
  66. if($rows == 0){
  67. $votes = '0 Votes';
  68. }
  69. else if($rows == 1){
  70. $votes = '1 Vote';
  71. } else {
  72. $votes = $rows.' Votes';
  73. }
  74. return $votes;
  75.  
  76. }
  77.  
  78.  
  79. add_shortcode('rater', 'pullRating');
  80.  
  81. function pullRating($attr){
  82.  
  83. $name = $attr['name'];
  84. $show5 = $attr['show5'];
  85. $showPerc=$attr['showPerc'];
  86. $showVotes=$attr['showVotes'];
  87. $static= $attr['static'];
  88.  
  89.  
  90. // Check if they have already voted...
  91. $text = '';
  92.  
  93. $sel = mysql_query("SELECT id FROM ratings WHERE IP = '".$_SERVER['REMOTE_ADDR']."' AND rating_id = '$name'");
  94. if(mysql_num_rows($sel) > 0 || $static == 'novote' || isset($_COOKIE['has_voted_'.$name])){
  95.  
  96.  
  97.  
  98. if($show5 || $showPerc || $showVotes){
  99.  
  100. $text .= '<div class="rated_text">';
  101.  
  102. }
  103.  
  104. if($show5){
  105. $text .= 'Rated <span id="outOfFive_'.$name.'" class="out5Class">'.outOfFive($name).'</span>/5';
  106. }
  107. if($showPerc){
  108. $text .= ' (<span id="percentage_'.$name.'" class="percentClass">'.getRating($name).'</span>)';
  109. }
  110. if($showVotes){
  111. $text .= ' (<span id="showvotes_'.$name.'" class="votesClass">'.getVotes($name).'</span>)';
  112. }
  113.  
  114. if($show5 || $showPerc || $showVotes){
  115.  
  116. $text .= '</div>';
  117.  
  118. }
  119.  
  120.  
  121. return $text.'
  122. <ul class="star-rating2" id="rater_'.$name.'">
  123. <li class="current-rating" style="width:'.getRating($name).';" id="ul_'.$name.'"></li>
  124. <li><a onclick="return false;" href="#" title="1 star out of 5" class="one-star" >1</a></li>
  125. <li><a onclick="return false;" href="#" title="2 stars out of 5" class="two-stars">2</a></li>
  126. <li><a onclick="return false;" href="#" title="3 stars out of 5" class="three-stars">3</a></li>
  127. <li><a onclick="return false;" href="#" title="4 stars out of 5" class="four-stars">4</a></li>
  128. <li><a onclick="return false;" href="#" title="5 stars out of 5" class="five-stars">5</a></li>
  129. </ul>
  130. <div id="loading_'.$name.'"></div>';
  131.  
  132.  
  133. } else {
  134.  
  135. if($show5 || $showPerc || $showVotes){
  136.  
  137. $text .= '<div class="rated_text">';
  138.  
  139. }
  140. if($show5){
  141. $show5bool = 'true';
  142. $text .= 'Rated <span id="outOfFive_'.$name.'" class="out5Class">'.outOfFive($name).'</span>/5';
  143. } else {
  144. $show5bool = 'false';
  145. }
  146. if($showPerc){
  147. $showPercbool = 'true';
  148. $text .= ' (<span id="percentage_'.$name.'" class="percentClass">'.getRating($name).'</span>)';
  149. } else {
  150. $showPercbool = 'false';
  151. }
  152. if($showVotes){
  153. $showVotesbool = 'true';
  154. $text .= ' (<span id="showvotes_'.$name.'" class="votesClass">'.getVotes($name).'</span>)';
  155. } else {
  156. $showVotesbool = 'false';
  157. }
  158.  
  159. if($show5 || $showPerc || $showVotes){
  160.  
  161. $text .= '</div>';
  162.  
  163. }
  164.  
  165. return $text.'
  166. <ul class="star-rating" id="rater_'.$name.'">
  167. <li class="current-rating" style="width:'.getRating($name).';" id="ul_'.$name.'"></li>
  168. <li><a onclick="rate(\'1\',\''.$name.'\','.$show5bool.','.$showPercbool.','.$showVotesbool.'); return false;" href="includes/rating_process.php?id='.$name.'&rating=1" title="1 star out of 5" class="one-star" >1</a></li>
  169. <li><a onclick="rate(\'2\',\''.$name.'\','.$show5bool.','.$showPercbool.','.$showVotesbool.'); return false;" href="includes/rating_process.php?id='.$name.'&rating=2" title="2 stars out of 5" class="two-stars">2</a></li>
  170. <li><a onclick="rate(\'3\',\''.$name.'\','.$show5bool.','.$showPercbool.','.$showVotesbool.'); return false;" href="includes/rating_process.php?id='.$name.'&rating=3" title="3 stars out of 5" class="three-stars">3</a></li>
  171. <li><a onclick="rate(\'4\',\''.$name.'\','.$show5bool.','.$showPercbool.','.$showVotesbool.'); return false;" href="includes/rating_process.php?id='.$name.'&rating=4" title="4 stars out of 5" class="four-stars">4</a></li>
  172. <li><a onclick="rate(\'5\',\''.$name.'\','.$show5bool.','.$showPercbool.','.$showVotesbool.'); return false;" href="includes/rating_process.php?id='.$name.'&rating=5" title="5 stars out of 5" class="five-stars">5</a></li>
  173. </ul>
  174. <div id="loading_'.$name.'"></div>';
  175.  
  176. }
  177. }
  178.  
  179. // Added in version 1.5
  180. // Fixed sort in version 1.7
  181. function getTopRated($limit, $table, $namefield, $namefield){
  182. $name = $_SERVER['REQUEST_URI'];
  183. $result = '';
  184.  
  185. $sql = "SELECT COUNT(ratings.id) as rates,ratings.rating_id,".$table.".".$namefield." as thenamefield,ROUND(AVG(ratings.rating_num),2) as rating
  186. FROM ratings,".$table." WHERE ".$table.".".$namefield." = ratings.rating_id GROUP BY rating_id
  187. ORDER BY rates DESC,rating DESC LIMIT ".$limit."";
  188.  
  189. $sel = mysql_query($sql);
  190.  
  191. $result .= '<ul class="topRatedList">'."\n";
  192.  
  193. while($data = @mysql_fetch_assoc($sel)){
  194. $result .= '<li>'.$data['thenamefield'].' ('.$data['rating'].')</li>'."\n";
  195. }
  196.  
  197. $result .= '</ul>'."\n";
  198.  
  199. return $result;
  200.  
  201. }
  202. ?>
a no i od razu na pw. adres gdzie wysłać flachę ^^
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 18.10.2025 - 00:21