<div class=“showPage”><?php echo $totalRows_rsTest ?></div>
<?php do { ?>
<?php if ($totalRows_rsTest > 0) { // Show if recordset not empty ?>
<div class=“output” id=“Results”>
<p><?php echo $row_rsTest[‘year’]; ?></p>
<p><?php echo $row_rsTest[‘model’]; ?></p>
</div>
<?php } // Show if recordset empty ?>
<?php } while ($row_rsTest = mysql_fetch_assoc($rsTest)); ?>
I think i found out why the hide region is not working, whenever i search for something that is not in my database or does not satisfy my search criteria the results page is displaying all records in the database. However i don’t know how to fix that. Can someone please help me?
Also i’m trying to find out how i would display ALL vehicles in my database either for rent or sale, i dont have a field in my database for “ALL” so i’m not sure where to start. Thanks in advance
<?php
$maxRows_rsTest = 10;
$pageNum_rsTest = 0;
if (isset($_GET['pageNum_rsTest'])) {
$pageNum_rsTest = $_GET['pageNum_rsTest'];
}
$startRow_rsTest = $pageNum_rsTest * $maxRows_rsTest;
mysql_select_db($database_ddd, $ddd);
$query_rsTest = "SELECT `year`, `model` FROM vehicles";
if (isset ($_POST ['vehicles']) && ($_POST ['radioVehicles'])) {
$parSearch = mysql_real_escape_string ($_POST['vehicles']);
$parSearch1 = mysql_real_escape_string ($_POST['radioVehicles']);
if($_POST['vehicles'] == "Sedan"){
$query_rsTest.= " WHERE vehicle_type LIKE '%$parSearch%'";
}else if f($_POST['vehicles'] == "Jeep"){
$query_rsTest.= " WHERE vehicle_type LIKE '%$parSearch%'";
}
}
if (isset($_GET['totalRows_rsTest'])) {
$totalRows_rsTest = $_GET['totalRows_rsTest'];
} else {
$all_rsTest = mysql_query($query_rsTest);
$totalRows_rsTest = mysql_num_rows($all_rsTest);
}
$query_limit_rsTest = sprintf("%s LIMIT %d, %d", $query_rsTest, $startRow_rsTest, $maxRows_rsTest);
$rsTest = mysql_query($query_limit_rsTest, $ddd) or die(mysql_error());
$row_rsTest = mysql_fetch_assoc ($rsTest);
$totalRows_rsTest = mysql_num_rows ($rsTest);
$totalPages_rsProperties = ceil($totalRows_rsProperties/$maxRows_rsProperties)-1;
?>
<div class="showPage"><?php echo $totalRows_rsTest ?></div>
<?php do { ?>
<?php if ($totalRows_rsTest > 0) { // Show if recordset not empty ?>
<div class="output" id="Results">
<p><?php echo $row_rsTest['year']; ?></p>
<p><?php echo $row_rsTest['model']; ?></p>
</div>
<?php } // Show if recordset empty ?>
<?php } while ($row_rsTest = mysql_fetch_assoc($rsTest)); ?>
My problem is when i try to search for sedan and there are no sedans in my database i display all records, how do i get the query to display nothing if there is nothing in my database.
Did you test this as you where building it? “else if f($_POST[‘vehicles’] == “Jeep”){” But besides that you need to add a condition to your script to check if you results set is empty or not. Then you can display accordingly.
That “f” is a typo from putting the code on this site. Everything works once i have info in the database that matches my search criteria, but when there is no matching information everything is displayed. You mentioned a condition to check if the results is empty, can you help me the relevant code?
# Column Type Collation Attributes Null Default Extra Action
1 vehicle_id int(11) No None AUTO_INCREMENT Change Drop More
2 vehicle_type varchar(255) latin1_swedish_ci No None Change Drop More
3 model varchar(255) latin1_swedish_ci No None Change Drop More
4 year varchar(255) latin1_swedish_ci No None Change Drop More
5 sale varchar(255) latin1_swedish_ci No None Change Drop More
6 rent varchar(255) latin1_swedish_ci No None Change Drop More
mysql_select_db($database_ddd, $ddd);
$query_rsTest = "SELECT `year`, `model` FROM vehicles";
if (isset ($_POST ['vehicles'])) {
$parSearch = mysql_real_escape_string ($_POST['vehicles']);
if($_POST['vehicles'] == "Sedan"){
$query_rsTest.= " WHERE vehicle_type LIKE '%$parSearch%'";
}
}
So your Q is about queries like the above, I want to know if you have any matching data in the database. The use of LIKE on a column such as vehicle_type looks way off.
Surely vehicle type is limited to a small set of possibilities:
van
sedan
pickup
and so on?
If so, you should have the equivalent of “select year, model, from vehicles where vehicle_type = ‘sedan’”
What you need to prove is that
a) you have some matching data in your database.
b) you successfully connected to mysql
c) PHP ACTUALLY does construct that desired sql string correctly
BTW, varchar(255) is not the best way to hold a YEAR value, use a datestamp instead.
Thanks for the response, and thanks for the correction with datestamp.
My problem is, when i have no jeeps or sedans for sale and someone selects one of them i am getting all the vehicles for sale rather than them message no vehicles for sale. I have added the vehicle count from Dreamweaver server behaviour and tht is counting correctly everytime. So I believe my problem is the records i am returning.
eg. If i select Sedan since i have 2 Sedans in the database, the count for Sedans = 2 and the two Sedans are displayed.
However i have no Jeeps in the database and when i select Jeeps all the vehicles in the database are displayed. How do i go about fixing that problem?
To look at c) in my previous post you would simply echo the assembled sql query onto the page:
echo $query_rsTest;
… and take a close look at it, paste it into your database admin screen and see what it returns.
My guess is that you are not adding the second part, the WHERE clause because you are searching on “jeep” instead of “Jeep” or perhaps you have a rogue space eg " Jeep"