Bootstrap Autocomplete using typeahead.js in F3 way

120 views
Skip to first unread message

karthick b

unread,
Dec 29, 2016, 1:04:57 PM12/29/16
to Fat-Free Framework
hi,

i am trying to implement typeahead.js in f3 way.I was able to do everything as mentioned in this Bootstrap Autocomplete example except the array part.

the below is the original example which i am trying to duplicate
if (isset($_REQUEST['query'])) {
$query
= $_REQUEST['query'];
$sql
= mysql_query ("SELECT * FROM country WHERE country LIKE '%{$query}%'");
$array
= array();
while ($row = mysql_fetch_assoc($sql)) {
$array
[] = $row['country'];
}
echo json_encode
($array); //Return the JSON Array
}


This is the model
class Sales extends DB\SQL\Mapper {

    public function __construct(DB\SQL $db) {
        parent::__construct($db,'sales');
    }
    public function searchProduct($query) {
        return $this->db->exec(
          'SELECT name FROM products WHERE name=?',
          $query
          );        
    }
}




I am struck here in the controller. 


<?php

class SalesController extends Controller {

function beforeroute(){
$f3 = Base::instance();
$f3->set('html_title','Sales');
}

public function searchProduct($f3) {
$objSales = new Sales($this->db);
$aProduct = $objSales->searchProduct($f3->get('PARAMS.query'));
print_r($aProduct);
/* actual output
Array
(
   [0] => Array
       (
           [name] => Product1
       )

   [1] => Array
       (
           [name] => Product2
       )

)
*/


/* required output */

$result = array();
$result['0'] = 'Product1';
$result['1'] = 'Product2';
print_r($result);

/* outputs
Array
(
   [0] => Product1
   [1] => Product2
)
    */


}
}


Please see under, "the required output" and "actual output" section of code above.

Before I try to manipulate the output array with native PHP, could you please advise if there is a possibility to solve this with F3. Thanks.

Anatol Buchholz

unread,
Dec 29, 2016, 3:06:33 PM12/29/16
to f3-fra...@googlegroups.com
Hey karthick,

 I´m using mostly the mapper not exec, other will surely know better.

Plain PHP should not add to much overhead either ...

$aProduct  = array(array("name" => 'product1'),array("name" => 'product2'));
array_walk_recursive
($aProduct,function($v, $k) use (&$result){ $result[] = $v; });
print_r
($result[0]); // product1
print_r
($result[1]); // product2

cheers,

– anatol

karthick b

unread,
Dec 30, 2016, 6:14:24 AM12/30/16
to Fat-Free Framework
Hi Anatol,

Thanks for the reply. I solved it with native PHP way.

$objSales = new Sales($this->db);
        $aProduct
= $objSales->searchProduct($f3->get('PARAMS.query'));

       
        $finalArray
= array();
       
for($i = 0;$i<count($aProduct);$i++){  // GET ALL OBJECT FROM ARRAY
           
foreach($aProduct[$i] as $key=>$value){ // GET ALL KEY FROM OBJECT
             $finalArray
[$i] = $aProduct[$i][$key];
           
}
       
}

        echo json_encode
($finalArray); //Return the JSON Array

credit goes to this stackoverflow post

pannet1

unread,
Jan 5, 2017, 3:38:04 AM1/5/17
to Fat-Free Framework on behalf of Anatol Buchholz

hi anatol,

i have mention the link to my f3 ecommere site under your post under the f3 sample application thread. did you get it?

i would like to take this opportunity to thank you personally for the help you have provided me throughout.

have a wonderful new year ahead.

br

karthick

On Friday 30 December 2016 01:36 AM, Anatol Buchholz via Fat-Free Framework wrote:
Hey karthick,

I only know of -> cast to convert an object result to an array. In this case
I would do something like:

$aProduct  = array(array("name" => 'product1'),array("name" => 'product2'));
array_walk_recursive
($aProduct,function($v, $k) use (&$result){ $result[] = $v; });
print_r
($result[0]); // product1
print_r
($result[1]); // product2

But I´m using mostly the mapper not exec, other will know better.
And uff, the original code looks vunerable ;)

cheers,

– anatol
--
-- You've received this message because you are subscribed to the Google Groups group. To post to this group, send an email to f3-fra...@googlegroups.com. To unsubscribe from this group, send an email to f3-framework...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/f3-framework?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Fat-Free Framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/f3-framework/lIDry_jKBdw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to f3-framework...@googlegroups.com.
To post to this group, send email to f3-fra...@googlegroups.com.
Visit this group at https://groups.google.com/group/f3-framework.
To view this discussion on the web visit https://groups.google.com/d/msgid/f3-framework/dc428fbe-adc1-413c-b791-30f2afb183aa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anatol

unread,
Jan 5, 2017, 6:41:54 AM1/5/17
to Fat-Free Framework
Karthick, 

haven´t seen it but just commented!

All the best for you as well! Happy new year!
Have you seen that there is an F3 slack chat now?
Happy to see you there.

Cheers,

– anatol

pannet1

unread,
Jan 5, 2017, 2:08:30 PM1/5/17
to Fat-Free Framework on behalf of Anatol

Dear Anatol,

Thanks for the wishes.

I have seen ikkez mention it somewhere. Then the sign up sounded little bit complicated for me. I will definitely try it next time.

Cheers.

--
-- You've received this message because you are subscribed to the Google Groups group. To post to this group, send an email to f3-fra...@googlegroups.com. To unsubscribe from this group, send an email to f3-framework...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/f3-framework?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Fat-Free Framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/f3-framework/lIDry_jKBdw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to f3-framework...@googlegroups.com.
To post to this group, send email to f3-fra...@googlegroups.com.
Visit this group at https://groups.google.com/group/f3-framework.
Reply all
Reply to author
Forward
0 new messages