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 trialLogan Fox
Courses Plus Student 3,379 Pointsinclude connection.php error
when I include connection.php into functions.php I get 2 notices and 2 warnings.
Notice: Use of undefined constant connection - assumed 'connection' in /home/treehouse/workspace/inc/functions.php on line 2
Notice: Use of undefined constant php - assumed 'php' in /home/treehouse/workspace/inc/functions.php on line 2
Warning: include(connectionphp): failed to open stream: No such file or directory in /home/treehouse/workspace/inc/functions.php on line 2
Warning: include(): Failed opening 'connectionphp' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /home/treehouse/workspace/inc/functions.php on line 2
<?php
include(connection.php);
/*
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);
}*/
<?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;
}
try {
$results = $db->query("SELECT title, category, img FROM Media");
} catch (Exception $e) {
echo "Unable to retrieve results";
exit;
}
$catalog = $results->fetchAll();
5 Answers
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsYou're missing quotes
<?php
include('connection.php');
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsYou forgot a $ before calling the function get_item_html()
<?php
echo $get_item_html($id,$catalog[$id]);
Logan Fox
Courses Plus Student 3,379 Pointsok! now I am getting this
Notice: Undefined variable: get_item_html in /home/treehouse/workspace/index.php on line 19
Fatal error: Uncaught Error: Function name must be a string in /home/treehouse/workspace/index.php:19 Stack trace: #0 {main} thrown in /home/treehouse/workspace/index.php on line 19
<ul class="items">
<?php
$random = array_rand($catalog,4);
foreach ($random as $id) {
echo $get_item_html($id,$catalog["$id"]);
}
?>
</ul>
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsWhy did you put quotes around $id?
<?php
echo $get_item_html($id,$catalog[$id]); // try this
Notice: Undefined variable: get_item_html in /home/treehouse/workspace/index.php on line 19 - did you remember to include the functions.php file?
Logan Fox
Courses Plus Student 3,379 PointsI put quotes around it because it said Function name must be a string. I have done what you said and it still says Notice: Undefined variable: get_item_html in /home/treehouse/workspace/index.php on line 19
Fatal error: Uncaught Error: Function name must be a string in /home/treehouse/workspace/index.php:19 Stack trace: #0 {main} thrown in /home/treehouse/workspace/index.php on line 19
<?php
$random = array_rand($catalog,4);
foreach ($random as $id) {
echo $get_item_html($id,$catalog[$id]);
}
?>
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsSorry, I made a mistake before:
<?php
echo get_item_html($id,$catalog[$id]); // remove the $ again
did you un-comment functions.php again?
<?php
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);
}
Logan Fox
Courses Plus Student 3,379 PointsThank you that was it!
Logan Fox
Courses Plus Student 3,379 PointsLogan Fox
Courses Plus Student 3,379 Pointsok so now the main page shows this Fatal error: Uncaught Error: Call to undefined function get_item_html() in /home/treehouse/workspace/index.php:19 Stack trace: #0 {main} thrown in /home/treehouse/workspace/index.php on line 19