<?php
// Database configuration
require_once("../../connection/conn_marseille.php");
mysql_set_charset('utf8');
// Get search term
$searchTerm = $_GET['term'];
if($searchTerm == " "){
    $searchTerm = "xxxxxxxxxxx";
}
// Get matched data from the database
$query = "SELECT * FROM tb_produto WHERE status = 1 AND CONCAT_WS ( ' ', nome, sku, busca_interna ) LIKE '%$searchTerm%' ORDER BY nome ASC LIMIT 7";

$result = mysql_query($query);
//echo mysqli_num_rows($result);
// Generate users data array
$userData = array();
if(mysql_num_rows($result) > 0){
while($row = mysql_fetch_array($result)){
    if($row['imagem_principal'] == ""){
        $row['imagem_principal'] = "prod-sem-foto.png";
    }
    $relurlprod        =    $row['url_seo'];
    $name             = $row['nome'];
    $data['value'] = $name;
    $data['label'] = '
    <a href="'.HOST."p/".$relurlprod.'">
    <img src="'.HOST.'images/produtos/principal/miniatura/'.$row['imagem_principal'].'" />
    <span>'.$name.'</span>
    </a>';
    array_push($userData, $data);
    }
}
// Return results as json encoded array
if(json_encode($userData)){
 echo json_encode($userData);
} else {
    switch (json_last_error()) {
        case JSON_ERROR_NONE:
            echo ' - No errors';
        break;
        case JSON_ERROR_DEPTH:
            echo ' - Maximum stack depth exceeded';
        break;
        case JSON_ERROR_STATE_MISMATCH:
            echo ' - Underflow or the modes mismatch';
        break;
        case JSON_ERROR_CTRL_CHAR:
            echo ' - Unexpected control character found';
        break;
        case JSON_ERROR_SYNTAX:
            echo ' - Syntax error, malformed JSON';
        break;
        case JSON_ERROR_UTF8:
            echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
        break;
        default:
            echo ' - Unknown error';
        break;
    }
}

?>