Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've had a similar problem to solve.<br/> <br/> First, you have to understand you're looking for a Not reserved car in a database containing your reserved cars...<br/> <br/> You're looking for information that IS NOT in you database!<br/> But you can deduct it from what you've got.<br/></p> <p>To simplify the comparison challenge, you should join your date and time (in your reservation table and in the two user criterias -start and end-) to a "timestamp" format... It will be easier to compare all your existing reservations to the user request.<br/> see: <a href="http://db.apache.org/derby/docs/10.7/ref/rrefsqlj27620.html" rel="nofollow">http://db.apache.org/derby/docs/10.7/ref/rrefsqlj27620.html</a> for format info.<br/> <br/></p> <p>So you have to look for your reserved cars using your user data (start and end timestamps) and filter them OUT of all your possible cars.<br/> <br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UserStart&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UserEnd<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br/> case A&nbsp;&nbsp;|------|&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br/> case B&nbsp;&nbsp;&nbsp;&nbsp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> <br/> You want cases A and E.<br/> <br/> This script should find out cases B,C,D and F... And place all the CarIDs into an array I called "NotAvaibleCars".<br/> It forgets about cases A and E (past/finished reservations and future reservations).<br/> <br/> Then, you have to check, for each cars, if its ID is "inArray" before to show it on your result page.<br/> <br/> Code:<br/></p> <pre><code>$allReservedCars = mysql_query("SELECT * from reservation") or die(mysql_error()); $allCars = mysql_query("SELECT * from cars") or die(mysql_error()); $NotAvailableCars = array(); $userStart = $_POST['yourClientStartCriteria']; $userEnd = $_POST['yourClientEndCriteria']; while ($allCarsCheck = mysql_fetch_array($allReservedCars)){ $TimestampStart = $allCarsCheck['startDate'] . ' ' . $allCarsCheck['startTime']; $TimestampEnd = $allCarsCheck['startDate'] . ' ' .$allCarsCheck['startTime']; if ( (($TimestampStart&gt;$userStart)&amp;&amp;($TimestampStart&lt;$userEnd)) || // if cases C or D (A reservation start already exist for this car between userStart and userEnd) (($TimestampEnd&gt;$userStart)&amp;&amp;($TimestampEnd&lt;$userEnd)) || // if case B or C (A reservation end already exist for this car between userStart and userEnd) (($TimestampStart&lt;$userStart)&amp;&amp;($TimestampEnd&gt;$userEnd)) // if case F (A reservation that wraps userStart and userEnd already exist for this car) ) { array_push($NotAvailableCars, $allCarsCheck['CarID']); // You have now an array of the NOT available cars. } } while($searchcars = mysql_fetch_array($allcars)) { if (inArray($searchcars['CarID'],$NotAvailableCars)){ //Do nothing! } else{ // Your code to show this available car. } } </code></pre> <p><br/></p> <p>;)<br/> <br/> <br/> Okay... I'm reading my solution again 8 hours later...<br/> And I see that I missed the main question: To show something when there is no car available for a make/model combinaison.<br/> <br/> I worked on it... And have a... Kind of a solution.<br/> But I hope there is a better one... This all I can see for now.<br/> <br/></p> <pre><code>$distinctMakeModel = mysql_query("SELECT distinct make, model from cars"); while ($row = mysql_fetch_array($distinctMakeModel)){ array_push($CarModelArray, $row['make'] . ' -splitter- ' . $row['model']) // Array with all make/model combination } $CarQtyArray = array(); for($i=0;$i&lt;$CarModelArray.length;$i++){ $thisCar = explode(' -splitter- ',$carModelArray[$i]); $ThisCarQty = mysql_query("SELECT count(*) from cars where make=$thisCar[0] AND model=$thisCar[1]"); $thiscar = mysql_fetch_array($ThisCarQty); array_push($CarQtyArray, $thiscar[0]; //So now you have an array of quantity with indexes matching the $carModelArray indexes. } </code></pre> <p>You now have an array of car make/model and an array of quantity with matching indexes.<br/> <br/> The next chunk of code will create an array of unavailable quantity with the same matching indexes.<br/></p> <pre><code>for($i=0;$i&lt;$CarModelArray.length;$i++){ $thisCar = explode(' -splitter- ',$carModelArray[$i]); $NotAvailableCarQtyArray[$i] = 0; for($j=0;$j&lt;$NotAvailableCars.length;$j++){ $thisNotAvailable = mysql_query("Select count(*) where make=$thisCar[$i] AND model=$thisCar[$i] AND carID=$NotAvailableCars[$j]"); $isThisTheModel = mysql_fetch_array($thisNotAvailable); if ($isThisTheModel&lt;&gt;'0'){ $NotAvailableCarQtyArray[$i] ++; } } } </code></pre> <p><br/> So now...<br/> in $CarModelArray[0], lets say you have Honda Civic.<br/> in $CarQtyArray[0], you have 3.<br/> in $NotAvailableCarQtyArray[0], you have 3 (all gone!).<br/> <br/></p> <pre><code>for ($i=0;$i&lt;$CarModelArray.length;$i++){ if ($CarQtyArray[$i]==$NotAvailableCarQtyArray[$i]){ echo $CarModelArray[$i] . ' are not available.&lt;br&gt;'; } } </code></pre> <p>This takes waaayyy to many queries here!!!<br/> It is not supposed to be a great idea to put a SQL query into a loop.<br/> But, again... I don't see how to do otherwise.<br/> <br/> It's like the amount of cars out between Start and End search multiplied by the amount of make/model you have.<br/> <br/> But it should work...<br/> At least I tried.<br/> ;)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload