Jump to content

Querying and displaying data using php and mysql using checkboxes?


masterk123

Recommended Posts

I have a table in a mysql database with 5 columns, id, Name, Wifi, Bluetooth, GPS, with rows that are for example 1, Galaxy S2, yes, yes, yes. So basically i want to build a form that has check boxes (3 checkboxes for wifi bluetooth and GPS respectively) that once selected will query the database depending on which check boxes are selected. I have made the form but need to know what to put in the filter.php to make the results be displayed accordingly. Ideally if anyone knows how i would want it so the results were shown on the same page which i believe u need to use ajax to happen but any help on how to show the results will be greatful.

 

the code for the form i have made is as follows:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test</title>
</head>
<body>

<form action="filter.php" method="post">
<INPUT TYPE=CHECKBOX NAME="option[]" VALUE="Wifi" id="r1">Wifi
<INPUT TYPE=CHECKBOX NAME="option[]" VALUE="Bluetooth" id="b1">Bluetooth 
<INPUT TYPE=CHECKBOX NAME="option[]" VALUE="GPS" id="g1">GPS
<input type="submit" name="formSubmit" value="Submit" />

</form>

</body>
</html>

im not sure on how to make the filter.php page but i do have this code for displaying the all the data but i want to kno how to make it only display the data from the choices on the checkboxes

 

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>pls work</title>
    </head>
    <body>
<?php
    function h($s) {
        echo htmlspecialchars($s);
    }
    mysql_connect("localhost", "root", "") or die (mysql_error());
    mysql_select_db("project") or die (mysql_error());
    $result= mysql_query('SELECT * FROM test') or die('Error, query failed');
?>

<?php if (mysql_num_rows($result)==0) { ?>
    Database is empty <br/>
<?php } else { ?>
    <table>
        <tr>
            <th></th>
            <th>Name</th>
            <th>Wifi</th>
            <th>Bluetooth</th>
            <th>GPS</th>
        </tr>
        <?php while ($row= mysql_fetch_assoc($result)) { ?>
            <tr>
                <td>
                    <a href="uploaded-images/<?php h($row['Name']); ?>.jpg">
                        <img src="uploaded-images/<?php h($row['Name']); ?>.jpg" alt="test"/>
                    </a>
                </td>
                <td><a href="textonly.html"><?php h($row['Name']); ?></a></td>
                <td><?php h($row['Wifi']); ?></td>
                <td><?php h($row['Bluetooth']); ?></td>
                <td><?php h($row['GPS']); ?></td>
            </tr>
        <?php } ?>
    </table>
<?php  } ?>
    </body>
</html>

Link to comment
Share on other sites

You have to construct a query with the appropriate where clause conditions based on the checkboxes that are selected.  Something like:

 

$sql = 'SELECT * FROM test ';
$where=array();
if (isset($_POST['option'])){
   foreach ($_POST['option'] as $opt){
      switch ($opt){
         case 'Wifi':  $where[] = "Wifi='yes'"; break;
         case 'Bluetooth': $where[] = "Bluetooth='yes'"; break;
         case 'GPS': $where[] = "GPS='yes'"; break;
      }
    }
}

if (count($where)>0){
  $sql .= 'WHERE '.implode(' AND ', $where);
}

//query and display the results.

Link to comment
Share on other sites

You have to construct a query with the appropriate where clause conditions based on the checkboxes that are selected.  Something like:

 

$sql = 'SELECT * FROM test ';
$where=array();
if (isset($_POST['option'])){
   foreach ($_POST['option'] as $opt){
      switch ($opt){
         case 'Wifi':  $where[] = "Wifi='yes'"; break;
         case 'Bluetooth': $where[] = "Bluetooth='yes'"; break;
         case 'GPS': $where[] = "GPS='yes'"; break;
      }
    }
}

if (count($where)>0){
  $sql .= 'WHERE '.implode(' AND ', $where);
}

//query and display the results.

 

i see what u mean but im not sure where i would put this new query code, im very new to php :S do u think u could apply that example above to my the checkbox form i posted?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.