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>


$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);


    <meta charset="utf-8">
    <link rel="stylesheet" href="experiment3.css">
    <link rel="stylesheet" href="font-awesome\css\font-awesome.min.css">
    <div class="header">
        <div class="spazzo">
 <form method="get" action="./">
    <div class="orçamento">Orçamento
    <div class="budget">
        <div class="search1">
                <input type="text" placeholder="Ex: R$3.000">
    <div class="pessoas">Pessoas
    <div class="peeps">
              <div class="search1">
                <input type="text" placeholder="Ex: 10">
    <div class="regiao">Região
    <div class="region">
        <div class="search1">
                 <input type="text" placeholder="Ex: Zona Oeste">
        <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 class="submit">
        <input type="submit" value="Ache!">

    <div class="section places latest">

            <div class="wrapper1">

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

                        $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;




    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");


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


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;



 $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);




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.