<?php require "connect.php"; require "suggestion.class.php"; $result = $mysqli->query(" SELECT s.*, if (v.ip IS NULL,0,1) AS have_voted FROM suggestions AS s LEFT JOIN suggestions_votes AS v ON( s.id = v.suggestion_id AND v.day = CURRENT_DATE AND v.ip = $ip ) ORDER BY s.rating DESC, s.id DESC "); $str = ''; if(!$mysqli->error) { $str = '<ul class="suggestions">'; while($suggestion = $result->fetch_object('Suggestion')){ $str.= $suggestion; } $str .='</ul>'; } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Zagłosuj !</title> <link rel="stylesheet" type="text/css" href="styles.css" /> </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.):
w miejscach x są normalne dane
$db_host = 'xxxl'; $db_user = 'xxx'; $db_pass = 'xxx'; $db_name = 'xxx'; @$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name); if (mysqli_connect_errno()) { } $mysqli->set_charset("utf8");

Plik "suggestion.class.php":
<?php class Suggestion { { $this->data = $arr; } } public function __get($property){ return $this->data[$property]; } return NULL; } public function __toString() { return ' <li id="s'.$this->id.'"> <div class="vote '.($this->have_voted ? 'inactive' : 'active').'"> <span class="up"></span> <span class="down"></span> </div> <div class="text">'.$this->suggestion.'</div> <div class="rating">'.(int)$this->rating.'</div> </li>'; } } ?>
Plik ajax.php:
<?php require "connect.php"; require "suggestion.class.php"; if($_SERVER['HTTP_X_REQUESTED_WITH'] !='XMLHttpRequest'){ exit; } if ($_SERVER['HTTP_X_FORWARDED_FOR']) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip = $_SERVER['REMOTE_ADDR']; } if($_GET['action'] == 'vote'){ $v = (int)$_GET['vote']; $id = (int)$_GET['id']; if($v != -1 && $v != 1){ exit; } if(!$mysqli->query("SELECT 1 FROM suggestions WHERE id = $id")->num_rows){ exit; } $mysqli->query(" INSERT INTO suggestions_votes (suggestion_id,ip,day,vote) VALUES ( $id, $ip, CURRENT_DATE, $v ) "); if($mysqli->affected_rows == 1) { $mysqli->query(" UPDATE suggestions SET ".($v == 1 ? 'votes_up = votes_up + 1' : 'votes_down = votes_down + 1').", rating = rating + $v WHERE id = $id "); } } else if($_GET['action'] == 'submit'){ array_walk_recursive($_GET,create_function('&$v,$k','$v = stripslashes($v);')); } if(mb_strlen($_GET['content'],'utf-8')<3){ exit; } $mysqli->query("INSERT INTO suggestions SET suggestion = '".$mysqli->real_escape_string($_GET['content'])."'"); 'id' => $mysqli->insert_id, 'suggestion' => $_GET['content'] ))) )); } ?>
No i script.js:
$(document).ready(function(){ var ul = $('ul.suggestions'); // Listening of a click on a UP or DOWN arrow: $('div.vote span').live('click',function(){ var elem = $(this), parent = elem.parent(), li = elem.closest('li'), ratingDiv = li.find('.rating'), id = li.attr('id').replace('s',''), v = 1; // If the user's already voted: if(parent.hasClass('inactive')){ return false; } parent.removeClass('active').addClass('inactive'); if(elem.hasClass('down')){ v = -1; } // Incrementing the counter on the right: ratingDiv.text(v + +ratingDiv.text()); // Turning all the LI elements into an array // and sorting it on the number of votes: return +$('.rating',r).text() - +$('.rating',l).text(); }); // Adding the sorted LIs to the UL ul.html(arr); // Sending an AJAX request $.get('ajax.php',{action:'vote',vote:v,'id':id}); }); $('#suggest').submit(function(){ var form = $(this), textField = $('#suggestionText'); // Preventing double submits: if(form.hasClass('working') || textField.val().length<3){ return false; } form.addClass('working'); $.getJSON('ajax.php',{action:'submit',content:textField.val()},function(msg){ textField.val(''); form.removeClass('working'); if(msg.html){ // Appending the markup of the newly created LI to the page: $(msg.html).hide().appendTo(ul).slideDown(); } }); return false; }); });
Będę bardzo wdzięczny jeśli ktoś pomoże
