Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [MySQL][PHP]Problem z wyswietlaniem autosugesti
karolo_k
post 11.02.2012, 10:24:50
Post #1





Grupa: Zarejestrowani
Postów: 84
Pomógł: 5
Dołączył: 12.01.2010

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


Witam, długo czasu szukałem takiego skryptu aż w końcu znalazłem. Jest to autosugestia mniej wiecej taka jaką mamy teraz w googlach.
Chce ten skrypt wykorzystać do podpowiadań adresów IP i emaili w moim panelu. Jadnak mam jeden problem bo poprzerobieniu skryptu na moje potrzeby pojawił się jakiś błąd z którym nie mogę sobie sam porawdzić.
Oto kod :
  1. <?php
  2.  
  3.  
  4. $db = mysql_connect('localhost', 'root' ,'');
  5. mysql_select_db('kraje');
  6.  
  7.  
  8.  
  9.  
  10. if(!$db) {
  11.  
  12. echo 'Could not connect to the database.';
  13. } else {
  14.  
  15. if(isset($_POST['queryString'])) {
  16. $abc = $_POST['queryString'];
  17. $queryString = mysql_real_escape_string($abc);
  18.  
  19. if(strlen($queryString) >0) {
  20.  
  21. $query = mysql_query("SELECT country FROM countries WHERE country LIKE '$queryString%' LIMIT 10");
  22. if($query) {
  23. echo '<ul>';
  24.  
  25. while ($result = $query ->fetch_object()) {
  26. echo '<li onClick="fill(\''.addslashes($result->country).'\');">'.$result->country.'</li>';
  27. }
  28. echo '</ul>';
  29.  
  30. } else {
  31. echo 'OOPS we had a problem :(';
  32. }
  33. } else {
  34. // do nothing
  35. }
  36. } else {
  37. echo 'There should be no direct access to this script!';
  38. }
  39. }
  40. ?>

A tutaj jest oryginalny kod pliku:
  1. <?php
  2. $db = mysql('localhost', 'root' ,'', 'kraje');
  3.  
  4. if(!$db) {
  5.  
  6. echo 'Could not connect to the database.';
  7. } else {
  8.  
  9. if(isset($_POST['queryString'])) {
  10. $queryString = $db->real_escape_string($_POST['queryString']);
  11.  
  12. if(strlen($queryString) >0) {
  13.  
  14. $query = $db->query("SELECT country FROM countries WHERE country LIKE '$queryString%' LIMIT 10");
  15. if($query) {
  16. echo '<ul>';
  17. while ($result = $query ->fetch_object()) {
  18. echo '<li onClick="fill(\''.addslashes($result->country).'\');">'.$result->country.'</li>';
  19. }
  20. echo '</ul>';
  21.  
  22. } else {
  23. echo 'OOPS we had a problem :(';
  24. }
  25. } else {
  26. // do nothing
  27. }
  28. } else {
  29. echo 'There should be no direct access to this script!';
  30. }
  31. }
  32. ?>

Mój problem jest takiej natury:
http://imageshack.us/photo/my-images/37/problemmm.png/
Bardzo proszę o pomoc.
Go to the top of the page
+Quote Post
CuteOne
post 11.02.2012, 10:53:49
Post #2





Grupa: Zarejestrowani
Postów: 2 958
Pomógł: 574
Dołączył: 23.09.2008
Skąd: wiesz, że tu jestem?

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


while ($result = $query ->fetch_object()) {

podmień na

while ($result = mysql_fetch_assoc($query)) {
Go to the top of the page
+Quote Post
karolo_k
post 11.02.2012, 11:00:27
Post #3





Grupa: Zarejestrowani
Postów: 84
Pomógł: 5
Dołączył: 12.01.2010

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


Rozwiązało to tylko część problemu.
Teraz już lista się rozwija ale nie wyświetla żadnych wartości ale lista dopasowuje swoją długość do ilości wyników, więc coś działa tylko nie wyświetla dostępnych opcji ale wie ile ich jest.

To oryginalny kod index.php
  1.  
  2. <script>
  3. function suggest(inputString){
  4. if(inputString.length == 0) {
  5. $('#suggestions').fadeOut();
  6. } else {
  7. $('#country').addClass('load');
  8. $.post("autosuggest.php", {queryString: ""+inputString+""}, function(data){
  9. if(data.length >0) {
  10. $('#suggestions').fadeIn();
  11. $('#suggestionsList').html(data);
  12. $('#country').removeClass('load');
  13. }
  14. });
  15. }
  16. }
  17.  
  18. function fill(thisValue) {
  19. $('#country').val(thisValue);
  20. setTimeout("$('#suggestions').fadeOut();", 600);
  21. }
  22.  
  23. </script>
  24. <body>
  25.  
  26.  
  27.  
  28.  
  29. <form id="form" action="#">
  30. <div id="suggest">Start to type a country: <br />
  31. <input type="text" size="25" value="" id="country" onkeyup="suggest(this.value);" onblur="fill();" class="" />
  32.  
  33. <div class="suggestionsBox" id="suggestions" style="display: none;"> <img src="arrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
  34. <div class="suggestionList" id="suggestionsList"> &nbsp; </div>
  35. </div>
  36. </div>
  37. </form>
  38.  
  39.  
  40.  
  41. </body>
  42. </html>

A to oryginalny kod sugestera:
  1. <?php
  2. $db = new mysqli('localhost', 'root' ,'', 'kraje');
  3.  
  4. if(!$db) {
  5.  
  6. echo 'Could not connect to the database.';
  7. } else {
  8.  
  9. if(isset($_POST['queryString'])) {
  10. $queryString = $db->real_escape_string($_POST['queryString']);
  11.  
  12. if(strlen($queryString) >0) {
  13.  
  14. $query = $db->query("SELECT country FROM countries WHERE country LIKE '$queryString%' LIMIT 10");
  15. if($query) {
  16. echo '<ul>';
  17. while ($result = $query ->fetch_object()) {
  18. echo '<li onClick="fill(\''.addslashes($result->country).'\');">'.$result->country.'</li>';
  19. }
  20. echo '</ul>';
  21.  
  22. } else {
  23. echo 'OOPS we had a problem :(';
  24. }
  25. } else {
  26. // do nothing
  27. }
  28. } else {
  29. echo 'There should be no direct access to this script!';
  30. }
  31. }
  32. ?>
Nie wiem dlaczego ale każda niewielka zmiana w tym kodzie powoduje to że lista nie wyświetla dostępnych wartości tylko oryginalny kod działa. Problem jest nawet jak próbowałem zmienić bazę żeby mi szukało np adresów IP co_jest.gif

Ten post edytował karolo_k 11.02.2012, 21:37:28
Go to the top of the page
+Quote Post
-Gość-
post 11.02.2012, 21:40:18
Post #4





Goście







test test
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: 17.06.2025 - 01:33