Solved

php, autocomplete, mysql and scriptaculous

Posted on 2006-06-21
6
1,047 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

910 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

19 Experts available now in Live!

Get 1:1 Help Now