Mam skrypt do oceniania zdjęć (na zasadzie lewa - prawa), próbowałem dodać możliwość uploadu zdjęć formularzem, przez użytkownika (bo obecnie jest plik, który dodaje garść zdjęć z folderu)
Niestety, po kilku zabiegach stanęło na tym, że zdjęcie niby się dodaje, w bazie jest widoczne, lecz brak nazwy i zamiast widocznego obrazka jest pusta ramka.

Druga kwestia to komentarze pod "parami" zdjęć, jak to sklecić?
Poniżej pliki:

functions.php
  1. <?php
  2.  
  3. // Calculate the expected % outcome
  4. function expected($Rb, $Ra) {
  5. return 1/(1 + pow(10, ($Rb-$Ra)/400));
  6. }
  7.  
  8. // Calculate the new winnner score
  9. function win($score, $expected, $k = 24) {
  10. return $score + $k * (1-$expected);
  11. }
  12.  
  13. // Calculate the new loser score
  14. function loss($score, $expected, $k = 24) {
  15. return $score + $k * (0-$expected);
  16. }
  17.  
  18. ?>


index.php
  1. <?php
  2.  
  3. include('mysql.php');
  4. include('functions.php');
  5.  
  6.  
  7. // Get random 2
  8. $query="SELECT * FROM images ORDER BY RAND() LIMIT 0,2";
  9. $result = @mysql_query($query);
  10.  
  11. while($row = mysql_fetch_object($result)) {
  12. $images[] = (object) $row;
  13. }
  14.  
  15.  
  16. // Get the top10
  17. $result = mysql_query("SELECT *, ROUND(score/(1+(losses/wins))) AS performance FROM images ORDER BY ROUND(score/(1+(losses/wins))) DESC LIMIT 0,10");
  18. while($row = mysql_fetch_object($result)) $top_ratings[] = (object) $row;
  19.  
  20.  
  21. // Close the connection
  22.  
  23.  
  24. ?>
  25. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  26. <html xmlns="http://www.w3.org/1999/xhtml">
  27. <head>
  28. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  29. <title>Facemash-Alike Script</title>
  30. <style type="text/css">
  31.  
  32. body, html {font-family:Arial, Helvetica, sans-serif;width:100%;margin:0;padding:0;text-align:center;}
  33. h1 {background-color:#600;color:#fff;padding:20px 0;margin:0;}
  34. a img {border:0; height:300px; width:500px;}
  35. td {font-size:11px;}
  36. .image {background-color:#eee;border:1px solid #ddd;border-bottom:1px solid #bbb;padding:5px;}
  37.  
  38. </style>
  39. </head>
  40.  
  41. <body>
  42.  
  43.  
  44. <h1>FACEMASH-ALIKE</h1>
  45. <h3>Were we let in for our looks? No. Will we be judged on them? Yes.</h3>
  46. <h2>Who's hotter? Click to choose.</h2>
  47.  
  48. <center>
  49. <table>
  50. <tr>
  51. <td valign="top" class="image"><a h[spam]"rate.php?winner=<?=$images[0]->image_id?>&loser=<?=$images[1]->image_id?>"><img src="images/<?=$images[0]->filename?>" /></a></td>
  52. <td valign="top" class="image"><a h[spam]"rate.php?winner=<?=$images[1]->image_id?>&loser=<?=$images[0]->image_id?>"><img src="images/<?=$images[1]->filename?>" /></a></td>
  53. </tr>
  54. <tr>
  55. <td>Won: <?=$images[0]->wins?>, Lost: <?=$images[0]->losses?></td>
  56. <td>Won: <?=$images[1]->wins?>, Lost: <?=$images[1]->losses?></td>
  57. </tr>
  58. <tr>
  59. <td>Score: <?=$images[0]->score?></td>
  60. <td>Score: <?=$images[1]->score?></td>
  61. </tr>
  62. <tr>
  63. <td>Expected: <?=round(expected($images[1]->score, $images[0]->score), 4)?></td>
  64. <td>Expected: <?=round(expected($images[0]->score, $images[1]->score), 4)?></td>
  65. </tr>
  66. </table>
  67. </center>
  68.  
  69. <h2>Top Rated</h2>
  70. <center>
  71. <table>
  72. <tr>
  73. <? foreach($top_ratings as $key => $image) : ?>
  74. <td valign="top"><img src="images/<?=$image->filename?>" width="70" /></td>
  75. <? endforeach ?>
  76. </tr>
  77. <? /* Remove this to see the scoring
  78.   <tr>
  79.   <? foreach($top_ratings as $key => $image) : ?>
  80.   <td valign="top">Score: <?=$image->score?></td>
  81.   <? endforeach ?>
  82.   </tr>
  83.   <tr>
  84.   <? foreach($top_ratings as $key => $image) : ?>
  85.   <td valign="top">Performance: <?=$image->performance?></td>
  86.   <? endforeach ?>
  87.   </tr>
  88.   <tr>
  89.   <? foreach($top_ratings as $key => $image) : ?>
  90.   <td valign="top">Won: <?=$image->wins?></td>
  91.   <? endforeach ?>
  92.   </tr>
  93.   <tr>
  94.   <? foreach($top_ratings as $key => $image) : ?>
  95.   <td valign="top">Lost: <?=$image->losses?></td>
  96.   <? endforeach ?>
  97.   </tr>
  98.   */ ?>
  99. </table>
  100. </center>
  101.  
  102. </body>
  103. </html>


mysql.php
  1. <?php
  2.  
  3. $user = "root";
  4. $password = "";
  5. $database = "baza";
  6. $host = "localhost";
  7.  
  8. mysql_connect($host,$user,$password);
  9. mysql_select_db($database) or die( "Unable to select database");
  10.  
  11. ?>


rate.php
  1. <?php
  2.  
  3.  
  4. include('mysql.php');
  5. include('functions.php');
  6.  
  7.  
  8. // If rating - update the database
  9. if ($_GET['winner'] && $_GET['loser']) {
  10.  
  11.  
  12. // Get the winner
  13. $result = mysql_query("SELECT * FROM images WHERE image_id = ".$_GET['winner']." ");
  14. $winner = mysql_fetch_object($result);
  15.  
  16.  
  17. // Get the loser
  18. $result = mysql_query("SELECT * FROM images WHERE image_id = ".$_GET['loser']." ");
  19. $loser = mysql_fetch_object($result);
  20.  
  21.  
  22. // Update the winner score
  23. $winner_expected = expected($loser->score, $winner->score);
  24. $winner_new_score = win($winner->score, $winner_expected);
  25. //test print "Winner: ".$winner->score." - ".$winner_new_score." - ".$winner_expected."<br>";
  26. mysql_query("UPDATE images SET score = ".$winner_new_score.", wins = wins+1 WHERE image_id = ".$_GET['winner']);
  27.  
  28.  
  29. // Update the loser score
  30. $loser_expected = expected($winner->score, $loser->score);
  31. $loser_new_score = loss($loser->score, $loser_expected);
  32. //test print "Loser: ".$loser->score." - ".$loser_new_score." - ".$loser_expected."<br>";
  33. mysql_query("UPDATE images SET score = ".$loser_new_score.", losses = losses+1 WHERE image_id = ".$_GET['loser']);
  34.  
  35.  
  36. // Insert battle
  37. mysql_query("INSERT INTO battles SET winner = ".$_GET['winner'].", loser = ".$_GET['loser']." ");
  38.  
  39.  
  40. // Back to the frontpage
  41. header('location: /facemash-alike/index.php');
  42.  
  43. }
  44.  
  45.  
  46. ?>


no i plik do ładowania obrazków (domyślnie):

install_images.php
  1. <?php
  2.  
  3. include('mysql.php');
  4.  
  5. if ($handle = opendir('images')) {
  6.  
  7. /* This is the correct way to loop over the directory. */
  8. while (false !== ($file = readdir($handle))) {
  9. if($file!='.' && $file!='..') {
  10. $images[] = "('".$file."')";
  11. }
  12. }
  13.  
  14. closedir($handle);
  15. }
  16.  
  17. $query = "INSERT INTO images (filename) VALUES ".implode(',', $images)." ";
  18. if (!mysql_query($query)) {
  19. }
  20. else {
  21. print "finished installing your images!";
  22. }
  23.  
  24.  
  25. ?>


po moich zabiegach:
  1. <?php
  2.  
  3. include('mysql.php');
  4.  
  5. $images=$_POST['images'];
  6.  
  7. $query = "INSERT INTO images (filename) VALUES ('$images')";
  8. mysql_query($query);
  9.  
  10. echo "Dane zostały wprowadzone poprawnie";
  11.  
  12.  
  13.  
  14. //$query = "INSERT INTO przedmioty VALUES ('','$nazwa','$wykladowca')";
  15. ?>


do tego formularz:
  1. <?php
  2. echo "<FORM ACTION='install_images.php' METHOD='POST' ENCTYPE='multipart/form-data'>";
  3. echo " Zdjęcie: <INPUT type='file' name='images'><br>";
  4. echo "<INPUT TYPE='hidden' NAME='username' value= '$l';><br>";
  5. echo "<input type='hidden' name='MAX_FILE_SIZE' value='32500' />"; /* maksymalna wielkość pliku w bajtach */
  6. echo "<button type='submit' name='ok' value='wyślij zdjęcie do bazy'>";
  7. echo "<img src='zdj/wyslij.jpg' width='50' height='14' alt='wyślij zdjęcie do bazy danych'>";
  8. echo "</button>";
  9. echo "</form>";
  10. ?>


MySQL:
  1. CREATE TABLE IF NOT EXISTS `battles` (
  2. `battle_id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  3. `winner` bigint(20) UNSIGNED NOT NULL,
  4. `loser` bigint(20) UNSIGNED NOT NULL,
  5. PRIMARY KEY (`battle_id`),
  6. KEY `winner` (`winner`)
  7. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
  8.  
  9.  
  10. CREATE TABLE IF NOT EXISTS `images` (
  11. `image_id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  12. `filename` varchar(255) NOT NULL,
  13. `score` int(10) UNSIGNED NOT NULL DEFAULT '1500',
  14. `wins` int(10) UNSIGNED NOT NULL DEFAULT '0',
  15. `losses` int(10) UNSIGNED NOT NULL DEFAULT '0',
  16. PRIMARY KEY (`image_id`)
  17. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;


Aby umieścić zdjęcia w bazie, trzeba stworzyć folder "images" i tam je wrzucić, następnie plikiem install_images.php ładuje fotki...

Pogrzebie ktoś w tym?