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

PHP Integrating PHP with Databases Limiting Records in SQL Setting Up Pagination Variables

jinhwa yoo
jinhwa yoo
10,042 Points

plz help me

I feel just copying machine even if I don't understand what teacher said.. I just tried to type all the code i learned.. so the problem is I can not describe how to connect functions.php connection.php, data.php, catalog.php suggest.php...

how come do i overcome this situation???

is there anybody helping me understand from 1 to 4?

  1. header.php <html> <head> <title><?php echo $pageTitle; ?></title> <link rel="stylesheet" href="css/style.css" type="text/css"> </head> <body>

    <div class="header">

    <div class="wrapper">
    
        <h1 class="branding-title"><a href="/">Personal Media Library</a></h1>
    
        <ul class="nav">
            <li class="books<?php if ($section == "books") { echo " on"; } ?>"><a href="catalog.php?cat=books">Books</a></li>
            <li class="movies<?php if ($section == "movies") { echo " on"; } ?>"><a href="catalog.php?cat=movies">Movies</a></li>
            <li class="music<?php if ($section == "music") { echo " on"; } ?>"><a href="catalog.php?cat=music">Music</a></li>
            <li class="suggest<?php if ($section == "suggest") { echo " on"; } ?>"><a href="suggest.php">Suggest</a></li>
        </ul>
    
    </div>
    

    </div>

    <div id="content">

  2. functions.php <?php

