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

I Need Help With A Personal Project

Hi guys,

I've started working on a project and am getting stuck with how to write a particular function so would like some help please :-)

My project is a website which communicates with a database which holds booking information for a Courtesy Bus service.

My website is taking booking information and displaying it in the various pick up times for the day.

Where I am getting stuck is I'm trying to filter my booking data to only show bookings which are for a particular date, rather than every booking being displayed regardless of date as it currently does.

No matter how I seem to write my get_bookings() function I just can't seem to get the date variable to work.

I've uploaded the source files to my Github, which can be viewed here:- https://github.com/Donatron/CourtesyBus

I'm really stuck on this so any help would be greatly appreciated.

Thanks in advance Don :-)

Algirdas Lalys
Algirdas Lalys
9,389 Points

Hi Don,

Do you have like a test .sql database file. I can't find it or atleast could you provide "shema" of database.

Hi Algirdas,

Thanks for looking at it for me.

All the database information should be in the "courtesy_bus" folder.

This is the folder I uploaded from my "db" folder in my local MAMP directory. Is that all you would need? Or is there something else I am unaware of which would help?

Thanks again Don

Algirdas Lalys
Algirdas Lalys
9,389 Points

Hello Don,

I am not sure if I can somehow create DB from that folder I have never encountered that kind of aproach so I'm not sure about it. But If you have MAMP then i guees maybe you have tryed using "phpmyadmin" if not, it's not difficult to run it you just have to open MAMP, start Apache and MySQL and then go in the browser to "localhost/phpmyadmin", then on the left you can see all your databases. And what actually I need is just select your database on left menu and than at the top you can see many tables/buttons and one of them is "Export" where you can actually export your DB to .sql file. That is the file which I'm missing. Ofcourse I can create DB from examing your php files, but that could take some time and I could make a mistake:)

Hi Algirdas,

Thanks for clearing that all up. I wasn't sure if the data you need was in either of the files/directory I uploaded.

At any rate I've downloaded the database to .sql format and uploaded it to the branch master of my Github repository.

Thanks again for your help. Don :-)

Algirdas, you absolute legend! That's exactly what I was after.

Thanks again for all your help Don :-)

Algirdas Lalys
Algirdas Lalys
9,389 Points

Awesome! I'm glad it worked out:)

2 Answers

Algirdas Lalys
Algirdas Lalys
9,389 Points

Hi Don,

Great! I got .sql file and everything seems working. I looked through database and It looked a little bit confusing to me some tables (I guees I'm still not good enaugh with them:) But I managed to do so filterings on the dates. I guees this kind of fuctionallity you been trying to achieve. That when you pressed one of menu buttons, then only that date records should show up. Ok I started from looking at get_bookings() function and I created a new one which will filer down records.

<?php
// file: functions.php

// Added a new function with parameter $date
// Which will return associative arrays filtered by date
function get_bookings_by_date($date) {

  include 'connection.php';

  try {
      // Query which will filter by date
      $query = 'SELECT * FROM bookings WHERE date = :date';
      $stmt = $db->prepare($query);
      // Assign $date parameter to SQL Query
      $stmt->bindParam(':date', $date);
      // Execute it
      $stmt->execute();

      // return values as associative array ($key => $value)
      return $stmt->fetchAll(PDO::FETCH_ASSOC);
  } catch (Exception $e) {
      echo "Error!: " . $e->getMessage() . "</br>";
      return array();
  }
}
?>

After writing this function I modified header.php where those links/buttons are, I have added a $_GET['date'] variable.

<?php
<nav class="main-nav">
      <ul>
        <!-- Added to GET variable date a value of appropaite date, when you press one of the links/buttons it assigns $_GET['date'] variable with appropiate date 
              appropiate date -->
        <li><a href="index.php?date=<?php echo date('Y-m-d', strtotime($bookingDate)); ?>" tabindex="-1"><?php echo date('D d/m/y', strtotime($bookingDate)) ?></a></li>
        <li><a href="index.php?date=<?php echo date('Y-m-d', strtotime($dayPlus1)); ?>" tabindex="-1"><?php echo date('D d/m/y', strtotime($dayPlus1)) ?></a></li>
        <li><a href="index.php?date=<?php echo date('Y-m-d', strtotime($dayPlus2)); ?>" tabindex="-1"><?php echo date('D d/m/y', strtotime($dayPlus2)) ?></a></li>
        <li><a href="index.php?date=<?php echo date('Y-m-d', strtotime($dayPlus3)); ?>" tabindex="-1"><?php echo date('D d/m/y', strtotime($dayPlus3)) ?></a></li>
        <li><a href="index.php?date=<?php echo date('Y-m-d', strtotime($dayPlus4)); ?>" tabindex="-1"><?php echo date('D d/m/y', strtotime($dayPlus4)) ?></a></li>
        <li><a href="index.php?date=<?php echo date('Y-m-d', strtotime($dayPlus5)); ?>" tabindex="-1"><?php echo date('D d/m/y', strtotime($dayPlus5)) ?></a></li>
        <li><a href="index.php?date=<?php echo date('Y-m-d', strtotime($dayPlus6)); ?>" tabindex="-1"><?php echo date('D d/m/y', strtotime($dayPlus6)) ?></a></li>
        <li><a href="index.php?date=<?php echo date('Y-m-d', strtotime($dayPlus7)); ?>" tabindex="-1"><?php echo date('D d/m/y', strtotime($dayPlus7)) ?></a></li>
        <li><a href="" tabindex="-1">Future Date</a></li>
        <li><a href="" tabindex="-1">Permanent Booking</a></li>
      </ul>
    </nav>
?>

And finally I have changed your get_bookings function in foreach loop with get_bookings_by_date() function which accepts parameter date, which in this case is $_GET['date'] variable.

<?php
// file: index.php
// Returns assiociative array filter down by date
    foreach(get_bookings_by_date($_GET['date']) as $booking) {

      if ($booking['time'] == $time['time']) {
      ........
?>

And added a little security at the top of the index.php for $_GET['date'] variable if it's empty when page starts.

<?php

require 'inc/functions.php';
include 'inc/header.php';

// file: index.php
// When index.php loads GET variable date is empty, so it sets it to todays date
if(!isset($_GET['date']))  $_GET['date'] = date("Y-m-d");
?>

Ohh and don't forget to add new records in your database when testing:) I'm not sure if this is the functionality you were trying to create. But let me know how did it worked or not worked:)

Hi again,

Thanks so much for looking into this for me. I've read through your code and see where I was going wrong.

Unfortunately it's my "weekend" at the moment and no coding is the rule so I won't get to it until Friday morning my time. I'll let you know how I get on and if there's any problems with it.

Thanks again Don :-)