Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP][AJAX]System głosowania
Forum PHP.pl > Forum > Przedszkole
pawelsz66
Mój problem polega na tym, że coś blokuje dalsze wyświetlanie się mojej strony. Mianowicie:
  1. <?php
  2.  
  3. require "connect.php";
  4. require "suggestion.class.php";
  5.  
  6. $ip = sprintf('%u',ip2long($_SERVER['REMOTE_ADDR']));
  7.  
  8.  
  9.  
  10.  
  11. $result = $mysqli->query("
  12. SELECT s.*, if (v.ip IS NULL,0,1) AS have_voted
  13. FROM suggestions AS s
  14. LEFT JOIN suggestions_votes AS v
  15. ON(
  16. s.id = v.suggestion_id
  17. AND v.day = CURRENT_DATE
  18. AND v.ip = $ip
  19. )
  20. ORDER BY s.rating DESC, s.id DESC
  21. ");
  22.  
  23. $str = '';
  24.  
  25. if(!$mysqli->error)
  26. {
  27.  
  28.  
  29. $str = '<ul class="suggestions">';
  30.  
  31.  
  32.  
  33. while($suggestion = $result->fetch_object('Suggestion')){
  34.  
  35. $str.= $suggestion;
  36.  
  37. }
  38.  
  39. $str .='</ul>';
  40. }
  41.  
  42. ?>
  43.  
  44. <!DOCTYPE html>
  45. <html>
  46. <head>
  47. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  48. <title>Zagłosuj !</title>
  49.  
  50. <link rel="stylesheet" type="text/css" href="styles.css" />
  51.  
  52. </head>

Jest to plik z formularzem do głosów itd. Niestety nic się na niej nie wyświetla.
Plik connect.php(Plik z którego pobierane są informacje o bazie danej itd.):
  1. error_reporting(E_ALL ^ E_NOTICE);
  2.  
  3. $db_host = 'xxxl';
  4. $db_user = 'xxx';
  5. $db_pass = 'xxx';
  6. $db_name = 'xxx';
  7.  
  8. @$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
  9.  
  10. if (mysqli_connect_errno()) {
  11. die('<h1>Nie można się połaczyć z bazą danych</h1>');
  12. }
  13.  
  14. $mysqli->set_charset("utf8");
w miejscach x są normalne dane smile.gif

Plik "suggestion.class.php":
  1. <?php
  2.  
  3. class Suggestion
  4. {
  5. private $data = array();
  6.  
  7. public function __construct($arr = array())
  8. {
  9. if(!empty($arr)){
  10.  
  11.  
  12. $this->data = $arr;
  13. }
  14. }
  15.  
  16. public function __get($property){
  17.  
  18.  
  19.  
  20. if(array_key_exists($property,$this->data)){
  21. return $this->data[$property];
  22. }
  23.  
  24. return NULL;
  25. }
  26.  
  27. public function __toString()
  28. {
  29.  
  30.  
  31. return '
  32. <li id="s'.$this->id.'">
  33. <div class="vote '.($this->have_voted ? 'inactive' : 'active').'">
  34. <span class="up"></span>
  35. <span class="down"></span>
  36. </div>
  37.  
  38. <div class="text">'.$this->suggestion.'</div>
  39. <div class="rating">'.(int)$this->rating.'</div>
  40. </li>';
  41. }
  42. }
  43.  
  44. ?>

Plik ajax.php:
  1. <?php
  2.  
  3. require "connect.php";
  4. require "suggestion.class.php";
  5.  
  6.  
  7. if($_SERVER['HTTP_X_REQUESTED_WITH'] !='XMLHttpRequest'){
  8. }
  9.  
  10.  
  11.  
  12. if ($_SERVER['HTTP_X_FORWARDED_FOR']) {
  13. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  14. }
  15. else {
  16. $ip = $_SERVER['REMOTE_ADDR'];
  17. }
  18.  
  19. if($_GET['action'] == 'vote'){
  20.  
  21. $v = (int)$_GET['vote'];
  22. $id = (int)$_GET['id'];
  23.  
  24. if($v != -1 && $v != 1){
  25. }
  26.  
  27.  
  28. if(!$mysqli->query("SELECT 1 FROM suggestions WHERE id = $id")->num_rows){
  29. }
  30.  
  31.  
  32. $mysqli->query("
  33. INSERT INTO suggestions_votes (suggestion_id,ip,day,vote)
  34. VALUES (
  35. $id,
  36. $ip,
  37. CURRENT_DATE,
  38. $v
  39. )
  40. ");
  41.  
  42. if($mysqli->affected_rows == 1)
  43. {
  44. $mysqli->query("
  45. UPDATE suggestions SET
  46. ".($v == 1 ? 'votes_up = votes_up + 1' : 'votes_down = votes_down + 1').",
  47. rating = rating + $v
  48. WHERE id = $id
  49. ");
  50. }
  51.  
  52. }
  53. else if($_GET['action'] == 'submit'){
  54.  
  55. array_walk_recursive($_GET,create_function('&$v,$k','$v = stripslashes($v);'));
  56. }
  57.  
  58.  
  59. $_GET['content'] = htmlspecialchars(strip_tags($_GET['content']));
  60.  
  61. if(mb_strlen($_GET['content'],'utf-8')<3){
  62. }
  63.  
  64. $mysqli->query("INSERT INTO suggestions SET suggestion = '".$mysqli->real_escape_string($_GET['content'])."'");
  65.  
  66.  
  67.  
  68. echo json_encode(array(
  69. 'html' => (string)(new Suggestion(array(
  70. 'id' => $mysqli->insert_id,
  71. 'suggestion' => $_GET['content']
  72. )))
  73. ));
  74. }
  75.  
  76.  
  77. ?>

No i script.js:
  1. $(document).ready(function(){
  2.  
  3. var ul = $('ul.suggestions');
  4.  
  5. // Listening of a click on a UP or DOWN arrow:
  6.  
  7. $('div.vote span').live('click',function(){
  8.  
  9. var elem = $(this),
  10. parent = elem.parent(),
  11. li = elem.closest('li'),
  12. ratingDiv = li.find('.rating'),
  13. id = li.attr('id').replace('s',''),
  14. v = 1;
  15.  
  16. // If the user's already voted:
  17.  
  18. if(parent.hasClass('inactive')){
  19. return false;
  20. }
  21.  
  22. parent.removeClass('active').addClass('inactive');
  23.  
  24. if(elem.hasClass('down')){
  25. v = -1;
  26. }
  27.  
  28. // Incrementing the counter on the right:
  29. ratingDiv.text(v + +ratingDiv.text());
  30.  
  31. // Turning all the LI elements into an array
  32. // and sorting it on the number of votes:
  33.  
  34. var arr = $.makeArray(ul.find('li')).sort(function(l,r){
  35. return +$('.rating',r).text() - +$('.rating',l).text();
  36. });
  37.  
  38. // Adding the sorted LIs to the UL
  39. ul.html(arr);
  40.  
  41. // Sending an AJAX request
  42. $.get('ajax.php',{action:'vote',vote:v,'id':id});
  43. });
  44.  
  45.  
  46. $('#suggest').submit(function(){
  47.  
  48. var form = $(this),
  49. textField = $('#suggestionText');
  50.  
  51. // Preventing double submits:
  52. if(form.hasClass('working') || textField.val().length<3){
  53. return false;
  54. }
  55.  
  56. form.addClass('working');
  57.  
  58. $.getJSON('ajax.php',{action:'submit',content:textField.val()},function(msg){
  59. textField.val('');
  60. form.removeClass('working');
  61.  
  62. if(msg.html){
  63. // Appending the markup of the newly created LI to the page:
  64. $(msg.html).hide().appendTo(ul).slideDown();
  65. }
  66. });
  67.  
  68. return false;
  69. });
  70. });

Będę bardzo wdzięczny jeśli ktoś pomoże smile.gif
CuteOne
1. Najpierw zobacz temat w podczepionych "Jak zadawać pytania"...
2. Sprawdź w konsoli błędów czy JS nie wywala błędów [jak masz w mojej sygnie]
3. Zamiast exit; wstaw jakieś komunikaty [być może to przez nie brakuje ci części strony]
pawelsz66
Już rozwiązałem problem.
  1. @$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);

Serwer na, którym był ten skrypt nie obsługiwał funkcji "mysqli'. A nie wyświetlało mi błędu przez to, że na początku dodałem "@" smile.gif
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.