Let users filter mysql table with checkboxes (html,php,mysql)

Hello,

I made a website to book any hotel. everything works fine and all but i cannot make a filter from navbar. example for users who likes to choose from hotel stars or which country they want. Or If the user clicks on “2 star hotels” the website will redirect to only 2 star hotels.

Here is my code:

`if ($action == “go”) {

$htop .= '      <div id="custom-bootstrap" class="navbar navbar-default " role="navigation">';
$htop .= '      <div class="container">';
$htop .= '       <div class="row">';
$htop .= '       <a href="http://house.cp3.glimworm.net">  <img src="/IMAGES_HOTELS/logo.gif"id"]?></a>';

echo $htop;
echo $h;
echo $hbot;

} else {

if ($action == "price") {

    $htop .= '<div id="custom-bootstrap" class="navbar navbar-default " role="navigation">';
    $htop .= '	<div class="container">';
    $htop .= '   	<div class="row">';
    $htop .= '       	<a href="http://house.cp3.glimworm.net">  <img src="/IMAGES_HOTELS/logo.gif"id"]?></a>';
    $htop .= '<form class="navbar-form navbar-right" role="search">';
    $htop .= '  <div class="form-group">';
    $htop .= '    <input type="text" class="form-control" placeholder="Search">';
    $htop .= '  </div>';
    $htop .= '  <button type="submit" class="btn btn-default">Submit</button>';
    $htop .= '</form>';
    $htop .= '   	</div>';
    $htop .= ' 	</div>';
    $htop .= ' </div>';
    $h .= '    <div class="container">';
    $h .= '</pre>';
    $h .= '	<label>Select Date: </label>';
    $h .= '		<div id="datepicker" class="input-group date" data-date-format="mm-dd-yyyy">';
    $h .= '    <input class="form-control" type="text" readonly />';
    $h .= '	<span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>';
    $h .= '</div>';
    $h .= '	</div>';
    $h .= '	</div>';

    echo $htop;
    echo $h;
    echo $hbot;

} else {

    if ($action == "slider") {

        $h = "";
        $htop .= '		<div id="custom-bootstrap" class="navbar navbar-default " role="navigation">';
        $htop .= '		<div class="container">';
        $htop .= '       <div class="row">';
        $htop .= '       <a href="http://house.cp3.glimworm.net">  <img src="/IMAGES_HOTELS/logo.gif"id"]?></a>';

        // Search //

        $htop .= '<form class="navbar-form navbar-right" role="search">';
        $htop .= '  <div class="form-group">';
        $htop .= '    <input type="text" class="form-control" placeholder="Search">';
        $htop .= '  </div>';
        $htop .= '  <button type="submit" class="btn btn-default">Submit</button>';
        $htop .= '</form>';
        $htop .= '      </div>';
        $htop .= '      </div>';
        $htop .= '      </div>';
        $htop .= '  </head>';
        $htop .= '  <body>';
        $htop .= '   <div class="navbar-wrapper">';
        $htop .= '     <div class="container">';
        $htop .= '';
        $htop .= '          <div class="container">';
        $htop .= '          <div class="navbar-header">';
        $htop .= '             <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">';
        $htop .= '               <span class="sr-only">Toggle navigation</span>';
        $htop .= '                <span class="icon-bar"></span>';
        $htop .= '                <span class="icon-bar"></span>';
        $htop .= '                <span class="icon-bar"></span>';
        $htop .= '              </button>';
        $htop .= '            </div>';
        $htop .= '            <div id="navbar" class="navbar-collapse collapse">';
        $htop .= '              <ul class="nav navbar-nav">';
        $htop .= '                  </ul>';
        $htop .= '                </li>';
        $htop .= '              </ul>';
        $htop .= '            </div>';
        $htop .= '          </div>';
        $htop .= '        </nav>';
        $htop .= '';
        $htop .= '      </div>';
        $htop .= '    </div>';
        $htop .= '';
        $htop .= '';

        echo $htop;
        echo $h;
        echo $hbot;

    } else {

        // Navbar Start

        $h = "";
        $htop .= '		<div id="custom-bootstrap" class="navbar navbar-default " role="navigation">';
        $htop .= '		<div class="container">';
        $htop .= '      <div class="row">';
        $htop .= '       	<a href="http://house.cp3.glimworm.net">  <img src="/IMAGES_HOTELS/logo.gif"id"]?></a>';

        // Search //

        $htop .= '<form class="navbar-form navbar-right" role="search">';
        $htop .= '  <div class="form-group">';
        $htop .= '    <input type="text" class="form-control" placeholder="Search">';
        $htop .= '  </div>';
        $htop .= '  <button type="submit" class="btn btn-default">Submit</button>';
        $htop .= '</form>';
        $htop .= '      </div>';
        $htop .= '      </div>';
        $htop .= '      </div>';

        // Navbar End

        $h .= '<body>';
        $h .= '<nav class="navbar navbar-default sidebar" role="navigation">';
        $h .= '    <div class="container-fluid">';
        $h .= '    <div class="navbar-header">';
        $h .= '      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-sidebar-navbar-collapse-1">';
        $h .= '        <span class="sr-only">Toggle navigation</span>';
        $h .= '        <span class="icon-bar"></span>';
        $h .= '        <span class="icon-bar"></span>';
        $h .= '        <span class="icon-bar"></span>';
        $h .= '      </button>      ';
        $h .= '    </div>';
        $h .= '    <div class="collapse navbar-collapse" id="bs-sidebar-navbar-collapse-1">';
        $h .= '      <ul class="nav navbar-nav">';
        $h .= '        <li class="active"><a href="#">Home<span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-home"></span></a></li>';
        $h .= '        <li class="dropdown">';

        $h .= '          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Star Rating <span class="caret"></span><span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-star"></span></a>';
        $h .= '          <ul class="dropdown-menu forAnimate" role="menu">';

        $h .= '<form  method="post" action="go"  id="searchform">';
        $h .= '	<input  type="text" name="name">';
        
        $h .= '  <a href="?action=go" class="btn btn-primary" role="button">Meer info</a></p>';
        $h .= '</form>';
        $h .= '</ul>';
        $h .= '	</li>';

        $h .= '		<li class="dropdown">';
        $h .= '      <a href="#" class="dropdown-toggle" data-toggle="dropdown">Country<span class="caret"></span><span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-road"></span></a>';

        $h .= '		<ul class="dropdown-menu forAnimate" role="menu">';
        $h .= '<form  method="post" action="search.php?go"  id="searchform">';

        $h .= '	<input  type="text" name="name">';
        $h .= '	<input  type="submit" name="submit" value="Search">';
        $h .= '</form>';
        $h .= '        </li>';
        $h .= '      </ul>';
        $h .= '          <ul class="dropdown-menu forAnimate" role="menu">';
        $h .= '        </li>';
        $h .= '      </ul>';
        $h .= '    </div>';
        $h .= '  </div>';
        $h .= '</nav>';
        $h .= '      </div>';
        $h .= '   </div>';
        $h .= '   ';
        $h .= '</div>';
        $h .= '<!-- Page Content -->';
        $h .= '   <div class="container">';
        $h .= '';
        $h .= '        <!-- Page Header -->';
        $h .= '        <div class="row">';
        $h .= '                <h1 class="page-header">Hotels';
        $h .= '                    <small>Results </small>';
        $h .= '                </h1>';
        $h .= '        <!-- /.row -->';

        $db = mysql_connect($host, $user, $pass);
        mysql_select_db($database);
        mysql_query("SET NAMES utf8", $db);
        mysql_query("SET CHARACTER SET utf8", $db);
        $h .= "        <div class='container'>";
        $sql = "SELECT * FROM hotels";
        $res = mysql_query($sql);
        for ($i = 0; $i < mysql_num_rows($res); $i++) {
            error_reporting(E_ERROR);

            $sql1 = "SELECT * FROM pictures  WHERE hotelID ='" . mysql_result($res, $i, "id") . "'";
            $res1 = mysql_query($sql1);

            $h .= "		<input type='hidden' name='action' value='slider'><input type='hidden' name='action2' value='1'><input type='hidden' name='id' value='" . $_GET['id'] . "'>";
            $h .= '			<div class="col-sm-6 col-md-3">';
            $h .= '        		<div class="row">';
            $h .= '	    			<div class="thumbnail">';
            $h .= '    					<img src=' . mysql_result($res1, 0, "URL") . '>';
            $h .= '    				<div class="caption">';
            $h .= '    			<h3>' . mysql_result($res, $i, "name") . '</h3>';
            $h .= '    			<h4>' . mysql_result($res, $i, "stars") . '&nbsp star</h4>';
            $h .= '        		<p>' . mysql_result($res, $i, "bio") . '<br>';
            $h .= '    			<h5>' . mysql_result($res, $i, "country") . '</h5>';
            $h .= '        		</p>';
            $h .= '        		<p><a href="?action=slider&id=' . mysql_result($res, $i, "id") . '" class="btn btn-primary" role="button">Meer info</a></p>';
            $h .= '      					</div>';
            $h .= '      				</div>';
            $h .= '   				</div>';
            $h .= '   			</div>';
            $h .= '			</tr>';
            $h .= '		</tbody>';
            $h .= '	</table>';
            $h .= '</body>';
        }
        $hbot .= '            </div>';
        $hbot .= '        </div>';
        $hbot .= '        <!-- /.row -->';
        $hbot .= '';
        $hbot .= '        <hr>';
        $hbot .= '';
        $hbot .= '        <!-- Footer -->';
        $hbot .= '        <footer>';
        $hbot .= '            <div class="row">';
        $hbot .= '                <div class="col-lg-6">';
        $hbot .= '                    <p>Copyright &copy; Bookme.com 2016</p>';
        $hbot .= '                </div>';
        $hbot .= '            </div>';
        $hbot .= '            <!-- /.row -->';
        $hbot .= '        </footer>';
        $hbot .= '';
        $hbot .= '    </div>';
        $hbot .= '    </div>';
        $hbot .= '    <!-- /.container -->';
        $hbot .= '';
        echo $htop;
        echo $h;
        echo $hbot;
    }
}

}
?>`

If you have a look at the JOIN keyword for SQL, you could combine the first query and the queries that you execute within the loop to retrieve all the information in one go. You could then easily add in the code you require to select hotels by their star-rating.

You also need to look at upgrading to mysqli or PDO calls instead of the old-style ones you are using, as the current version of PHP does not support them.

Thanks mate but could you help me a bit by giving me a litte example by code i’m a bit new to php. it would be awesome.

Sorry, I can’t do the code for you but I (and everyone else) would be happy to comment on your code as you develop it. To change the query into a single query, you’d need to post the column (field) names in your hotels and pictures tables - apart from anything else, there’s no point retrieving more data in the query than you’re going to actually use on the page.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.