Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Auto Complete w/ Php MySQL & Jquery

    Hello,

    I'm attempting to build something that has auto-complete. This combines PHP, MySQL and Jquery so I'm not 100% sure what section of the forum this should go in.

    Here is the deal. I have two columns in my table:

    FirstName | LastName

    Obviously FirstName could have duplicates (there will probably eventually be more than one John in the table) so I want the auto complete to pull both first and last name. Problem is, since they're in different columns, I can't quite get it to work correctly.

    Here is HTML with the auto complete form (javascript included):

    Code:
    <script>
    $(document).ready(function() {
    
    $('input.FirstName').typeahead({
      name: 'FirstName',
      remote : 'name.php?query=%QUERY'
    
    });
    
    })
    </script>
    </head>
    <body>
    <h1>Select data from Database</h1>
    <input type="text" name="FirstName" size="20" class="FirstName">
    </body>
    For the name.php file with the included query i have:

    PHP Code:
    <?php
    $connection
    =mysql_connect($host,$uname,$pass) or die("connection in not ready <br>");
    $result=mysql_select_db($database) or die("database cannot be selected <br>");

    if (isset(
    $_REQUEST['query'])) {

        
    $query $_REQUEST['query'];
        
        
    $sql mysql_query ("SELECT * FROM name WHERE FirstName LIKE '%{$query}%'");
        
    $array = array();
        
        while (
    $row mysql_fetch_assoc($sql)) {
            
    $array[] = $row['FirstName'];    
            
    $array[] = $row['LastName'];            
        }
        
        echo 
    json_encode ($array); //Return the JSON Array

    }

    ?>
    If I have duplicate first names in the table the result I get in the auto-complete is a single first name on the top with multiple last names underneath it. I want to make it so that all of the duplicate first names show along with the corresponding last names in a row down the list. So instead of:

    John
    Adams
    Ballard
    Casey


    I would get:

    John Adams
    John Ballard
    John Casey

    Any help you could give would be greatly appreciated!

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,594
    Thanks
    23
    Thanked 646 Times in 645 Posts
    Would this help?

    PHP Code:
    while ($row mysql_fetch_assoc($sql)) {
       
    $array[] = $row['FirstName'].' '.$row['LastName'];

    You don't show how you decode it.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    froppo (12-03-2013)

  • #3
    New Coder
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Perfect! Exactly what I was looking for. Thanks!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •