?
Solved

php, autocomplete, mysql and scriptaculous

Posted on 2006-06-21
6
Medium Priority
?
1,059 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 8

Accepted Solution

by:
Autogard earned 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

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…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

770 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