Solved

php, autocomplete, mysql and scriptaculous

Posted on 2006-06-21
6
1,049 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

831 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