Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [MySQL][PHP]Problem ze skryptem - głosowanie z MySQL
Forum PHP.pl > Forum > Przedszkole
imatix
Może ktos wie czemu ten skrypt nie działa?? :

  1. <?php
  2. // Connects to your Database
  3. mysql_connect("mysql1.yoyo.pl", "dbxxxxxx", "haslo") or die(mysql_error());
  4. mysql_select_db("dbxxxxxx") or die(mysql_error());
  5.  
  6. //We only run this code if the user has just clicked a voting link
  7. if ( $mode=="vote")
  8. {
  9.  
  10. //If the user has already voted on the particular thing, we do not allow them to vote again $cookie = "Mysite$id";
  11. if(isset($_COOKIE[$cookie]))
  12. {
  13. Echo "Sorry You have already ranked that site <p>";
  14. }
  15.  
  16. //Otherwise, we set a cooking telling us they have now voted
  17. else
  18. {
  19. $month = 2592000 + time();
  20. setcookie(Mysite.$id, Voted, $month);
  21.  
  22. //Then we update the voting information by adding 1 to the total votes and adding their vote (1,2,3,etc) to the total rating
  23. mysql_query ("UPDATE vote SET total = total+$voted, votes = votes+1 WHERE id = $id");
  24. Echo "Your vote has been cast <p>";
  25. }
  26. }
  27.  
  28. //Puts SQL Data into an array
  29. $data = mysql_query("SELECT * FROM vote") or die(mysql_error());
  30.  
  31. //Now we loop through all the data
  32. while($ratings = mysql_fetch_array( $data ))
  33. {
  34.  
  35. //This outputs the sites name
  36. Echo "Name: " .$ratings['name']."<br>";
  37.  
  38. //This calculates the sites ranking and then outputs it - rounded to 1 decimal
  39. $current = $ratings[total] / $ratings[votes];
  40. Echo "Current Rating: " . round($current, 1) . "<br>";
  41.  
  42. //This creates 5 links to vote a 1, 2, 3, 4, or 5 rating for each particular item
  43. Echo "Rank Me: ";
  44. Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=1&id=".$ratings[id].">Vote 1</a> | ";
  45. Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=2&id=".$ratings[id].">Vote 2</a> | ";
  46. Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=3&id=".$ratings[id].">Vote 3</a> | ";
  47. Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=4&id=".$ratings[id].">Vote 4</a> | ";
  48. Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=5&id=".$ratings[id].">Vote 5</a><p>";
  49. }
  50.  
  51.  
  52. ?>


Wczesniej dodalem do bazy danych :
  1. CREATE TABLE vote (id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30), total INTEGER, votes INTEGER)

oraz
  1. INSERT INTO vote (name, total, votes) VALUES ( "First item", 45, 10 ), ( "Second item", 15, 4 ), ( "Third thing", 25, 7 ), ( "The Forth", 20, 5 ), ( "Fifth Thing", 0, 0 )


Na stronie wszystko sie wyswietla, ale skrypt nie liczy sredniej, po kliknieciu nic sie nie zmienia:
Adres do skryptu http://www.muzycznalista.yoyo.pl/

Prosze o pomoc.
Pozdro
croc
Myślę, że chodzi o to, że skrypt jest staroświecki i używa register_globals.

