homepage Welcome to WebmasterWorld Guest from 54.237.98.229
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Search and Empty PHP
Adamphp



 
Msg#: 4415167 posted 7:44 pm on Feb 7, 2012 (gmt 0)

I need help with processing this form

It works but i want to the submit to search the whole db if nothing is selected to value of ""

Also is this the correct way to process a search form for searching cars or is there a more pro way

HTML
Make = Make
Model = Model
Min Price = Name "pf"
Max Price = Name "pt"

PHP
----

<body>


<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

// include you database connect file with username etc.
include ("db.php");

//Connect to the database using values from db.php ajones_cardealer $database variable value from db.php
$connect = mysql_connect($host,$username,$password) or die('<p class="error">Unable to connect to the database server at this tim</p>');

//select the database we are going to use in this case its
mysql_select_db($database,$connect) or die('<p class="error">Unable to connect to the database at this time.</p>');

// Get the posted value based on option select.

$Make = $_POST['make'];
$Min_Price = $_POST['pf'];
$Max_Price = $_POST['pt'];

//Uncomment below to check post is working.
//echo $Make;

// Create SQL query to find the row with make_id.
$query = "SELECT * FROM site_cars WHERE car_Make = $Make AND car_price BETWEEN $Min_Price AND $Max_Price";

//Run Query and store values in the $result array.
$result = @mysql_query("$query") or die('<p class="error">There was an unexpected error grabbing comments from the database.</p>');

//output results

?>

<table>
<tr>
<td>
<?php
if ($result != false)
{
while ($car = mysql_fetch_array ($result) )
{ // close the php here so you can write your html easier.?>

<!-- write your html here -->
<table>
<table>
<tr>
<td><img height="100" width="100"/>
</td>
<td valign="top">
<table>
<tr>
<td>
REF: <?php echo $car['car_id'];?>
</td>
</tr>
<tr>
<td>
Make
</td>
<td>
<?php echo $car["car_make"]; ?>
</td>
</tr>
<tr>
<td>
Price
</td>
<td>
&pound; <?php echo $car['car_price'] = number_format($car["car_price"] , 2);;?>
</td>
</tr>
<tr>
<td>
Milage
</td>
<td>
<?php echo $car['car_mileage'];?>
</td>
</tr>
<tr>
<td>
Year
</td>
<td>
<?php echo $car['car_year'];?>
</td>
</tr>
</table>
</td>
<td style="padding-left:10px;" valign="top">
<?php echo $car['car_description'];?>
</td>
</tr>
</table>
</table>

<?php // open your php again here to close the while and if statements.
}
}?>

</td>
</tr>
</table>

</body>
</html>

 

ZakAltF4



 
Msg#: 4415167 posted 1:23 am on Feb 8, 2012 (gmt 0)

Can't you just run an if statement? Check if the strings are empty, and if they are SELECT * FROM ORDER BY ? Or you could simply have a "view all" button that does the same thing ...

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4415167 posted 3:06 am on Feb 8, 2012 (gmt 0)

It works but i want to the submit to search the whole db if nothing is selected to value of ""


Build your select dynamically. You'll have to use equality operators instead of between to make it more simple but the results are synonymous, plus you can expand it to any number of input values.

$where = null;

// Keys on left, table fields and comparison operators on right
$vars = array(
'make' => array ('car_Make','='),
'pf' => array('car_price','>='),
'pt' => array ('car_price','<=')
);
//
foreach ($vars as $key => $op_array) {
if (isset($_POST[$key]) and ! empty($_POST[$key])) {
// Only need to add "and" if $where has been started.
// Note the spaces. Important.
if ($where) { $where .= ' and'; }
// Wrap text values, not needed for numeric values
$tmp = ($key=='make')?"'".$_POST[$key]."'":$_POST[$key];
// Ex: " car_price>=2000 " or " and car_price>=2000 "
$where .= " " . $op_array[0] . $op_array[1] . $tmp;
}
}
//
$query = "select * from site_cars";
if ($where) { $query .= " where $where"; }


This can give you

select * from site_cars (nothing selected, all records)
select * from site_cars where make='Chevy'; (make only)
select * from site_cars where make='Chevy' and car_price>=2000 (greater than only)
select * from site_cars where make='Chevy' and car_price<=5000 (less than only)
select * from site_cars where make='Chevy' and car_price>=2000 and car_price<=5000 (all inputs)

Once you get your head around it, you can use it to build "or" clauses (wrap them in parentheses), order, and limit clauses from inputs.

Adamphp



 
Msg#: 4415167 posted 9:30 am on Feb 8, 2012 (gmt 0)

look complicates sure there a simple way like using isset or empty with if statements

Maybe have a variable equal to ""

$Empty =$_POST"";

Adamphp



 
Msg#: 4415167 posted 10:25 am on Feb 8, 2012 (gmt 0)

look complicates sure there a simple way like using isset or empty with if statements

Maybe have a variable equal to ""

$Empty =$_POST"";

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4415167 posted 4:27 pm on Feb 8, 2012 (gmt 0)

A bunch of spaghetti code if's/else's would be far more complicated, and with your idea you still have to resolve it in an SQL statement one way or another.

select * from table where value=""

would give you no results. Give it a try. :-)

Adamphp



 
Msg#: 4415167 posted 6:18 pm on Feb 8, 2012 (gmt 0)

This is confusing

i have created a html search form and want to search my db from make model price from and price too

Example search Make = BMW, Model 3 Series, Price 2000 - 5000

But also want to be able to search the whole db if equal to nothing

but someone wanna maybe just search price so make and model would =""


dont like posting url but i want to have a search like this web site [motors.co.uk...]

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4415167 posted 5:30 pm on Feb 13, 2012 (gmt 0)

Create a test file and give it a try. If no items are selected, that's exactly what it will do - search the entire database table.


This can give you

select * from site_cars (nothing selected, all records)
select * from site_cars where make='Chevy'; (make only)
select * from site_cars where make='Chevy' and car_price>=2000 (greater than only)
select * from site_cars where make='Chevy' and car_price<=5000 (less than only)
select * from site_cars where make='Chevy' and car_price>=2000 and car_price<=5000 (all inputs)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved