I am trying to search data in php mysql using different input fields but my code seems correct but doesn't stay in session whenever i try to switch within pages...
<form method="POST" action="search_all_properties">
<div class="form-row align-items-center">
<div class="col-auto my-1">
<select class="custom-select mr-sm-2" id="inlineFormCustomSelect" name="category" value="category" >
<option selected>Category</option>
<option value="houses">Houses</option>
<option value="shops">Shops</option>
<option value="office_spaces">Office Spaces</option>
</select>
</div>
<div class="col-auto my-1">
<select class="custom-select mr-sm-2" id="inlineFormCustomSelect" name="bedroom">
<option selected>Bedrooms</option>
<option value="1_bedroom">1 Bedroom</option>
<option value="2_bedroom">2 Bedrooms</option>
<option value="3_bedroom">3 Bedrooms</option>
</select>
</div>
<div class="col-auto my-1">
<select class="custom-select mr-sm-2" id="inlineFormCustomSelect" name="price">
<option selected>Price</option>
<option value="200,000">200,000</option>
<option value="300,000">300,000</option>
<option value="500,000">500,000</option>
<option value="800,000">800,000</option>
<option value="1_million">1 Million</option>
</select>
</div>
<div class="col">
<input type="text" onchange="trim(this)" name="location" class="form-control" placeholder="Location">
</div>
<div class="col-auto my-1">
<button type="submit" name="submit" class="btn bg-info">Search</button>
</div>
</div>
</form>
i always get an error that some values are not defined...
$bedroom = $database->escape_value($bedroom);
$price = $database->escape_value($price);
$location = $database->escape_value($location);
The above variables are not defined each time i try to paginated to next page... session is for the variables are reset once i try to move to next page.
<?php
if(isset($_POST['submit']) || isset($_SESSION['search'])){
// 1. the current page number ($current_page)
$page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
// 2. records per page ($per_page)
$per_page = 2;
// 3. total record count ($total_count)
if(isset($_POST['submit'])) {
$_SESSION['category'] = $_POST['category'];
$category = $_POST['category'];
$_SESSION['bedroom'] = $_POST['bedroom'];
$bedroom = $_POST['bedroom'];
$_SESSION['price'] = $_POST['price'];
$price = $_POST['price'];
$_SESSION['location'] = $_POST['location'];
$location = $_POST['location'];
} else { if(isset($_SESSION['category'])){
$category = $_SESSION['category'];
}else if(isset($_SESSION['bedroom'])){
$bedroom = $_SESSION['bedroom'];
}else if(isset($_SESSION['price'])){
$price = $_SESSION['price'];
}else if(isset($_SESSION['location'])){
$location = $_SESSION['location'];
}
}
$category = $database->escape_value($category);
$bedroom = $database->escape_value($bedroom);
$price = $database->escape_value($price);
$location = $database->escape_value($location);
$sql = "SELECT COUNT(*) FROM pictures WHERE title LIKE '%$category%'";
$sql .= " OR location LIKE '%$location%'";
$sql .= " OR price LIKE '%$price%'";
$sql .= " OR description LIKE '%$category%'";
$sql .= " OR bedroom LIKE '%$bedroom%'";
$sql = $database->query($sql);
$sql = $database->fetch_array($sql);
$total_count = array_shift($sql);
// Find all photos
// use pagination instead
$pagination = new Pagination($page, $per_page, $total_count);
// Instead of finding all records, just find the records
$sql = "SELECT * FROM pictures WHERE title LIKE '%$category%'";
$sql .= " OR location LIKE '%$location%'";
$sql .= " OR price LIKE '%$price%'";
$sql .= " OR description LIKE '%$category%'";
$sql .= " OR bedroom LIKE '%$bedroom%'";
$sql .= " ORDER by id DESC";
$sql .= " LIMIT {$per_page}";
$sql .= " OFFSET {$pagination->offset()}";
$photos = Picture::find_by_sql($sql);
$tcount = count($photos);
}
?>