Musiałbyś zamienić ten fragment:

  1. //We only run this code if the user has just clicked a voting link
  2. if ( $mode=="vote")
  3. {


Na taki:

  1. //We only run this code if the user has just clicked a voting link
  2. if ( $_GET['mode']=="vote")
  3. {
  4. $voted = $_GET['voted'];
  5. $id= $_GET['id'];
imatix
OK dzieki dziala smile.gif
Tylko jest cos zle jeszcze z cookie, bo ich nie przechwytuje i mozna glosowac ile wlezie, wiesz jak to zrobic?
croc
Jedna instrukcja wkradła się do komentarza. Poza tym w instrukcji setcookie lepiej podać wartość ze zmiennej, czyli zrób tak:

zamień to:

  1. //If the user has already voted on the particular thing, we do not allow them to vote again $cookie = "Mysite$id";
  2. if(isset($_COOKIE[$cookie]))
  3. {
  4. Echo "Sorry You have already ranked that site <p>";
  5. }
  6.  
  7. //Otherwise, we set a cooking telling us they have now voted
  8. else
  9. {
  10. $month = 2592000 + time();
  11. setcookie(Mysite.$id, Voted, $month);


zamień na:

  1. //If the user has already voted on the particular thing, we do not allow them to vote again
  2. $cookie = "Mysite$id";
  3. if(isset($_COOKIE[$cookie]))
  4. {
  5. Echo "Sorry You have already ranked that site <p>";
  6. }
  7.  
  8. //Otherwise, we set a cooking telling us they have now voted
  9. else
  10. {
  11. $month = 2592000 + time();
  12. setcookie($cookie, 'Voted', $month);
imatix
Nom dzieki bardzo biggrin.gif
A jak zrobić żeby rekordy pokazywalo zgodnie ze srednia a nie ID ?
croc
To znaczy masz na myśli sortowanie, tak?

Ja bym zrobił tak:

zamiast tego:
  1. //Puts SQL Data into an array
  2. $data = mysql_query("SELECT * FROM vote") or die(mysql_error());
  3.  
  4. //Now we loop through all the data
  5. while($ratings = mysql_fetch_array( $data ))
  6. {
  7.  
  8. //This outputs the sites name
  9. Echo "Name: " .$ratings['name']."<br>";
  10.  
  11. //This calculates the sites ranking and then outputs it - rounded to 1 decimal
  12. $current = $ratings[total] / $ratings[votes];
  13. Echo "Current Rating: " . round($current, 1) . "<br>";


to:
  1. //Puts SQL Data into an array
  2. $data = mysql_query("SELECT *, total / votes AS average FROM vote ORDER BY average DESC") or die(mysql_error());
  3.  
  4. //Now we loop through all the data
  5. while($ratings = mysql_fetch_array( $data ))
  6. {
  7.  
  8. //This outputs the sites name
  9. Echo "Name: " .$ratings['name']."<br>";
  10.  
  11. //This calculates the sites ranking and then outputs it - rounded to 1 decimal
  12. $current = $ratings['average'];
  13. Echo "Current Rating: " . round($current, 1) . "<br>";

imatix
Nom dzieki za pomoc smile.gif

Jeszcze jedno pytanie, bo chciałem, aby zeby wybieralo tylko z tabeli vote i z kolumny "polskie" gdzie "nie" jak to trzeba wpisac tongue.gif?
Bo tak na pewno nie:
  1. $data = mysql_query("SELECT *, total / votes AS average FROM vote ORDER BY average DESC," . "SELECT * FROM vote WHERE polskie=nie ") or die(mysql_error());

I jak bym chcial dodac jeszcze 1 warunek np: kolumna "gatunek" gdzie "pop"
Do bazy dopisalem za pomoca funkcji set np: wartosc: 'tak','nie'

Dobra juz mam ^^:
  1. $data = mysql_query("SELECT *, total / votes AS average FROM vote WHERE polskie='nie' ORDER BY average DESC") or die(mysql_error());


Wie ktos jak zrobic zeby przy kazdej zapetlonej pozycji pojawiaja sie kolejna liczba np 1,2,3,4?

Haloooo
Moze ktos pomoc??
croc
Tak, ładnie znalazłeś rozwiązanie problemu kryteriów wyszukiwania smile.gif Jak chcesz dodać kolejne kryteria, to łączysz je słowem AND (lub OR, zależy co chcesz osiągnąć - poczytaj o tym).

Żeby zrobić kolejne liczby, możesz zrobić np. tak:

  1. $data = mysql_query("SELECT *, total / votes AS average FROM vote WHERE polskie='nie' ORDER BY average DESC") or die(mysql_error());
  2. for($i = 1; $row = mysql_fetch_assoc($data); ++$i) {
  3. // i tutaj masz liczbę w zmiennej $i, co wiersz większą o 1
  4. }


Jeszcze jedna uwaga - zamiast polskie = 'tak' / polskie = 'nie' lepiej zrobić typ pola na char(1) i wpisywać do niego tylko t/n lub typ liczbowy i 0/1.
imatix
  1. //Wstawienie informacji z MySQL
  2. $data = mysql_query("SELECT *, total / votes AS average FROM vote ORDER BY average DESC") or die(mysql_error());
  3.  
  4. for($i = 1; $row = mysql_fetch_assoc($data); ++$i) {
  5. // i tutaj masz liczbę w zmiennej $i, co wiersz większą o 1
  6. }
  7. //Zapętlenie skryptu
  8. while($ratings = mysql_fetch_array( $data ))
  9. {
  10.  
  11. //Wykonawca
  12. Echo "<div id=lista><TABLE border=0 cellspacing=0 cellpadding=0><TR><TD><IMG SRC=img/nuta.jpg ALT=nuta></TD>
  13. <TD width=300px;>" .$ratings['wykonawca']." - ";
  14.  
  15. //Tytuł piosenki
  16. Echo "" .$ratings['tytul']."</TD>";
  17.  
  18. //Link do YouTube
  19. Echo "<TD width=50px;><a href=" .$ratings['adres']." target=_blank>Play</a></TD>";
  20.  
  21. //Wstawienie średniej arytmetycznej
  22. $current = $ratings['average'];
  23. Echo "<TD width=75px;>Ocena: " . round($current, 1) . "</TD>";


Cos mi sie strona nie chce wlaczac dalej
croc
Pętla for może być użyta tu ZAMIAST while.
  1. //Wstawienie informacji z MySQL
  2. $data = mysql_query("SELECT *, total / votes AS average FROM vote ORDER BY average DESC") or die(mysql_error());
  3.  
  4. //Zapętlenie skryptu
  5. for($i = 1; $ratings = mysql_fetch_array( $data ); ++$i)
  6. {
  7.  
  8. //Pozycja i wykonawca
  9. Echo "<div id=lista><TABLE border=0 cellspacing=0 cellpadding=0>
  10. <TR>
  11. <TD>" . $i . "</TD>
  12. <TD><IMG SRC=img/nuta.jpg ALT=nuta></TD>
  13. <TD width=300px;>" .$ratings['wykonawca']." - ";
  14.  
  15. //Tytuł piosenki
  16. Echo "" .$ratings['tytul']."</TD>";
  17.  
  18. //Link do YouTube
  19. Echo "<TD width=50px;><a href=" .$ratings['adres']." target=_blank>Play</a></TD>";
  20.  
  21. //Wstawienie średniej arytmetycznej
  22. $current = $ratings['average'];
  23. Echo "<TD width=75px;>Ocena: " . round($current, 1) . "</TD>";
imatix
Mam jeszcze problem ze skryptem komentarzy.
Mam na 1 stronie 2 skrypty komentarzy i jak naciskam wyslij to oba sie wlaczaja chociaz sa skierowane do innych tabel, jak to naprawic?

  1. <?
  2. mysql_connect("mysql3.yoyo.pl", "xxxx", "xxxx") or
  3. die ("Nie można połączyć się z MySQL");
  4. mysql_select_db ("xxxxxx") or
  5. die ("Nie można połączyć się z bazą cwphp");
  6.  
  7. $wynik = mysql_query ("SELECT * FROM komentarze WHERE ok=0 ".
  8. "ORDER BY nr DESC LIMIT 0,3");
  9. while ($wynik && $rekord = mysql_fetch_assoc ($wynik)) {
  10. print "<P><DIV id=news><DIV id=news_title><B>".$rekord['nick']. "</B></DIV><BR>"
  11. .$rekord['tresc']."</P>";
  12. print "<P ALIGN=RIGHT></I>".$rekord['data']."</I></P></DIV>\n";
  13. }
  14. ?>
  15. <?
  16.  
  17. $nick = addslashes(htmlspecialchars ($_POST['nick']));
  18. $tresc = addslashes(nl2br(htmlspecialchars ($_POST['tresc'])));
  19.  
  20. if ($nick && $tresc) {
  21. mysql_connect("mysql3.yoyo.pl", "xxxxx", "xxxxxx") or
  22. die ("Nie można połączyć się z MySQL");
  23. mysql_select_db ("xxxxx") or
  24. die ("Nie można połączyć się z bazą cwphp");
  25. $query = "INSERT INTO komentarze (nick, tresc, "." data) VALUES ('$nick', '$tresc', "." now());";
  26. $wynik = mysql_query ($query);
  27. print "Nowy post został dodany biggrin.gif";
  28.  
  29. } else {
  30. print "<H3>Dodaj news:</H3>";
  31. print "<FORM METHOD=POST><B>Dodaj nowy komentarz: </B><BR>";
  32. print "<INPUT TYPE=\"text\" NAME=\"nick\" VALUE=\"$nick\" ";
  33. print "<B>Treść:</B><BR><TEXTAREA NAME=\"tresc\" ";
  34. print "ROWS=6 COLS=60>$tresc</TEXTAREA><BR>";
  35. print "<INPUT TYPE=\"submit\" VALUE=\"Wyslij\">";
  36. print "</FORM>";
  37. }
  38.  
  39. ?>


  1. <?
  2.  
  3. $nick = addslashes(htmlspecialchars ($_POST['nick']));
  4. $tresc = addslashes(nl2br(htmlspecialchars ($_POST['tresc'])));
  5.  
  6. if ($nick && $tresc) {
  7. mysql_connect("mysql3.yoyo.pl", "xxxxx", "xxxxx") or
  8. die ("Nie można połączyć się z MySQL");
  9. mysql_select_db ("xxxxxx") or
  10. die ("Nie można połączyć się z bazą");
  11. $query = "INSERT INTO shoutbox (nick, tresc, "." data) VALUES ('$nick', '$tresc', "." now());";
  12. $wynik = mysql_query ($query);
  13. print "Nowy komentarz został dodany biggrin.gif";
  14.  
  15. } else {
  16. print "<IMG SRC=img/shoutbox.png>";
  17. print "<FORM METHOD=POST><B>Nick: </B><BR>";
  18. print "<INPUT TYPE=\"text\" NAME=\"nick\" VALUE=\"$nick\" ";
  19. print "<BR><B>Treść:</B><BR><TEXTAREA NAME=\"tresc\" ";
  20. print "ROWS=3 COLS=20>$tresc</TEXTAREA><BR>";
  21. print "<INPUT TYPE=\"submit\" VALUE=\"Wyslij\">";
  22. print "</FORM>";
  23. }
  24.  
  25. ?>
  26.  
  27. <?
  28. mysql_connect("mysql3.yoyo.pl", "xxxx", "xxxx") or
  29. die ("Nie można połączyć się z MySQL");
  30. mysql_select_db ("xxxxx") or
  31. die ("Nie można połączyć się z bazą cwphp");
  32.  
  33. $wynik = mysql_query ("SELECT * FROM shoutbox WHERE ok=0 ".
  34. "ORDER BY id DESC LIMIT 0,10");
  35. while ($wynik && $rekord = mysql_fetch_assoc ($wynik)) {
  36. print "<DIV id=shoutbox2><DIV id=shoutbox-tytul><B>".$rekord['nick']. "</B>, ".$rekord['data']."</DIV>&nbsp;"
  37. .$rekord['tresc']."";
  38. print "</DIV>\n";
  39. }
  40. ?>
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.