user1861710
user1861710

Reputation: 25

php - can someone help me make a sql sort to get listing between 2 price values?

i need to sort this table to show prices between 2 values that are set with text boxes but ive tryed loads of veriations but it just wont work here is the form for the text box and what i think the query should be(from index2.php):

<form action ="index2.php" method="post">
                        Games priced<input action="index2.php" method="post" type="text" name="min">
                        Between<input action="index2.php" method="post" type="text" name="max">
                        <input type="submit" value="Sort">

                        <?php

                        $query = "SELECT * FROM CSGames WHERE price <=min AND >=max ";
                        $result = pg_query("SELECT * FROM CSGames WHERE price <=min AND >=max ");

                        ?>

and this is the sql database im useing

<?php
$con = pg_connect("bla bla");
    if (!$con)
        {
    die('Could not connect: ' . pg_error());
        }
    $result = pg_query("SELECT * FROM CSGames");
    echo "<table>
    <tr>
    <th>Title</th>
    <th>Platform</th>
    <th>Description</th>
    <th>Price</th>
    <th>Select</th>
    </tr>";

    while($row = pg_fetch_array($result)){
    echo"<tr>";
    echo "<td>" . $row['1'] . "</td>";
    echo "<td>" . $row['2'] . "</td>";
    echo "<td>" . $row['3'] . "</td>";
    echo "<td>" . $row['4'] . "</td>";
    echo '<td><input type="checkbox" name="games[]" value="' . $row['1'] . '|||' . $row['2'] . '|||'. $row['3'] . '|||' . $row['4'] . '"/></td>';

    echo"</tr>";      
        }
    echo"</table>";

pg_close($con);

?>

i think its just the query thats wrong but im not sure if it helps this is the page im working on http://users.aber.ac.uk/edd14/cs25010/index.php

Upvotes: 0

Views: 160

Answers (2)

phpisuber01
phpisuber01

Reputation: 7715

Here you go, a between clause just for these situations:

SELECT * FROM `CSGames`
WHERE `price` BETWEEN min AND max;

Documentation: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html

Replace min and max with your sanitized $_POST variables from your form.

Upvotes: 2

lawl0r
lawl0r

Reputation: 870

     $query = "SELECT * FROM CSGames WHERE price >=min AND price <=max ";
     $result = pg_query("SELECT * FROM CSGames WHERE price >=min AND price <=max ");

Upvotes: 1

Related Questions