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 1 of 1
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,093
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Exclamation help to format database results into a specific array format?

    Hi All,

    im following an example which shows how to get an autosuggest feature working

    this is sample_data.php
    PHP Code:
    <?php
    $fruits 
    = array(
                                    
    'Apple' => array('image' => 'assets/images/fruits/apple.jpg''description' => 'One of America\'s favorite fruits.'),
                                    
    'Avocado' => array('image' => 'assets/images/fruits/avocado.jpg''description' => 'The avocado is a dense, evergreen tree, shedding many leaves in early spring.'),
                                    
    'Banana' => array('image' => 'assets/images/fruits/banana.jpg''description' => 'Bananas are fast-growing herbaceous perennials arising from underground rhizomes.'),
                                    
    'Gooseberry' => array('image' => 'assets/images/fruits/gooseberry.jpg''description' => 'Gooseberries are deciduous shrubs.'),
                                    
    'Grape' => array('image' => 'assets/images/fruits/grape.jpg''description' => 'Grapes come in large clusters.'),
                                    
    'Jackfruit' => array('image' => 'assets/images/fruits/jackfruit.jpg''description' => 'The jackfruit tree is handsome and stately.'),
                                    
    'Mango' => array('image' => 'assets/images/fruits/mango.jpg''description' => 'Mango trees make handsome landscape specimens and shade trees.'),
                                    
    'Papaya' => array('image' => 'assets/images/fruits/papaya.jpg''description' => 'The papaya is a short-lived, fast-growing, woody, large herb to 10 or 12 feet in height. It is also regarded by some as being delicious.'),
                                    
    'Peach' => array('image' => 'assets/images/fruits/peach.jpg''description' => 'These are great in the summertime.'),
                                    
    'Pear' => array('image' => 'assets/images/fruits/pear.jpg''description' => 'Pears are delicious fruits.'),
                                    
    'Pineapple' => array('image' => 'assets/images/fruits/pineapple.jpg''description' => 'The pineapple plant is a herbaceous perennial, 2-1/2 to 5 ft.'),
                                    
    'Rose Apple' => array('image' => 'assets/images/fruits/rose_apple.jpg''description' => 'The rose apple is a highly decorative evergreen large shrub.'),
                                    
    'Tamarind' => array('image' => 'assets/images/fruits/tamarind.jpg''description' => 'The bright green, pinnate foliage is dense and feathery in appearance.'),
                                    
    'White Sapote' => array('image' => 'assets/images/fruits/white_sapote.jpg''description' => 'The white sapote forms a medium to very large evergreen tree.'),
                                );
    $vegetables = array(
                                    
    'Alfalfa' => array('image' => 'assets/images/fruits/alfalfa.jpg''description' => 'One cup of raw, sprouted alfalfa seeds, contains 1.32 grams of protein.'),
                                    
    'Artichoke' => array('image' => 'assets/images/fruits/artichoke.jpg''description' => 'One medium artichoke cooked with no added salt has 3.47 grams protein.'),
                                    
    'Asparagus' => array('image' => 'assets/images/fruits/asparagus.jpg''description' => 'Half cup (about 6 spears) cooked with no added salt contains 2.16 grams of protein.'),
                                    
    'Broccoli' => array('image' => 'assets/images/fruits/broccoli.jpg''description' => 'Half cup of broccoli, cooked with no added salt contains 1.86 grams protein.'),
                                    
    'Carrots' => array('image' => 'assets/images/fruits/carrots.jpg''description' => 'Half cup cooked with no added salt contains 0.59 grams protein.'),
                                    
    'Celery' => array('image' => 'assets/images/fruits/celery.jpg''description' => 'One cup of celery, cooked, boiled, drained with no added salt has 1.25 grams protein.'),
                                    
    'Corn' => array('image' => 'assets/images/fruits/corn.jpg''description' => 'One large ear of yellow corn, cooked with no salt contains 4.02 grams protein.'),
                                    
    'Green Pepper' => array('image' => 'assets/images/fruits/green_pepper.jpg''description' => 'One small raw pepper contains 0.64 grams protein.'),
                                    
    'Mushroom' => array('image' => 'assets/images/fruits/mushroom.jpg''description' => 'Half a cup of raw mushrooms contains 1.08 grams of protein.'),
                                    
    'Onion' => array('image' => 'assets/images/fruits/onion.jpg''description' => 'One small onion cooked without salt contains 0.82 grams protein.'),
                                    
    'Potato' => array('image' => 'assets/images/fruits/potato.jpg''description' => 'One medium baked potato without salt contains 4.33 grams of protein.'),
                                    
    'Spinach' => array('image' => 'assets/images/fruits/spinach.jpg''description' => 'One cup of raw spinach contains 0.86 grams of protein.'),
                                    
    'Squash' => array('image' => 'assets/images/fruits/squash.jpg''description' => 'One cup of sliced summer squash, boiled with no added salt contains 1.87 grams of protein.'),
                                );
    ?>
    and this is the search_single.php which uses the above
    PHP Code:
    <?php
    /* Get the query string "q" variable -- this is what the user typed in. */
    $q $_GET['q'];

    /* Run some sort of searching operation.
                - Usually you will be searching a database and then pulling back results.
                - In this case, we're going to just use a simple array as our data source and find any matches. */
    include('sample_data.php');
    $results = array();
    foreach (
    $fruits as $name => $data)
    {
        if (
    stripos($name$q) !== false)
        {
            
    $results[$name] = $data;
        }
    }

    /* Get the data into a format that Smart Suggest will read (see documentation). */
    $final = array('header' => array(), 'data' => array());
    $final['header'] = array(
        
    'title' => 'Fruit Search',
        
    'num' => count($results),
        
    'limit' => 5
    );
    foreach (
    $results as $name => $data)
    {
        
    $final['data'][] = array(
            
    'primary' => $name,
            
    'secondary' => $data['description'],
            
    'image' => $data['image'],
            
    'onclick' => 'alert(\'You clicked on the '.$name.' fruit!\');'
        
    );
    }

    /* Output JSON */
    header('Content-type: application/json');
    echo 
    json_encode(array($final)); # Put it as the first result in an array, since we only have one category here
    die();
    ?>
    what i am trying to do is modify the above so that it no longer uses the hardcoded array found in sample_data.php but instead use an array created from a db query

    the problem is i cannot reproduce the same array layout found in the sample_data.php when trying the db way
    here is my ammended code
    PHP Code:
    <?php
    include("config.php");
    /* Get the query string "q" variable -- this is what the user typed in. */
    $q $_GET['q'];

    $results = array();

    $query mysqli_query($con"SELECT * FROM tbl_places")or die(mysqli_error());
    while(
    $row mysqli_fetch_array($query))
    {
        
    $id $row['placeID'];
        
    $name $row['placeName'];
        
    $desc $row['placeInfo'];
        
    $postcode $row['placeName'];
        if (
    stripos($name$q) !== false)
        {
            
    $results = array('id' => $id'name' => $name'desc' => $desc);
        }

    }

    /* Get the data into a format that Smart Suggest will read (see documentation). */

    $final = array('header' => array(), 'data' => array());
    $final['header'] = array(
        
    'title' => 'Town / Village Search',
        
    'num' => count($results),
        
    'limit' => 5
    );
    foreach (
    $results as $name => $data)
    {
        
    $final['data'][] = array(
            
    'primary' => $name,
            
    'secondary' => $data['desc'],
            
    'image' => $data['image'],
            
    'onclick' => 'alert(\'You clicked on the '.$name.' fruit!\');',
            
    'fill_text' => $name
        
    );
    }

    /* Output JSON */
    header('Content-type: application/json');
    echo 
    json_encode(array($final)); # Put it as the first result in an array, since we only have one category here
    die();
    ?>
    but $results doesnt return the expected results can anyone help me to gt my ammended example working correctly, i think the problem lies with the $result array format and the correct use of $data

    any help is much appreciated
    Luke
    Last edited by LJackson; 08-29-2013 at 12:21 AM.


 

Posting Permissions

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