按类别搜索PHP

时间:2018-02-13 03:51:43

标签: php html

我想使用php按类别搜索数据。我有一个带有类别和搜索框的选择框。我是php的新手。问题是,当我搜索某些内容时,数据不会显示。你能帮助我吗?请参阅下面的代码。我使用SHOW SQL QUERY生成了我的选择框。

选择框:

<select class="sort" name="category">
            <option value="">Category</option>
            <?php 
                require "connect.php";

                $sql = "SHOW columns FROM tblLocation";
                $result = mysqli_query($conn,$sql);

                while ($row = mysqli_fetch_array($result)) {
                    echo "<option value='".$row['Field']."'>".$row['Field']."</option>";
                }
            ?>
        </select>

搜索PHP:

<?php

include 'connect.php';

//Get Selected Value
$search = mysqli_real_escape_string($conn, $_POST['search-marker']);
$category = mysqli_real_escape_string($conn, $_POST['category']);

//SQL Conditions
if (empty($search) || empty($category)){
    echo "<script>
        alert('Please input a value');
    </script>";
}
else {
    $sql = "SELECT * FROM tblLocation WHERE ".$category." LIKE '%".$search."%'";
    $result = mysqli_query($conn,$sql);
    $count = mysqli_num_rows($result);

if($count > 0) {
    echo "<script>
        function initMap() {
            var infowindow = new google.maps.InfoWindow();

            var uluru = {lat: 13, lng: 122};
            var map = new google.maps.Map(document.getElementById('map'), {
              zoom: 6,
              center: uluru
            });";

    if ($result) {
        while ($row = mysqli_fetch_array($result)) {

            if($row['image'] == ""){
                $image = '<img src="stylesheet/images/default.png"  class="location"/>';
            }
            else {
                $image = '<img src="data:image/jpg;base64,'.base64_encode($row['image']).'"  class="location"/>';
            }

            echo "var content = '<div class=".'content'.">'+
                '$image'+
                '<p>".$row['street']."</p>'+
                '<span class=".'label'.">Branch:</span>'+
                '<span class=".'details'.">".$row['branch']."</span>'+
                '<span class=".'label'.">Country:</span>'+
                '<span class=".'details'.">".$row['country']."</span>'+
                '<span class=".'label'.">Barangay:</span>'+
                '<span class=".'details'.">".$row['barangay']."</span>'+
                '<span class=".'label'.">Town:</span>'+
                '<span class=".'details'.">".$row['town']."</span>'+
                '<span class=".'label'.">Province:</span>'+
                '<span class=".'details'.">".$row['province']."</span>'+
                '<span class=".'label'.">Phone:</span>'+
                '<span class=".'details'.">".$row['phone']."</span>'+
                '<span class=".'label'.">Email:</span>'+
                '<span class=".'details'.">".$row['email']."</span>'+
                '<span class=".'label'.">Website:</span>'+
                '<span class=".'details'.">".$row['website']."</span>'+
                '</div>';
                var marker = new google.maps.Marker({
                    content:content,
                    position:{ lat:{$row['lat']}, lng:{$row['lng']} },
                    map: map
                });
                marker.addListener('click', function(event) {
                    infowindow.open( map, this );
                    infowindow.setContent( this.content );
                    infowindow.setPosition( event.latLng );
                }.bind( marker ));";
        }
    }
    echo "}
        </script>";

    echo "<script async defer
            src='https://maps.googleapis.com/maps/api/js?key=AIzaSyDd03Su-LQGB3ZVB3MvP1a3q01lrgAvqlE&callback=initMap'>
        </script>";
}
else {
    echo "<script>
        alert('No Result(s) Found');
    </script>";
}

}

&GT;

0 个答案:

没有答案