Create a gist now

Instantly share code, notes, and snippets.

@antirez /search.php
Last active Mar 19, 2017

What would you like to do?
<?
require("redis.php");
require("json.php");
$term = $_GET['term'];
$r = new Redis("127.0.0.1","6379");
$r->connect();
$items = $r->zrangebylex("kernel","[$term","[$term\xff",Array("LIMIT","0","10"));
$t = Array();
$id = 0;
foreach($items as $item) {
$t[] = Array("id" => $id++, "label" => $item);
}
$json = new Services_JSON();
$output = $json->encode($t);
echo($output);
?>

mardix commented Apr 22, 2014

It is preferred to use the native functions json_encode and json_decode of PHP instead of the class Services_JSON.

Also the code below is for PHP >= 5.4

<?php
    require("redis.php");

    $term = $_GET['term'];

    $r = (new Redis("127.0.0.1","6379"))->connect();
    $items = $r->zrangebylex("kernel","[$term","[$term\xff",["LIMIT","0","10"]);

    $t = [];
    $id = 0;
    foreach($items as $item) {
        $t[] = ["id" => $id++, "label" => $item];
    }
    echo json_encode($t);

ncri commented May 22, 2014

http://autocomplete.redis.io/?mkt_tok=3RkMMJWWfF9wsRonv6nBZKXonjHpfsX56u4lXKe3lMI%2F0ER3fOvrPUfGjI4ARMdmI%2BSLDwEYGJlv6SgFQ7HAMa5m3rgMWRk%3D

The demo errors:

Warning: fsockopen() [function.fsockopen]: unable to connect to 127.0.0.1:10001 (Connection refused) in /var/virtual/autocomplete.redis.io/httpdocs/redis.php on line 29

Fatal error: Cannot open socket to 127.0.0.1:10001, error 111. in /var/virtual/autocomplete.redis.io/httpdocs/redis.php on line 37

JohnH- commented Jun 30, 2014

I wrote something for nodejs if anyone is interested. It uses the same jquery UI autocomplete widget as http://autocomplete.redis.io/ but it executes a function as the source value.

edit: oh and btw I'm a novice so idk how good this code is, but it works.

On the server it uses socket.io and the [redis](https://www.npmjs.org/package /redis) npm module.

server:

var redis    = require('redis');
var client   = redis.createClient();
var io       = require('socket.io')(8000);

io.on('connection', function(socket) {                                 

  socket.on('keypress', function(data, fn) {
    //DBKEY is the key to the values you want     
    client.zrangebylex(['DBKEY', '[' + data, '[' + data + 'z'],
      function (err, response) {                                       
        if (err) throw err;                                            
        fn(response);                                                  
      }                                                                
    );                                                                 
  });                                                                  

});                                                                    

client:

//set this function to be called as the `source` in the jquery autocomplete widget
function sourceFunc(request, response) {             
  socket.emit('keypress', request.term, function(data) {
    response(data);                              
  }); 
}                                                      

Hello,
could you share the source of redis.php?
every redis-php library i find, dont support "zrangebylex"

Warning: fsockopen() [For instance many configuration options can be modified without any kind of restart using the function.fsockopen]: unable to connect to 127.0.0.1:10001 (Connection refused) in /var/virtual/autocomplete.redis.io/httpdocs/redis.php on line 29

Fatal error: Cannot open socket to 127.0.0.1:10001, error 111. in /var/virtual/autocomplete.redis.io/httpdocs/redis.php on line 37

I am unsure how this works as I cannot find support for zrangebylex with PHP anywhere.

Seems like demo script doesn't work for several years :-(

al-ramadhan commented Jun 16, 2016 edited

You can use predis library and call ZSetRangeByLex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment