Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

[HELP] Search functionality for website

So I have 3 search bars in my website and a couple of buttons describing different places.

The person navigating should write their budget in the first bar, number of people in the second, region in the third and then click on a button for the type of place they want.

When the button is clicked a search should be performed and should show results which have these 4 variables.

I am trying to get this done with PHP but I am really struggling.

Any suggestion would be welcome.

Thanks in advanced.

1 Answer

Matt Campbell
Matt Campbell
9,767 Points

What code have you got so far and what is it or not as the case may be, doing?

<!DOCTYPE html> <html>

<?php

$search_term = "";
if (isset($_GET["s"])) {
    $search_term = trim($_GET{"s"});
    if ($search_term != "") {
        require_once(ROOT_PATH . "../includes/places.php");
        $places = get_places_search($search_term);
    }
}

?>

 <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="experiment3.css">
    <link rel="stylesheet" href="font-awesome\css\font-awesome.min.css">
    <title>Spazzo</title>
</head>
<body>
    <div class="header">
        <div class="spazzo">
            <h1>Spazzo</h1>
        </div>
    </div>
 <form method="get" action="./">
    <div class="orƧamento">OrƧamento
    </div>
    <div class="budget">
        <div class="search1">
                <input type="text" placeholder="Ex: R$3.000">
        </div>
    </div>
    <div class="pessoas">Pessoas
    </div>
    <div class="peeps">
              <div class="search1">
                <input type="text" placeholder="Ex: 10">
      </div>
    </div>
    <div class="regiao">RegiĆ£o
    </div>
    <div class="region">
        <div class="search1">
                 <input type="text" placeholder="Ex: Zona Oeste">
        </div>
    </div>
        <div class="label4">EspaƧos</div>
        <div class="wrap">
            <div class="buttons">
                <a class="btn" href="#">
                <i class="icon-glass"></i></a>
                <div class="bares">Bares</div>
                <a class="btn1" href="#">
                <i class="icon-food"></i></a>
                <div class="restaurantes">Restaurantes</div>
                <a class="btn2" href="#">
                <i class="icon-briefcase"></i></a>
                <div class="reunioes">ReuniƵes</div>
                <a class="btn3" href="#">
                <i class="icon-group"></i></a>
                <div class="eventos">Eventos</div>
                <a class="btn4" href="#">
                <i class="icon-music"></i></a>
                <div class="baladas">Baladas</div>
                <a class="btn5" href="#">
                <i class="icon-heart"></i></a>
                <div class="romantico">RomĆ¢nticos</div>
                <a class="btn6" href="#">
                <i class="icon-camera"></i></a>
                <div class="ensaio">Ensaios</div>
                <a class="btn7" href="#">
                <i class="icon-leaf"></i></a>
                <div class="exotico">ExĆ³ticos</div>
            </div>
        </div>
    <div class="submit">
        <input type="submit" value="Ache!">
    </div>
</form>

    <div class="section places latest">

            <div class="wrapper1">

                <?php include("includes/places.php"); ?>
                <ul class="places">
                    <?php 

                        $total_places = count($places);
                        $position = 0;
                        $list_view_html = "";
                        foreach($places as $place_id => $place) { 
                            $position = $position + 1;
                            if ($total_places - $position < 4) {
                                $list_view_html = get_list_view_html($place_id,$place) .            $list_view_html;
                            }
                        }
                        echo $list_view_html;
                     ?>                             
                </ul>

            </div>

    </div>


<?php

    if($search_term != "") {
            if (!empty($places)) {
            echo '<ul class="places">';
            foreach ($places as $place) {
                echo get_list_view_html($place);
            }
            echo '</ul>';
        } else {
            echo ("places.php");
        }
    }
?>


</body>
</html>

Thats my html code so far, I used the php search functions from the videos here but I gotta say Im completely lost. So far its not working.. My places page is the following

<?php

function get_list_view_html($place_id, $place) {

    $output = "";

    $output = $output . "<li>";
    $output = $output . '<a href="place.php?id=' . $place_id . '">';
    $output = $output . '<img src="' . $place["img"] . '" alt="' . $place["preƧo"] . '">';
    $output = $output . "<p>Ver Detalhes</p>";
    $output = $output . "</a>";
    $output = $output . "</li>";

    return $output;
}


$places = array();
$places[101] = array(
    "name" => "Double Deck"
    "preƧo" => 1000;
    "capacidade" => 10;
    "regiĆ£o" => Zona Oeste;
    "espaƧo" => Bares;
);
$places[102] = array(
    "name" => "Royal"
    "preƧo" => 1500;
    "capacidade" => 15;
    "regiĆ£o" => Zona Leste;
    "espaƧo" => Baladas;
    );
$places[103] = array(
    "name" => "Buffet"
    "preƧo" => 2000;
    "capacidade" => 20;
    "regiĆ£o" => Zona Norte;
    "espaƧo" => Eventos;
    );
$places[104] = array(
    "name" => "Office"
    "preƧo" => 2500;
    "capacidade" => 25;
    "regiĆ£o" => Zona Sul;
    "espaƧo" => ReuniƵes;
    );


?>

 <?php 

 $section ="lugares";

 ?>

    <div class ="section places page">

        <div class="wrapper1">

            <ul class="places">
                <?php foreach($places as $place) { 
                        echo get_list_view_html($place_id,$product);
                } 
                ?>
            </ul>

        </div>

    </div>


<?php

function get_places_search($s) {
    $results = array();
    $all = get_places_all();

    foreach($all as $place) {
        if (stripos($place["preƧo"],$s) !== false) {
            $results[] = $place;
            }
    }
    return $results;
}

?>

I know its a lot to ask but if you have any suggestions that would be great.. Its getting pretty frustrating.