Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9223461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:05:18+00:00 2026-06-18T04:05:18+00:00

I’m having trouble how to implement a search functionality on a website I’m working

  • 0

I’m having trouble how to implement a search functionality on a website I’m working on.

The search form has two fields, starting date and an end date which a user wants to rent a vehicle.

In the database I have two tables, one for the vehicle and another one for available dates.

Table: cars
 - car_id (int)
 - name (varchar)
 - description (varchar)

Table: cars_availability
 - car_id (int)
 - date (datetime)
 - status (int) // 1: available 2: booked

Each row in cars_availability represent a day that the associated car is available for rent or booked.

Problem:

If user searches for a car that is available from 5.feb.2013 to 8.feb.2013 I would like to do a query to select all cars that are available for all the days in the users selection.

My current unfinished solution:

I know I can iterate through all the days using something like this

$begin = new \DateTime( date('Y-m-d', strtotime($data['rent_start'])));
$end = new \DateTime( date('Y-m-d', strtotime($data['rent_end'])));
$end = $end->modify( '+1 day' ); 

$interval = \DateInterval::createFromDateString('1 day');
$period = new \DatePeriod($begin, $interval, $end);

$available_cars = array();

foreach ( $period as $dayTime ){

    // query the cars and joining the cars_availabilty table

    $item = DB::select('cars.*')
    ->from('cars')
    ->join('cars_availability')->on('cars_availability.item_id', '=', 'cars.item_id')
    ->where('items_availability.date', '=', $dayTime->format("Y-m-d H:i:s"))
    ->and_where('items_availability.status', '=', 1)
    ->execute()->as_array();

    if(count($item) > 0){

      if(isset($available_cars[$item[0]['item_id']])){

        $available_cars[$item[0]['item_id']][] = $item[0];

      }else{

        $available_cars[$item[0]['item_id']] = array();
        $available_cars[$item[0]['item_id']][] = $item[0];
      }
    }

}

After this I would have an array of vehicles that might have all the days available but I would need to do some extra math to find that out.

I want know if there is some more elegant solutions to this problem, using a better query maybe.

If you can give me any feedback on this I would be very grateful.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:05:19+00:00Added an answer on June 18, 2026 at 4:05 am

    This is actually a fairly sane setup to query. Personally, I’d recommend following @inhan’s advice, especially if it turns out cars can be booked for smaller slices of time (hours?). However, the queries get a little bit more complicated to write, because of edge-cases (if you look around here, you should find relevant examples).

    Unfortunately, I can’t speak to how this fits into your framework, but the SQL can look like this:

    SELECT Cars.car_id, Cars.name, Cars.description
    FROM Cars
    JOIN (SELECT car_id
          FROM Cars_Availability
          WHERE `date` >= '2013-02-05'
                AND `date` < '2013-02-09'
                AND status = 1
          GROUP BY car_id
          HAVING COUNT(*) = DATEDIFF('2013-02-09', '2013-02-05')) Available
      ON Available.car_id = Cars.car_id
    

    (Have an SQL Fiddle Example)
    This query assumes that there’s only one entry per day, or this breaks. Basically, it grabs every ‘available’ row greater-than/equal the beginning date, and less than the ‘end’ date (date/time/timestamps are… complicated; this article deals with SQL Server-specific problems, but the basic concepts apply), and makes sure there’s one for the count of days between them. Mostly, you were looking for the HAVING clause.
    Given the proper index on Cars_Availability, it may not even hit the table, making this a good performer, too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am using JSon response to parse title,date content and thumbnail images and place
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.