joshgarrod Posted October 6, 2008 Share Posted October 6, 2008 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>"; } ?> Link to comment https://forums.phpfreaks.com/topic/127221-solved-return-database-results-based-upon-weight-factor/ Share on other sites More sharing options...
Orio Posted October 6, 2008 Share Posted October 6, 2008 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. Link to comment https://forums.phpfreaks.com/topic/127221-solved-return-database-results-based-upon-weight-factor/#findComment-658028 Share on other sites More sharing options...
joshgarrod Posted October 6, 2008 Author Share Posted October 6, 2008 thank you so much, works a treat Link to comment https://forums.phpfreaks.com/topic/127221-solved-return-database-results-based-upon-weight-factor/#findComment-658032 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.