Solved

php, autocomplete, mysql and scriptaculous

Posted on 2006-06-21
6
1,044 Views
Last Modified: 2008-03-06
This is probably a pretty loaded question so I am going to award 500 points to someone who can give me an answer.

I have a form, that right now I am trying to do autocomplete on some of the form fields.  For learning and ease I am going to do them one at a time, although for the most part I should only need a nudge, push, shove on the first field.

HTML page is pretty simple.

<link rel="stylesheet" type="text/css" href="styles/form2.css" />
<script src="js/prototype.js" type="text/javascript"></script>
 <script src="js/scriptaculous.js" type="text/javascript"></script>
<input type="text" id="rep" name="_field" tabindex="1" value="">
<input  type="hidden" name="field" value="-1">
<div id="autocomplete_choices" class="autocomplete"></div>
            
<script type="text/javascript" language="javascript">
new Ajax.Autocompleter('rep','autocomplete_choices','acQuery.php', {paramName: "q",minChars: 1, afterUpdateElement: updateHidden });
</script>

I've gotten it to come over and pupulate the hidden drop down window with the proper starting letter, D returns Dave, Dan, Doug ..., however for some reason in my list it is leaving off some of the names that start with the letter, D doesnt return Deborah, Dean and so on, not sure if its a considence or not but the ones near the top get dropped, same with other letters.

$sql='SELECT name, surname FROM dealer';
$result = mysql_query($sql);
print('<ul>' . "\n");  
while ($row = mysql_fetch_assoc($result)) {
$fullname=array($row['name'].' '.$row['surname']);
foreach($fullname as $k=>$v) {
if(strpos($v,strtolower($q))===0) {
print("<li id=\"auto_$k\">" . ucwords($v) . '</li>' . "\n");
}
}
}
print('</ul>');
0
Comment
Question by:iceman19330
  • 3
  • 3
6 Comments
 
LVL 8

Accepted Solution

by:
Autogard earned 500 total points
ID: 16953666
First of all, you should try to optimize your SQL query and that might help if it is a programming error:

$result = mysql_query('SELECT CONCAT(name, " ", surname) as wholename FROM dealer WHERE wholename LIKE "$q%"');
print('<ul>' . "\n");  
while ($row = mysql_fetch_assoc($result))
{
    print("<li id=\"auto_$k\">" . $row['wholename'] . '</li>' . "\n");
}
print('</ul>');

That cleans up the code a lot more and gets rid of the extra loop and array constructs.

Note: I'm not sure if the second "wholename" in the query will pick up the alias created at the first one.  If not, try

$result = mysql_query('SELECT CONCAT(name, " ", surname) as wholename FROM dealer WHERE CONCAT(name, " ", surname) LIKE "$q%"');

instead.

Let me know if this helps at all.
0
 

Author Comment

by:iceman19330
ID: 16953669
When I test a static list generated from the php script it will pull up all those missing names, one problem there is that it doesnt sort and just throws up every name.  So there has to be something in the PHP thats dropping those first section of names.
0
 
LVL 8

Expert Comment

by:Autogard
ID: 16953686
Whups, I notieced you are using $k in your print of the list item.  To fix that just add a counter for the loop and you can print that out instead:

$counter = 0;
while (........) {
    print("<li id=\"auto_$counter\">" ........
    $counter += 1;
}
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:iceman19330
ID: 16953784
Yeah I got that, thanks for the query worked really great, wasnt thinking about that so I didnt look at ways to work it with the query.

0
 
LVL 8

Expert Comment

by:Autogard
ID: 16953802
Cool.  Glad it helped!
0
 

Author Comment

by:iceman19330
ID: 16953819
No problem, now I have to get my php and scriptaculous question answered. :)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now