Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP][HTML] sugesita do wyszukiwarki
Crovean
post
Post #1





Grupa: Zarejestrowani
Postów: 16
Pomógł: 0
Dołączył: 17.11.2009

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


Mam problem z sugestia co do wyszukiwarki... wyswietla mi przy kazdym wyszukiwaniu dane z bazy ale też błąd:
Notice: Undefined variable: str in suggest.php on line 26
Czyli w: $str .= $wiersz['title']."\n";
Czasami też jak wpisze trochę więcej to wyświetla mi:
Notice: Undefined variable: str in suggest.php on line 28
Czyli w: $str .= "empty \n";


suggest.php
  1. <?php
  2. function lacz_bd()
  3. {
  4. $db = new mysqli('localhost','uzyt_zak','haslo','uzytkownik');
  5. if (! $db)
  6. return false;
  7. $db->autocommit(TRUE);
  8. return $db;
  9. }
  10. $search = addslashes($_GET['search']);
  11. if(!empty($search)){
  12.  
  13. $db = lacz_bd();
  14. if (mysqli_connect_errno() ===0){
  15. $db -> query("SET NAMES 'utf8'");
  16. }
  17. $zapytanie = "select title from suggest where title LIKE '%$search%' LIMIT 10";
  18. $wynik = $db->query($zapytanie);
  19. $ile_znalezionych = $wynik->num_rows;
  20.  
  21. for ($i=0; $i<$ile_znalezionych; $i++)
  22. {
  23. $wiersz = $wynik->fetch_assoc();
  24. $str .= $wiersz['title']."\n";
  25. }
  26. $str .= "empty \n";
  27.  
  28. echo rtrim($str);
  29. }
  30. ?>


suggest.js
  1. function searchSuggest(){
  2. var str = encodeURI(document.getElementById('searchinput').value);
  3. var myAjax = new Ajax.Request(
  4. 'suggest.php',
  5. {
  6. method: 'get',
  7. parameters: "search="+str,
  8. onComplete: showResponse,
  9. onFailure: showAlert
  10. });
  11.  
  12. }
  13. function showResponse(text){
  14. if(text.responseText==''){
  15. var search_suggest = document.getElementById("search_suggest");
  16. search_suggest.style.visibility = "visible";
  17. var ss = document.getElementById('search_suggest');
  18. ss.innerHTML = '';
  19. }else{
  20. var search_suggest = document.getElementById("search_suggest");
  21. search_suggest.style.visibility = "visible";
  22. var ss = document.getElementById('search_suggest');
  23. ss.innerHTML = '';
  24. var str = text.responseText.split("\n");
  25. for(i=0; i < str.length - 1; i++)
  26. {
  27. var suggest = '<div onmouseover="java script:suggestOver(this);" ';
  28. suggest += 'onmouseout="java script:suggestOut(this);" ';
  29. suggest += 'onclick="java script:setSearch(this.innerHTML);" ';
  30. suggest += 'class="suggest_link">' + str[i] + '</div>';
  31. ss.innerHTML += suggest;
  32. }
  33. }
  34. }
  35.  
  36. function showAlert(MyRequest) {
  37. alert("Operacja nie powiodła się");
  38. }
  39. function suggestOver(div_value) {
  40.  
  41. div_value.className = 'suggest_link_over';
  42. }
  43. function suggestOut(div_value) {
  44.  
  45. div_value.className = 'suggest_link';
  46. }
  47. function setSearch(value) {
  48. var search_suggest = document.getElementById("search_suggest");
  49. search_suggest.style.visibility = "hidden";
  50. document.getElementById('searchinput').value = value;
  51. document.getElementById('search_suggest').innerHTML = '';
  52.  
  53. }



styl.css
  1. #search fieldset {
  2. margin: 0;
  3. padding: 0;
  4. border: none;
  5. }
  6. #search legend {
  7. display: none;
  8. }
  9. .search {
  10. background:url("grafika/searchtext.jpg");
  11. border:none;
  12. background-repeat:no-repeat;
  13. width:94px;
  14. height:21px;
  15. cursor:pointer;
  16. }
  17.  
  18. .suggest_link {
  19. background-color: #f9f8f0;
  20. padding: 2px 6px 2px 6px;
  21. border-bottom: #E9E9E9 solid 1px;
  22. padding:5px 5px 5px 5px;
  23. }
  24. .suggest_link_over {
  25. background-color: #ffffff;
  26. padding: 2px 6px 2px 6px;
  27. cursor:pointer;
  28. border-bottom: #E9E9E9 solid 1px;
  29. padding:5px 5px 5px 5px;
  30. }
  31. #search_suggest {
  32. width:208px;
  33. position: absolute;
  34. background-color: #FFFFFF;
  35. text-align: left;
  36. border: #edebd5 solid 1px;
  37. visibility:hidden;
  38.  
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 4)
darko
post
Post #2





Grupa: Zarejestrowani
Postów: 2 885
Pomógł: 463
Dołączył: 3.10.2009
Skąd: Wrocław

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


Zamień \n na <br> albo usuń w ogóle.


--------------------
Nie pomagam na pw, tylko forum.
Go to the top of the page
+Quote Post
blooregard
post
Post #3


Newsman


Grupa: Moderatorzy
Postów: 2 033
Pomógł: 290
Dołączył: 21.12.2007
Skąd: Łódź




Cytat
Notice: Undefined variable: str in suggest.php on line 26

Notice to nie błąd.
Jest to uwaga interpretera, że użyłeś zmiennej, która nigdzie wcześniej nie została zadeklarowana.
Taka wskazówka, by zawsze deklarować zmienne przed ich użyciem nadając im (inicjalizując je) jakąś wartością, np.:
  1. <?php
  2. $str = "";
  3. ...
  4. //reszta kodu



--------------------
Life's simple... You make choices and don't look back...
Go to the top of the page
+Quote Post
ChrisB
post
Post #4





Grupa: Zarejestrowani
Postów: 73
Pomógł: 4
Dołączył: 13.01.2004
Skąd: Bielsko-Biała

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


używasz .= czyli łączenia stringa
żeby to zadziałało bez notice - musisz gdzies powyżej dać $str=''; i dopiero potem dołączać pozostałe części:
  1.  
  2. $str=''; <------------------------------------------------------------------------
  3. for ($i=0; $i<$ile_znalezionych; $i++)
  4. {
  5. $wiersz = $wynik->fetch_assoc();
  6. $str .= $wiersz['title']."\n";
  7. }
  8. $str .= "empty \n";
  9. echo rtrim($str);

załatwi sprawe

Ten post edytował ChrisB 2.12.2009, 23:51:22


--------------------
gragieldowa.pl
Go to the top of the page
+Quote Post
Crovean
post
Post #5





Grupa: Zarejestrowani
Postów: 16
Pomógł: 0
Dołączył: 17.11.2009

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


O działa dzięki wielkie za pomoc smile.gif
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 Aktualny czas: 21.08.2025 - 10:36