Jump to content

[SOLVED] Return database results based upon weight factor


joshgarrod

Recommended Posts

Hello all, I have a small problem. I have a database containing vehicles, my code returns all of the vehicles which is a start, but I need it to return them if their weight is less than or equal to $x (entered by user) How do I do this please?

 

Here is my code so far:

 

<?php
$x = 0
while($info = mysql_fetch_array( $data )) 
{ 
Print "<div id=\"caravan_row\">";
Print "<div id=\"make\"><strong>Make: </strong>".$info['make'] . "</div><br />"; 
Print "<div id=\"model\"><strong>Model: </strong>".$info['model'] . " </div><br />"; 
Print "<div id=\"berth\"><strong>Berth: </strong>".$info['berth'] . " </div><br />";
Print "<div id=\"weight\"><strong>Weight: </strong>".$info['weight'] . "kg</div><br />";
Print "<div id=\"price\"><strong>Price: £</strong>".$info['price'] . " </div>";
Print "</div>";
} 
?> 

Here's how it should look basically:

 

<form action="<?php echo basename($_SERVER['PHP_SELF']); ?>" method="POST">
Enter weight: <input type="text" name="x"><input type="submit"><br>
</form>

<?php

$cond = "";
if(isset($_POST['x']) && is_numeric($_POST['x']))
{
$x = intval($_POST['x']);
$cond = " WHERE weight <= {$x}";
}

$data = mysql_query("SELECT * FROM cars".$cond);

while($info = mysql_fetch_array( $data )) 
{ 
Print "<div id=\"caravan_row\">";
Print "<div id=\"make\"><strong>Make: </strong>".$info['make'] . "</div><br />"; 
Print "<div id=\"model\"><strong>Model: </strong>".$info['model'] . " </div><br />"; 
Print "<div id=\"berth\"><strong>Berth: </strong>".$info['berth'] . " </div><br />";
Print "<div id=\"weight\"><strong>Weight: </strong>".$info['weight'] . "kg</div><br />";
Print "<div id=\"price\"><strong>Price: £</strong>".$info['price'] . " </div>";
Print "</div>";
}

?>

 

Just modify it according to your needs.

 

Orio.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.