Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [MySQL][PHP]Chmura tagów
Forum PHP.pl > Forum > Przedszkole
szczalpi
Witam
Poniżej wkleiłem skrypt, który znalazłem na necie. Skrypt ma tworzyć chmurę lecz nie działa. Jeżeli dodaję ręcznie rekord do mysql to wpis pojawia się w bazie. NIestety kiedy próbuje ze strony to nie żadnego efektu.
  1. <?// connect to the database
  2. $conn = mysql_connect('', 'i', '');
  3. if (!$conn or !mysql_select_db('', $conn)) die('cannot connect to db');
  4.  
  5. // handle new searches
  6. if (isset($_GET['action']) and $_GET['action'] == 'Search')
  7. {
  8. // get the current time
  9. $now = date("Y-m-d H:i:s");
  10.  
  11. // get the submitted term and prepare it for the database query
  12. $term = mysql_real_escape_string(strip_tags(trim($_GET['term'])));
  13.  
  14. // check if the term has been submitted before
  15. if (mysql_result(mysql_query("SELECT COUNT(id) FROM search WHERE term = '$term'"), 0) > 0)
  16. {
  17. // the term exists - update the counter and the last search timestamp
  18. mysql_query("UPDATE search SET counter = counter+1, last_search = '$now' WHERE term = '$term'");
  19. } else {
  20. // the term does not exist - insert a new record
  21. mysql_query("INSERT INTO search (term, last_search) VALUES ('$term', '$now')");
  22. }
  23. }
  24.  
  25. // prepare the tag cloud array for display
  26. $terms = array(); // create empty array
  27. $maximum = 1; // $maximum is the highest counter for a search term
  28.  
  29. $query = mysql_query("SELECT term, counter FROM search ORDER BY counter DESC LIMIT 30");
  30.  
  31. while ($row = mysql_fetch_array($query))
  32. {
  33. $term = $row['term'];
  34. $counter = $row['counter'];
  35.  
  36. // update $maximum if this term is more popular than the previous terms
  37. if ($counter > $maximum) $maximum = $counter;
  38.  
  39. $terms[] = array('term' => $term, 'counter' => $counter);
  40.  
  41. }
  42.  
  43. // shuffle terms unless you want to retain the order of highest to lowest
  44. shuffle($terms);
  45.  
  46. ?>
  47. <!DOCTYPE html>
  48. <html>
  49. <head>
  50. <title>Tag Cloud Example</title>
  51. <style type="text/css">
  52. #tagcloud {
  53. width: 300px;
  54. background:#CFE3FF;
  55. color:#0066FF;
  56. padding: 10px;
  57. border: 1px solid #559DFF;
  58. text-align:center;
  59. -moz-border-radius: 4px;
  60. -webkit-border-radius: 4px;
  61. border-radius: 4px;
  62. }
  63.  
  64. #tagcloud a:link, #tagcloud a:visited {
  65. text-decoration:none;
  66. color: #333;
  67. }
  68.  
  69. #tagcloud a:hover {
  70. text-decoration: underline;
  71. }
  72.  
  73. #tagcloud span {
  74. padding: 4px;
  75. }
  76.  
  77. #tagcloud .smallest {
  78. font-size: x-small;
  79. }
  80.  
  81. #tagcloud .small {
  82. font-size: small;
  83. }
  84.  
  85. #tagcloud .medium {
  86. font-size:medium;
  87. }
  88.  
  89. #tagcloud .large {
  90. font-size:large;
  91. }
  92.  
  93. #tagcloud .largest {
  94. font-size:larger;
  95. }
  96. </style>
  97. </head>
  98.  
  99. <body>
  100. <h1>Search</h1>
  101. <form id="search" method="get" action="?action=Search">
  102. <input type="text" name="term" id="term" />
  103. <input type="submit" name="submit" id="submit" value="search" />
  104. </form>
  105.  
  106. <h2>Popular Searches</h2>
  107. <div id="tagcloud">
  108. <?php
  109. // start looping through the tags
  110. foreach ($terms as $term):
  111. // determine the popularity of this term as a percentage
  112. $percent = floor(($term['counter'] / $maximum) * 100);
  113.  
  114. // determine the class for this term based on the percentage
  115. if ($percent < 20):
  116. $class = 'smallest';
  117. elseif ($percent >= 20 and $percent < 40):
  118. $class = 'small';
  119. elseif ($percent >= 40 and $percent < 60):
  120. $class = 'medium';
  121. elseif ($percent >= 60 and $percent < 80):
  122. $class = 'large';
  123. else:
  124. $class = 'largest';
  125. endif;
  126. ?>
  127. <span class="<?php echo $class; ?>">
  128. <a href="szukaj.php?search=<?php echo urlencode($term['term']); ?>"><?php echo $term['term']; ?></a>
  129. </span>
  130. <?php endforeach; ?>
  131. </div>
  132. </body>
  133. </html>

Proszę o pomoc.
Pozdrawiam
IProSoft
Struktura w bazie jest poprawna?
ID ma auto_increment?
Spróbuj:
Kod
mysql_query("INSERT INTO search (id,term, last_search, counter) VALUES (NULL,'$term', '$now', 0)");
szczalpi
Niesety zmieniłem i nie działa.

Id ma auto_incement.
IProSoft
Zamień:

  1. mysql_query("INSERT INTO search (term, last_search) VALUES ('$term', '$now')");


na

  1. if(!mysql_query("INSERT INTO search (term, last_search) VALUES ('$term', '$now')")){
  2. }


i napisz co zwraca.
szczalpi
Zwraca Array pusty.

Dodaje do bazy Napis "Array"
IProSoft
Wpis na 100% nie zostaje dodany?
Pusta tablica oznacza, że wpis jest dodany.

Spróbuj inaczej:
  1. if(!mysql_query("INSERT INTO search (term, last_search) VALUES ('$term', '$now')")){
  2. echo "INSERT INTO search (term, last_search) VALUES ('$term', '$now')";exit;
  3. }

To co wypisze Ci na ekranie wykonaj przez phpmyadmin i napisz czy jest ok.
szczalpi
Po dodaniu
  1. INSERT INTO search (term, last_search) VALUES ('$term', '$now')

Zwraca mi "Wstawionych rekordów: 1.
Identyfikator wstawionego rekordu: 19 ( Wykonanie zapytania trwało 0.0004 sekund(y) )"
IProSoft
Wrzuciłeś:
INSERT INTO search (term, last_search) VALUES ('$term', '$now')
czy to co wyświetlił skrypt php?
Miało być to drugie.
szczalpi
Wrzuciłem do phpmyadmin


przez skrypt zero reakcji

podaje adres sktyptu :
http://noclegi.eszukaj.com/szukaj.php?search=sssssss
IProSoft
Zamień w oryginalnym skrypcie:

  1. if (isset($_GET['action']) and $_GET['action'] == 'Search')


na

  1. if (isset($_GET['term']) and !empty($_GET['term']))
szczalpi
Dziekuje bardzo 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.