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 Build a Basic PHP Website (2018) Listing and Sorting Inventory Items Displaying Categories

Daniel Cudney
PLUS
Daniel Cudney
Courses Plus Student 1,378 Points

Indexing Category in php file Undefined inde

I am trying to figure out where I should be indexing category in my php files and how.

: Undefined index: category in /home/treehouse/workspace/inc/functions.php on line 18

Matthew Brock
Matthew Brock
16,791 Points

Can you show us the code in question that you have?

2 Answers

Daniel Cudney
PLUS
Daniel Cudney
Courses Plus Student 1,378 Points

Thank you so much for your time.

I am learning and adapting to my own site.

booking.php main page loaded

<?php 
$pageTitle = "Booking Schedule";
$selection = "booking"; 
include("inc/data.php");
include("inc/functions.php");
include("inc/header.php");
        ?>


        <div class="section catalog page">
      <div class="wrapper">
        <h1><?php echo $pageTitle; ?></h1>
       <?php include("inc/shop.php"); ?>        
        </div>
      </div>
<?php include("inc/footer.php"); ?>

data.php

<?php
$catalog = [];
$catalog[100] = [ "title" => "60 Minute Massage",
                 "img" => "http://i1.wp.com/www.tranquilinc.com/wp-content/uploads/2015/08/ID-10038310.jpg?resize=150%2C150",
                 "duration" => "1 Hour",
                 "value" => "$100",
                 "cost" => "$50",
                 "description" => "Get a hour massage from Tranquil Embodiment Massage in San Diego, CA. Comes complete with hot towels, aromatherapy, and your time doesnt start until your massage does.",
                 "time" => 60];
$catalog[150] = [ "title" => "90 Minute Massage",
                 "img" => "img/90m.png",
                 "duration" => "1 Hour and a Half",
                 "value" => "$150",
                 "cost" => "$75",
                 "description" => "Get a ninety minute massage from Tranquil Embodiment Massage in San Diego, CA. Comes complete with hot towels, aromatherapy, and your time doesnt start until your massage does.",
                 "time" => 90];
$catalog[200] = [ "title" => "2 Hour Massage",
                 "img" => "img/120m.png",
                 "duration" => "1 Hour",
                 "value" => "$100",
                 "cost" => "$50",
                 "description" => "Get a two hour massage from Tranquil Embodiment Massage in San Diego, CA. Comes complete with hot towels, aromatherapy, and your time doesnt start until your massage does.",
                 "time" => 120, ];
if (isset($_GET["cat"])) {
if ($_GET["cat"] == "60") { $pageTitle = "60 Minute Massage"; $selection = "60"; } Else if ($_GET["cat"] == "90") { $pageTitle = "90 Minute Massage"; $selection = "90";  } Else if ($_GET["cat"] == "120") { $pageTitle = "Two Hour Massage"; $selection = "120";  }  }
?>

functions.php

<?php 
function get_item_html($id,$item) {
 $output =  "<li><a href='#'><img src='"
          . $item["img"]
          . "' alt='"
          . $item["title"] . " ' />"
          . "<br /><div class='crossOut'>" . $item["value"]
          . "</div><div class='hotSale'>" . $item["cost"] . "</div>"
          . "<p>View Details</p>"
          . "</a></li>"; 
return $output;

}

function array_category($catalog,$time) {
 $output = array();
  foreach ($catalog as $id => $item) {
                   if ($time == null OR strtolower($time) == strtolower($item["time"])) {
                     $sort = $item["title"];
                     $sort = ltrim($sort, "A ");                     
                     $sort = ltrim($sort, "The ");                     
                     $sort = ltrim($sort, "And ");
                   $output[$id] = $sort;
                   }  } 
  asort($output);
return array_keys ($output);} 


?>

shop.php

<h1><?php if ($selection != null) {
  echo "<a href='booking.php'>Full Catalog</a> &gt; "; }
  echo $pageTitle;
  ?></h1>


<ul class="">
                <li class="Sixty Minute Massage<?php if ($selection == "60") { echo " on"; } ?>"><a href="booking.php?cat=60">Home</a></li>
                <li class="Ninety Minute Massage<?php if ($selection == "90") { echo " on"; } ?>"><a href="booking.php?cat=90">First Time</a></li>
                <li class="Two Hour Minute Massage<?php if ($selection == "120") { echo " on"; } ?>"><a href="booking.php?cat=120">Massage Therapists</a></li>
            </ul>
<ul class="items"><?php $times = array_category($catalog, $selection);
foreach ($time as $id) {
          echo get_item_html($id,$catalog[$id]);
} ?></ul>