2

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

6
  • you have to check this if ( data.length == 0 ) { } before $.each() function Commented Mar 6, 2015 at 5:42
  • If the data is null then it will not enter into the each function Commented Mar 6, 2015 at 5:43
  • If there is any data then only you need to iterate the results ? Commented Mar 6, 2015 at 5:45
  • can i post it as an answer ? Commented Mar 6, 2015 at 5:45
  • 1
    try to run the return data via console. It's very important to track the returning so that you are able to analyze and where you put your conditional statements. "console.log(data);" Commented Mar 6, 2015 at 5:49

2 Answers 2

1

Assuming the data is a JSON parsed object you can:

Object.keys(data).length === 0

Or

JSON.stingify(data) === '{}'
Sign up to request clarification or add additional context in comments.

Comments

0

Check length of the records before iteration (using $.each in jquery API)

    <script>
    $(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) {
                    console.log(data);
                    $('ul#content').show();
                    $('ul#content').empty()
                    if (data.length == 0) {
                        $('ul#content').empty().append('<li style="text-align: center;font-weight:bold;"><font color="white">empty</font></a></li>');
                    }
                    else {
                        $.each(data, function () {
                            $('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>');
                        });
                    }
                });
            }
        });
    });
</script>

Worked for me.

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.