Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [MySQL][PHP]Problem ze skryptem - głosowanie z MySQL
imatix
post 13.05.2010, 09:43:31
Post #1





Grupa: Zarejestrowani
Postów: 34
Pomógł: 0
Dołączył: 19.10.2008

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


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

Ten post edytował imatix 13.05.2010, 09:44:06
Go to the top of the page
+Quote Post
croc
post 13.05.2010, 09:56:26
Post #2





Grupa: Zarejestrowani
Postów: 706
Pomógł: 108
Dołączył: 12.03.2010

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


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'];
Go to the top of the page
+Quote Post
imatix
post 13.05.2010, 10:16:18
Post #3





Grupa: Zarejestrowani
Postów: 34
Pomógł: 0
Dołączył: 19.10.2008

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


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?
Go to the top of the page
+Quote Post
croc
post 13.05.2010, 10:26:10
Post #4





Grupa: Zarejestrowani
Postów: 706
Pomógł: 108
Dołączył: 12.03.2010

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


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);
Go to the top of the page
+Quote Post
imatix
post 13.05.2010, 10:31:25
Post #5





Grupa: Zarejestrowani
Postów: 34
Pomógł: 0
Dołączył: 19.10.2008

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


Nom dzieki bardzo biggrin.gif
A jak zrobić żeby rekordy pokazywalo zgodnie ze srednia a nie ID ?
Go to the top of the page
+Quote Post
croc
post 13.05.2010, 10:40:44
Post #6





Grupa: Zarejestrowani
Postów: 706
Pomógł: 108
Dołączył: 12.03.2010

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


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>";

Go to the top of the page
+Quote Post
imatix
post 14.05.2010, 16:42:07
Post #7





Grupa: Zarejestrowani
Postów: 34
Pomógł: 0
Dołączył: 19.10.2008

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


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??

Ten post edytował imatix 13.05.2010, 15:14:54
Go to the top of the page
+Quote Post
croc
post 14.05.2010, 20:31:23
Post #8





Grupa: Zarejestrowani
Postów: 706
Pomógł: 108
Dołączył: 12.03.2010

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


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.
Go to the top of the page
+Quote Post
imatix
post 16.05.2010, 10:06:19
Post #9





Grupa: Zarejestrowani
Postów: 34
Pomógł: 0
Dołączył: 19.10.2008

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


  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
Go to the top of the page
+Quote Post
croc
post 16.05.2010, 20:42:52
Post #10





Grupa: Zarejestrowani
Postów: 706
Pomógł: 108
Dołączył: 12.03.2010

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


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>";
Go to the top of the page
+Quote Post
imatix
post 17.05.2010, 10:18:18
Post #11





Grupa: Zarejestrowani
Postów: 34
Pomógł: 0
Dołączył: 19.10.2008

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


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. ?>
Go to the top of the page
+Quote Post

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 Wersja Lo-Fi Aktualny czas: 24.07.2025 - 20:16