i have a live searcher thats when its data its shows
i need to alert if the data is empty
this is the jquery:
$(document).ready(function() {
$('#q').on('input', function() {
var searchKeyword = $(this).val();
if (searchKeyword.length >= 3) {
$.post('/files/assets/php/ajax/search.php', { q: searchKeyword }, function(data) {
$('ul#content').show();
$('ul#content').empty()
$.each(data, function() {
if ( data.length == 0 ) {
$('ul#content').append('<li style="text-align: center;font-weight:bold;"><font color="white">empty</font></a></li>');
}else{
$('ul#content').append('<li style="text-align: center;font-weight:bold;"><a href="/store/' + this.id + '/' + this.seo + '"><font color="white">' + this.title + '</font></a></li>');
}
});
}, "json");
}
});
});
and the php:
$conexion = mysqli_connect($serv,$user,$pass,$base);
$arr = array();
if (!empty($_POST['q'])) {
$keywords = $Main->limpiar($_POST['q']);
$mysqli = $conexion;
$result = $mysqli->query("SELECT cat, titulo FROM pinturas WHERE titulo LIKE '%".$keywords."%' OR cat LIKE '%".$keywords."%'");
if ($result->num_rows > 0) {
while ($obj = $result->fetch_array()) {
$seo = str_replace(" ","_",$obj['titulo']);
$arr[] = array('id' => $obj['cat'], 'title' => $obj['titulo'], 'seo' => $seo);
}
}else{
$arr[] = array('id' => '', 'title' => '', 'seo' => '');
}
}
echo json_encode($arr);
i want to if the data is empty shows the append empty
but it dont work
2
Answers
Check length of the records before iteration (using $.each in jquery API)
Worked for me.
Hope this helps.
Assuming the data is a JSON parsed object you can:
Or