samedi 2 février 2019

Comparing checked checkbox values with values in DB

Let's say I have a huge database with car information(columns) as followed:

ID .... CAR NAME .... WHEEL SIZE .... YEAR OF PRODUCTION .... MAX SPEED

User inputted all three required information and then he'll be redirected to another page.

On this page are checkboxes and based on which checkboxes he chose to tick, the website will echo out the number of occurances it is inside database.

For example if he ticked only car name, the website will count number of rows that has the same name as he enetered and echo number of rows. If he ticked year of production and wheel size the website will count number of rows where cars have same year of production AND same wheels size as user entered and echo it out. Every combination is with AND except for when only 1 checkbox is ticked.

I'd like to know if there's a way to create a function for this, not just manually using if statement for each case. It would be never-ending code like this if I decided that I need to add some extra columns into my database.

<form action="function.php" method="post">
  <input type="checkbox" name = "checkbox[]" value="<?php echo $car; ?>"> CAR NAME<br>
  <input type="checkbox" name = "checkbox[]" value="<?php echo $wheel_size; ?>"> WHEEL SIZE<br>
  <input type="checkbox" name = "checkbox[]" value="<?php echo $year; ?>"> YEAR OF PRODUCTION<br>
<input type="checkbox" name = "checkbox[]" value="<?php echo $speed; ?>"> MAX SPEED<br>
  <button>Submit CHOICES</button>
</form>



Aucun commentaire:

Enregistrer un commentaire