0

I want to make something like this http://www.gsmarena.com/search.php3 to search the items from my database and give the required result. what would be the best way to do this in php.

EDIT:

I have an entity like 'student' or 'mobile' in this given case. I want to give the user the option to get the count or details about students/phone with selected characteristics .

e.g. how many students who have mess bill > some value and have gpa < 2.0 ?

how many students with gpa > 3 and live on 2nd floor in hostel?

I want to give out all the attributes to choose from.?

i understand i can use several if statements but whats the best way to do this kinda thing? assuming i have good C++,PHP,HTML,SQL,CSS skills.

Asim Adnan
  • 11
  • 1
  • 6
  • 1
    Sorry, this place is to ask _specific questions_ you encounter whilst implementing something. This is not a place for a general introduction to programming or the like. – arkascha Apr 28 '14 at 08:05
  • @Vincent why does he needs to bind "ajax with php" in that case? Oo – Xatenev Apr 28 '14 at 08:06

1 Answers1

0

Assuming you know PHP and a little SQL, and don't want to use a query builder that's already out there, this is kinda what you'd end up with;

<?php
    if(isset($_POST)) {
        $sql = "SELECT * FROM `table` "
        if($_POST['price_min'] != 'doesntmatter') {
            $sql .= "WHERE `price` > '".$_POST['price_min']."'' ";
        }
        if($_POST['price_max'] != 'doesntmatter') {
            $sql .= "WHERE `price` < '".$_POST['price_max']."'' ";
        }
    }
?>

Basically, check if a variable is checked/selected, and if so add a WHERE clause

Ieuan
  • 1,140
  • 1
  • 12
  • 27
  • 2
    ...and be [pwned](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). I realize this is just sample code, but I suspect the people that want an introduction won't see the SQL injection problem. – Joachim Isaksson Apr 28 '14 at 08:08
  • Obviously it's just an example, the code isn't safe and doesn't have enough data to be usable anyway. It's just to show how you could build an SQL query. – Ieuan Apr 29 '14 at 08:48