function get_catalog_count($category = null){ $category = strtolower($category); include("connection.php"); try{ $sql = "SELECT COUNT(media_id) FROM Media"; if(!empty($category){ $result = $db->prepare($sql. "WHERE LOWER(category) ?" ); $result->bindParam(1,$category,PDO::PARAM_STR); }else { $result = $db -> prepare ($sql); } $result -> execute(); }catch (Exception $e){ echo "bad query"; }

   $count = $result->fetchColumn(0);
   return $count;

} function full_catalog_arrray(){ include("connection.php); try { $results = $db->prepare("SELECT title, category, img FROM Media"); } catch (Exception $e) { echo "Unable to retrieved results"; exit; }

$catalog = $results->fetchAll();
if(empty($item)) return $item;
return $item;

} function get_item_html($id,$item) { $output = "<li><a href='details.php?id=" . $id . "'><img src='" . $item["img"] . "' alt='" . $item["title"] . "' />" . "<p>View Details</p>" . "</a></li>"; return $output; }

function array_category($catalog,$category) { $output = array();

foreach ($catalog as $id => $item) {
    if ($category == null OR strtolower($category) == strtolower($item["category"])) {
        $sort = $item["title"];
        $sort = ltrim($sort,"The ");
        $sort = ltrim($sort,"A ");
        $sort = ltrim($sort,"An ");
        $output[$id] = $sort;            
    }
}

asort($output);
return array_keys($output);

}

  1. connection.php <?php

try{ $db = new PDO("sqlite:".DIR."/database.db"); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (Exception $e){ echo "Unable to connect"; //echo $e->getMessage(); exit; }

echo"Connected to the database";

try { $results = $db->query("SELECT title, category From Media");

} catch (Exception $e ){ echo "Unable to retrieved results"; exit; }

var_dump($results->fetchAll());

4.suggest.php <?php

if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = trim(filter_input(INPUT_POST,"name",FILTER_SANITIZE_STRING)); $email = trim(filter_input(INPUT_POST,"email",FILTER_SANITIZE_EMAIL)); $category = trim(filter_input(INPUT_POST,"category",FILTER_SANITIZE_STRING)); $title = trim(filter_input(INPUT_POST,"title",FILTER_SANITIZE_STRING)); $format = trim(filter_input(INPUT_POST,"format",FILTER_SANITIZE_STRING)); $genre = trim(filter_input(INPUT_POST,"genre",FILTER_SANITIZE_STRING)); $year = trim(filter_input(INPUT_POST,"year",FILTER_SANITIZE_STRING)); $details = trim(filter_input(INPUT_POST,"details",FILTER_SANITIZE_SPECIAL_CHARS));

if ($name == "" || $email == "" || $category == "" || $title == "") {
    $error_message = "Please fill in the required fields: Name, Email, Category and Title";
}
if (!isset($error_message) && $_POST["address"] != "") {
    $error_message = "Bad form input";
}

require("inc/phpmailer/class.phpmailer.php");

$mail = new PHPMailer;

if (!isset($error_message) && !$mail->ValidateAddress($email)) {
    $error_message = "Invalid Email Address";
}

if (!isset($error_message)) {
    $email_body = "";
    $email_body .= "Name " . $name . "\n";
    $email_body .= "Email " . $email . "\n";
    $email_body .= "Suggested Item\n";
    $email_body .= "Category " . $category . "\n";
    $email_body .= "Title " . $title . "\n";
    $email_body .= "Format " . $format . "\n";
    $email_body .= "Genre " . $genre . "\n";
    $email_body .= "Year " . $year . "\n";
    $email_body .= "Details " . $details . "\n";

    $mail->setFrom($email, $name);
    $mail->addAddress('treehouse@localhost', 'Alena');     // Add a recipient

    $mail->isHTML(false);                                  // Set email format to HTML

    $mail->Subject = 'Personal Media Library Suggestion from ' . $name;
    $mail->Body    = $email_body;

    if($mail->send()) {
        header("location:suggest.php?status=thanks");
        exit;
    }
    $error_message = 'Message could not be sent.';
    $error_message .= 'Mailer Error: ' . $mail->ErrorInfo;
}

}

$pageTitle = "Suggest a Media Item"; $section = "suggest";

include("inc/header.php"); ?>

<div class="section page"> <div class="wrapper"> <h1>Suggest a Media Item</h1> <?php if (isset($_GET["status"]) && $_GET["status"] == "thanks") { echo "<p>Thanks for the email! I’ll check out your suggestion shortly!</p>"; } else { if (isset($error_message)) { echo "<p class='message'>".$error_message . "</p>"; } else { echo "<p>If you think there is something I’m missing, let me know! Complete the form to send me an email.</p>"; } ?> <form method="post" action="suggest.php"> <table> <tr> <th><label for="name">Name (required)</label></th> <td><input type="text" id="name" name="name" value="<?php if (isset($name)) { echo $name; } ?>" /></td> </tr> <tr> <th><label for="email">Email (required)</label></th> <td><input type="text" id="email" name="email" value="<?php if (isset($email)) { echo $email; } ?>" /></td> </tr> <tr> <th><label for="category">Category (required)</label></th> <td><select id="category" name="category"> <option value="">Select One</option> <option value="Books"<?php if (isset($category) && $category == "Books") { echo " selected"; } ?>>Book</option> <option value="Movies"<?php if (isset($category) && $category == "Movies") { echo " selected"; } ?>>Movie</option> <option value="Music"<?php if (isset($category) && $category == "Music") { echo " selected"; } ?>>Music</option> </select></td> </tr> <tr> <th><label for="title">Title (required)</label></th> <td><input type="text" id="title" name="title" value="<?php if (isset($title)) { echo $title; } ?>" /></td> </tr> <tr> <th> <label for="format">Format</label> </th> <td> <select name="format" id="format"> <option value="">Select One</option> <optgroup label="Books"> <option value="Audio"<?php if (isset($format) && $format=="Audio") { echo " selected"; } ?>>Audio</option> <option value="Ebook"<?php if (isset($format) && $format=="Ebook") { echo " selected"; } ?>>Ebook</option> <option value="Hardcover"<?php if (isset($format) && $format=="Hardcover") { echo " selected"; } ?>>Hardcover</option> <option value="Paperback"<?php if (isset($format) && $format=="Paperback") { echo " selected"; } ?>>Paperback</option> </optgroup> <optgroup label="Movies"> <option value="Blu-ray"<?php if (isset($format) && $format=="Blu-ray") { echo " selected"; } ?>>Blu-ray</option> <option value="DVD"<?php if (isset($format) && $format=="DVD") { echo " selected"; } ?>>DVD</option> <option value="Streaming"<?php if (isset($format) && $format=="Streaming") { echo " selected"; } ?>>Streaming</option> <option value="VHS"<?php if (isset($format) && $format=="VHS") { echo " selected"; } ?>>VHS</option> </optgroup> <optgroup label="Music"> <option value="Cassette"<?php if (isset($format) && $format=="Cassette") { echo " selected"; } ?>>Cassette</option> <option value="CD"<?php if (isset($format) && $format=="CD") { echo " selected"; } ?>>CD</option> <option value="MP3"<?php if (isset($format) && $format=="MP3") { echo " selected"; } ?>>MP3</option> <option value="Vinyl"<?php if (isset($format) && $format=="Vinyl") { echo " selected"; } ?>>Vinyl</option> </optgroup> </select> </td> </tr> <tr> <th> <label for="genre">Genre</label> </th> <td> <select name="genre" id="genre"> <option value="">Select One</option> <optgroup label="Books"> <option value="Action"<?php if (isset($genre) && $genre=="Action") { echo " selected"; } ?>>Action</option> <option value="Adventure"<?php if (isset($genre) && $genre=="Adventure") { echo " selected"; } ?>>Adventure</option> <option value="Comedy"<?php if (isset($genre) && $genre=="Comedy") { echo " selected"; } ?>>Comedy</option> <option value="Fantasy"<?php if (isset($genre) && $genre=="Fantasy") { echo " selected"; } ?>>Fantasy</option> <option value="Historical"<?php if (isset($genre) && $genre=="Historical") { echo " selected"; } ?>>Historical</option> <option value="Historical Fiction"<?php if (isset($genre) && $genre=="Historical Fiction") { echo " selected"; } ?>>Historical Fiction</option> <option value="Horror"<?php if (isset($genre) && $genre=="Horror") { echo " selected"; } ?>>Horror</option> <option value="Magical Realism"<?php if (isset($genre) && $genre=="Magical Realism") { echo " selected"; } ?>>Magical Realism</option> <option value="Mystery"<?php if (isset($genre) && $genre=="Mystery") { echo " selected"; } ?>>Mystery</option> <option value="Paranoid"<?php if (isset($genre) && $genre=="Paranoid") { echo " selected"; } ?>>Paranoid</option> <option value="Philosophical"<?php if (isset($genre) && $genre=="Philosophical") { echo " selected"; } ?>>Philosophical</option> <option value="Political"<?php if (isset($genre) && $genre=="Political") { echo " selected"; } ?>>Political</option> <option value="Romance"<?php if (isset($genre) && $genre=="Romance") { echo " selected"; } ?>>Romance</option> <option value="Saga"<?php if (isset($genre) && $genre=="Saga") { echo " selected"; } ?>>Saga</option> <option value="Satire"<?php if (isset($genre) && $genre=="Satire") { echo " selected"; } ?>>Satire</option> <option value="Sci-Fi"<?php if (isset($genre) && $genre=="Sci-Fi") { echo " selected"; } ?>>Sci-Fi</option> <option value="Tech"<?php if (isset($genre) && $genre=="Tech") { echo " selected"; } ?>>Tech</option> <option value="Thriller"<?php if (isset($genre) && $genre=="Thriller") { echo " selected"; } ?>>Thriller</option> <option value="Urban"<?php if (isset($genre) && $genre=="Urban") { echo " selected"; } ?>>Urban</option> </optgroup> <optgroup label="Movies"> <option value="Action"<?php if (isset($genre) && $genre=="Action") { echo " selected"; } ?>>Action</option> <option value="Adventure"<?php if (isset($genre) && $genre=="Adventure") { echo " selected"; } ?>>Adventure</option> <option value="Animation"<?php if (isset($genre) && $genre=="Animation") { echo " selected"; } ?>>Animation</option> <option value="Biography"<?php if (isset($genre) && $genre=="Biography") { echo " selected"; } ?>>Biography</option> <option value="Comedy"<?php if (isset($genre) && $genre=="Comedy") { echo " selected"; } ?>>Comedy</option> <option value="Crime"<?php if (isset($genre) && $genre=="Crime") { echo " selected"; } ?>>Crime</option> <option value="Documentary"<?php if (isset($genre) && $genre=="Documentary") { echo " selected"; } ?>>Documentary</option> <option value="Drama"<?php if (isset($genre) && $genre=="Drama") { echo " selected"; } ?>>Drama</option> <option value="Family"<?php if (isset($genre) && $genre=="Family") { echo " selected"; } ?>>Family</option> <option value="Fantasy"<?php if (isset($genre) && $genre=="Fantasy") { echo " selected"; } ?>>Fantasy</option> <option value="Film-Noir"<?php if (isset($genre) && $genre=="Film-Noir") { echo " selected"; } ?>>Film-Noir</option> <option value="History"<?php if (isset($genre) && $genre=="History") { echo " selected"; } ?>>History</option> <option value="Horror"<?php if (isset($genre) && $genre=="Horror") { echo " selected"; } ?>>Horror</option> <option value="Musical"<?php if (isset($genre) && $genre=="Musical") { echo " selected"; } ?>>Musical</option> <option value="Mystery"<?php if (isset($genre) && $genre=="Mystery") { echo " selected"; } ?>>Mystery</option> <option value="Romance"<?php if (isset($genre) && $genre=="Romance") { echo " selected"; } ?>>Romance</option> <option value="Sci-Fi"<?php if (isset($genre) && $genre=="Sci-Fi") { echo " selected"; } ?>>Sci-Fi</option> <option value="Sport"<?php if (isset($genre) && $genre=="Sport") { echo " selected"; } ?>>Sport</option> <option value="Thriller"<?php if (isset($genre) && $genre=="Thriller") { echo " selected"; } ?>>Thriller</option> <option value="War"<?php if (isset($genre) && $genre=="War") { echo " selected"; } ?>>War</option> <option value="Western"<?php if (isset($genre) && $genre=="Western") { echo " selected"; } ?>>Western</option> </optgroup> <optgroup label="Music"> <option value="Alternative"<?php if (isset($genre) && $genre=="Alternative") { echo " selected"; } ?>>Alternative</option> <option value="Blues"<?php if (isset($genre) && $genre=="Blues") { echo " selected"; } ?>>Blues</option> <option value="Classical"<?php if (isset($genre) && $genre=="Classical") { echo " selected"; } ?>>Classical</option> <option value="Country"<?php if (isset($genre) && $genre=="Country") { echo " selected"; } ?>>Country</option> <option value="Dance"<?php if (isset($genre) && $genre=="Dance") { echo " selected"; } ?>>Dance</option> <option value="Easy Listening"<?php if (isset($genre) && $genre=="Easy Listening") { echo " selected"; } ?>>Easy Listening</option> <option value="Electronic"<?php if (isset($genre) && $genre=="Electronic") { echo " selected"; } ?>>Electronic</option> <option value="Folk"<?php if (isset($genre) && $genre=="Folk") { echo " selected"; } ?>>Folk</option> <option value="Hip Hop/Rap"<?php if (isset($genre) && $genre=="Hip Hop/Rap") { echo " selected"; } ?>>Hip Hop/Rap</option> <option value="Inspirational/Gospel"<?php if (isset($genre) && $genre=="Inspirational/Gospel") { echo " selected"; } ?>>Insirational/Gospel</option> <option value="Jazz"<?php if (isset($genre) && $genre=="Jazz") { echo " selected"; } ?>>Jazz</option> <option value="Latin"<?php if (isset($genre) && $genre=="Latin") { echo " selected"; } ?>>Latin</option> <option value="New Age"<?php if (isset($genre) && $genre=="New Age") { echo " selected"; } ?>>New Age</option> <option value="Opera"<?php if (isset($genre) && $genre=="Opera") { echo " selected"; } ?>>Opera</option> <option value="Pop"<?php if (isset($genre) && $genre=="Pop") { echo " selected"; } ?>>Pop</option> <option value="R&B/Soul"<?php if (isset($genre) && $genre=="R&B/Soul") { echo " selected"; } ?>>R&B/Soul</option> <option value="Reggae"<?php if (isset($genre) && $genre=="Reggae") { echo " selected"; } ?>>Reggae</option> <option value="Rock"<?php if (isset($genre) && $genre=="Rock") { echo " selected"; } ?>>Rock</option> </optgroup> </select> </td> </tr> <tr> <th><label for="year">Year</label></th> <td><input type="text" id="year" name="year" value="<?php if (isset($year)) { echo $year; } ?>" /></td> </tr> <tr> <th><label for="name">Suggest Item Details</label></th> <td><textarea name="details" id="details"><?php if (isset($details)) { echo htmlspecialchars($_POST["details"]); } ?></textarea></td> </tr> <tr style="display:none"> <th><label for="address">Address</label></th> <td><input type="text" id="address" name="address" /> <p>Please leave this field blank</p></td> </tr> </table> <input type="submit" value="Send" /> </form> <?php } ?> </div> </div>

<?php include("inc/footer.php"); ?>

1 Answer

I'm not really sure I know what your question(s) is, but if you feel like you are not understanding the material presented in a lesson I would recommend that you research what you are not understanding and then practice it on your own. It will take awhile to grasp certain concepts, but if you practice what you are taught you will eventually get the hang of it.