Jump to content

PHP Learner

Members
  • Posts

    37
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

PHP Learner's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Yep it's flawed isn't. Ok back to the drawing board, so I know how to grab the date BETWEEN start and finsh dates in a mysql query, but how do you grab all dates from all the rows in one query and crank them out into an array which can be used to find the row it came from to gather the rest of the data?
  2. Yep your absolutely right Psycho. The numbers are actually dates (month and day) which I need for something. They exist in a row which has a start date and a finish date column (amongst others) and I have had to fill in the dates in between. I'm gonna post the code here, it's probably a bit shite but anyway. What I need to do is compare an inputted date with a value in the array and find the key which reffers to the row (rowNo) it came from. Not sure if that makes much sense to you or not, anywayz tha code. rowNo | startDate | finishDate | other columns 1 | 0301 | 0304 | stuff while($row = mysql_fetch_array($grabDates)) { $startDate = $row['startDate']; $finishDate = $row['finishDate']; $rowNo = $row['rowNo']; $arrayDate = $startDate; $break = ''; while($break !== 'something') { if ($break == 'break') {$arrayEntry .= $arrayDate;} else {$arrayEntry .= $arrayDate . ', ';} if ($break == 'break') {$break = ''; $arrayDate = ''; break;} if ($arrayDate == '1231') {$arrayDate = '0101';} else {$arrayDate++;} $countDateString = strlen($arrayDate); if ($countDateString == '3') {$arrayDate = '0' . $arrayDate;} if ($arrayDate == $finishDate) {$break = 'break';} } $numbers[] = array($arrayEntry); $arrayEntry = ''; } // End - while($row = mysql_fetch_array($grabDates)) { So I'm trying to build the array with key value pairs with a string seperated by commas in each key. Roughly something like this... Array [0] => (1234, 5678, 9012, 3456) [1] => (3456, 9012, 5678, 1234) [2] => (1234, 5678, 9012, 3456) etc...
  3. Hi all, This seems like it should be really simple but to me it's not. I have an array in a variable called $numbers. When I run print_r ($numbers) I get the following... $earchForNumber = '0304'; // Array called $numbers Array ( [0] => Array ( [0] => 0219, 0220, 0221, 0222, 0223 ) [1] => Array ( [0] => 0301, 0302, 0303, 0304 ) [2] => Array ( [0] => 1230, 1231, 0101, 0102 ) ) if (in_array($earchForNumber, $numbers)) {echo 'The key is' . $key;} What I need to do is search for a number within all of the sub arrays and find the key which it belongs to. So if I wanted to find the key for the value of $earchForNumber (0304) it would return a key of 1. There are tons of posts on stack overflow about this sort of thing but I can't find any that perform this task. I played around with inarray() but couldn't get any joy. Any ideas?
  4. I wrote the following code to create a set of 12 php calendars to display specific entries from a database. I have found many posts about converting a date into an integer but can find nothing to convert an integer into a date or more specifically in this case a month. The interesting thing about all of this is that it accepts and integer as a string in a concatinated set of values initially but won't accept the $month after the change at the end of the code when it returns to continue the loop. Can anyone tell me where I'm going wrong here? <?php $month = date("m"); // Initate $month to current month $twelveCount = '1'; // Set count to 1 for loop while($twelveCount != '13') { // Create loop condition to break at 13 producing 12 calendars $monthName = date("F", strtotime($month)); // Grab name of month from $month variable $year = date("Y"); // Initiate $year from current year $firstDate = $month . '/01/' . $year; // Calculate first day of the month, NOTE: It accepts the string integer here; $firstDay = date("D", strtotime($firstDate)); // Grab day of the first date $firstDay = strtoupper($firstDay); // Capitalise first day string $lastDay = cal_days_in_month(CAL_GREGORIAN, $month, $year); // Grab last day of the month $i = '1'; // Set integer to 1 for begining of auto incremental date display $lastDayPlusOne = $lastDay + 1; // Set limit for loop break when days are complete if ($firstDay == 'SUN') {$skipDays = '0';} // Skip spaces on calendar to equal actual first day if ($firstDay == 'MON') {$skipDays = '1';} if ($firstDay == 'TUE') {$skipDays = '2';} if ($firstDay == 'WED') {$skipDays = '3';} if ($firstDay == 'THU') {$skipDays = '4';} if ($firstDay == 'FRI') {$skipDays = '5';} if ($firstDay == 'SAT') {$skipDays = '6';} $count = '0'; // Iniate $count for loop break when first day is in the correct position $calendarDisplay .= '<div style="float:left; width:182px; margin:0px 0px 7px 7px;"> <div style="float:left; width:161px; text-align:left; margin-bottom:5px;">' . $monthName . ' ' . $year . '</div> <div style="float:left; width:23px; margin:0px 3px 3px 0px; background-color:#CCC; font-size:10px; text-align:center;">SUN</div> <div style="float:left; width:23px; margin:0px 3px 3px 0px; background-color:#CCC; font-size:10px; text-align:center;">MON</div> <div style="float:left; width:23px; margin:0px 3px 3px 0px; background-color:#CCC; font-size:10px; text-align:center;">TUE</div> <div style="float:left; width:23px; margin:0px 3px 3px 0px; background-color:#CCC; font-size:10px; text-align:center;">WED</div> <div style="float:left; width:23px; margin:0px 3px 3px 0px; background-color:#CCC; font-size:10px; text-align:center;">THU</div> <div style="float:left; width:23px; margin:0px 3px 3px 0px; background-color:#CCC; font-size:10px; text-align:center;">FRI</div> <div style="float:left; width:23px; margin:0px 3px 3px 0px; background-color:#CCC; font-size:10px; text-align:center;">SAT</div> <div style="float:left; width:182px;">'; // Create day names for header while($count != $skipDays) { // Output spaces to match day of the week before dates begin $calendarDisplay .= '<div style="float:left; width:23px; padding:4px 0; margin:0px 3px 3px 0px;"></div>'; $count++; } while($i != $lastDayPlusOne) { // Output dates inline with days in header $calendarDisplay .= '<div style="float:left; padding:4px 0; width:23px; margin:0px 3px 3px 0px; background-color:#0C0; font-size:10px; text-align:center;">' . $i . '</div>'; $i++; } $calendarDisplay .= '</div></div>'; // End calendar display // This is where it all goes wrong... if ($month == date("m", strtotime("1/10/10"))) {$month = date("m", strtotime("2/10/10")); echo $month;} if ($month == '02') {$month = date("m", strtotime("3/10/10")); echo $month;} if ($month == '03') {$month = '04'; echo $month;} /* if ($month == '04') {$month = date("m", strtotime("5/10/10")); echo $month;} if ($month == '05') {$month = date("m", strtotime("6/10/10")); echo $month;} if ($month == '06') {$month = date("m", strtotime("7/10/10")); echo $month;} if ($month == '07') {$month = date("m", strtotime("8/10/10")); echo $month;} if ($month == '08') {$month = date("m", strtotime("9/10/10")); echo $month;} if ($month == '09') {$month = date("m", strtotime("10/10/10")); echo $month;} if ($month == '10') {$month = date("m", strtotime("11/10/10")); echo $month;} if ($month == '11') {$month = date("m", strtotime("12/10/10")); echo $month;} if ($month == '12') {$month = date("m", strtotime("01/10/10")); echo $month;} */ $twelveCount++; // Increment $twelveCount by 1 } // End while($twelveCount != '13') { echo $calendarDisplay; // Echo result out to page ?>
  5. I want to thank you very much for your patients Barand, Im sorry if this is difficult to understand but the fact of the matter is that the description of the rooms will not need a table because it will be hard coded if you like into its own page. There simply is no need for a rooms table because these details are not the kind that change very often unless they are altering the premises in some way and if or when that is so, I'll cross that bridge. So it's just the room name/number we need which we will add to the table like so. ALTER TABLE `bookingscalendar` ADD `roomId` VARCHAR( 30 ) NOT NULL ; That said, the main focus is the search function for the bookingscalendar which after optimising the original table (with much gratitude!) is where we began. Cheers mate, php LEARNER
  6. Oh sorry, It's this code here isn't it? $db = new mysqli(HOST, USERNAME, PASSWORD, 'test'); $prefDate = '2013-06-24'; // preferred date $reqDays = 3; // days required $sql = "SELECT bookingdate FROM bookingscalendar WHERE status = 'available' AND bookingdate >= CURDATE() ORDER BY bookingdate"; $res = $db->query($sql); $dates = $earlier = $later = array(); $count = 0; $preferred = new DateTime($prefDate); $prevDay = new DateTime(); $prevDay->modify('-1 days'); /******************************** * search for N cosecutive dates *********************************/ while (list($bd) = $res->fetch_row()) { $dt = new DateTime($bd); if ($dt->diff($prevDay)->days==1 ) { $dates[] = $dt->format('Y-m-d'); } else { $dates = array($dt->format('Y-m-d')); } if (count($dates)>=$reqDays) { if ($dt->diff($preferred,0)->format('%R%a') > 1-$reqDays) { $earlier = array_slice($dates, -$reqDays); // last available N days prior } else { $later = array_slice($dates, -$reqDays); // next available N days break; } } $prevDay = clone $dt; } // output search results if ($later[0] != $prefDate) { echo "Prior dates: " . join(' | ', $earlier) . '<br>'; echo "Later dates: " . join(' | ', $later); } else { echo join(' | ', $later) . " is available"; }
  7. Hello mate, I think the room table isn't necessary at this stage, all the information for the types of rooms and facilities will be in each page. A prices table is a good suggestion I will be making the client a small CMS to change their requirements depending on fluctuations in their tariffs etc as you outlined above so thanks for that. The brass tacks of it all now really is the way we work with the bookingsCalendar table you tweaked this morning, care to print some logic Barand?
  8. Nice! That's got to be the most efficient calendar table there is. Well the invoice table is probably something I'll come back to refine a bit later on, for now if I'm right here it will serve it's purpose. It will be necessary to have the ability to check for a number of rooms at a time, I was going to use a different table for each room to achieve that. Now that you mention it tho, do you reckon we should add a room number/name column after autoInc or something? I'm really quite out of my depth as far as processing will go here, what would you suggest?
  9. Sorry screwed that up. Ok so above, I grab the dates from the sessions created when a calendar is used (date and nights submitted) and posted back to the bookings page for processing. So this is pretty much where my expertise runs dry lol. Question! 1) How do you tell mysql to search for dates that don't already exist? So you want to tell it to... SELECT * FROM bookingsCalendar WHERE bookingDate='$QueryDate' But how do you say and include the next 7 days in mySQL talk? Question! Next step I suppose if we assume that the dates have been booked on say 01/06/2013 for 7 days, 2) How do we search for the next available block of 7 days despite the possibility of other blocks of dates existing in the table. The search has to find previous dates as well as future dates on the table. Really its a bid to aid the customer from loosing the will to live lol, but in all seriousness is highly practical and could make the difference between a conversion and nothing at all. What do you think B?
  10. <?php session_start(); if (!isset($_SESSION['CalendarDate'])) {$_SESSION['CalendarDate'] = date("d/m/Y");} $NumberOfNights = $_SESSION['NumberOfNights']; if ($NumberOfNights == '7 Nights') {$Selected_1 = 'selected="selected"';} if ($NumberOfNights == '8 Nights') {$Selected_2 = 'selected="selected"';} if ($NumberOfNights == '9 Nights') {$Selected_3 = 'selected="selected"';} if ($NumberOfNights == '10 Nights') {$Selected_4 = 'selected="selected"';} if ($NumberOfNights == '11 Nights') {$Selected_5 = 'selected="selected"';} if ($NumberOfNights == '12 Nights') {$Selected_6 = 'selected="selected"';} if ($NumberOfNights == '13 Nights') {$Selected_7 = 'selected="selected"';} if ($NumberOfNights == '14 Nights') {$Selected_8 = 'selected="selected"';} if ($NumberOfNights == '15 Nights') {$Selected_9 = 'selected="selected"';} if ($NumberOfNights == '16 Nights') {$Selected_10 = 'selected="selected"';} if ($NumberOfNights == '17 Nights') {$Selected_11 = 'selected="selected"';} if ($NumberOfNights == '18 Nights') {$Selected_12 = 'selected="selected"';} if ($NumberOfNights == '19 Nights') {$Selected_13 = 'selected="selected"';} if ($NumberOfNights == '20 Nights') {$Selected_14 = 'selected="selected"';} if ($NumberOfNights == '21 Nights') {$Selected_15 = 'selected="selected"';} if (isset($_POST['SubmitCalendar'])) { $CalendarDate = $_POST['CalendarField']; $_SESSION['CalendarDate'] = $_POST['CalendarField']; $NumberOfNights = $_POST['NumberOfNights']; $_SESSION['NumberOfNights'] = $NumberOfNights; if ($NumberOfNights == '7 Nights') {$Selected_1 = 'selected="selected"'; $NightsForQuery = 7;} if ($NumberOfNights == '8 Nights') {$Selected_2 = 'selected="selected"'; $NightsForQuery = 8;} if ($NumberOfNights == '9 Nights') {$Selected_3 = 'selected="selected"'; $NightsForQuery = 9;} if ($NumberOfNights == '10 Nights') {$Selected_4 = 'selected="selected"'; $NightsForQuery = 10;} if ($NumberOfNights == '11 Nights') {$Selected_5 = 'selected="selected"'; $NightsForQuery = 11;} if ($NumberOfNights == '12 Nights') {$Selected_6 = 'selected="selected"'; $NightsForQuery = 12;} if ($NumberOfNights == '13 Nights') {$Selected_7 = 'selected="selected"'; $NightsForQuery = 13;} if ($NumberOfNights == '14 Nights') {$Selected_8 = 'selected="selected"'; $NightsForQuery = 14;} if ($NumberOfNights == '15 Nights') {$Selected_9 = 'selected="selected"'; $NightsForQuery = 15;} if ($NumberOfNights == '16 Nights') {$Selected_10 = 'selected="selected"'; $NightsForQuery = 16;} if ($NumberOfNights == '17 Nights') {$Selected_11 = 'selected="selected"'; $NightsForQuery = 17;} if ($NumberOfNights == '18 Nights') {$Selected_12 = 'selected="selected"'; $NightsForQuery = 18;} if ($NumberOfNights == '19 Nights') {$Selected_13 = 'selected="selected"'; $NightsForQuery = 19;} if ($NumberOfNights == '20 Nights') {$Selected_14 = 'selected="selected"'; $NightsForQuery = 20;} if ($NumberOfNights == '21 Nights') {$Selected_15 = 'selected="selected"'; $NightsForQuery = 21;} $CalendarDate = str_replace("/", "-", "$CalendarDate"); $QueryDate = date("Y-m-d", strtotime($CalendarDate)); include_once('../connect/connect.php'); //$bookingsCalendar = mysql_query("INSERT INTO bookingsCalendar (bookingDate, status, invoiceId) VALUES ('now()', 'booked', 'invoiceId')"); } // End - if (isset($_POST['SubmitCalendar'])) {
  11. My word! lol, Guru is certainly apt, many thanks for your efforts Barand. So if a fresh new approach is what the doctor ordered thats what we will do. In this case, by the look of it, I'll make 3 tables to spread out or 'normalize' the data, and your right making entries only when is necessary will save an awful lot of clutter. CREATE TABLE `bookingsCalendar` ( `autoIncId` int(10) NOT NULL AUTO_INCREMENT, `bookingDate` date NOT NULL, `status` enum('available','booked') NOT NULL, `invoiceId` varchar(30) NOT NULL, PRIMARY KEY (`autoIncId`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; CREATE TABLE `invoice` ( `invoiceId` varchar(30) NOT NULL, `invoiceRef` varchar(30) NOT NULL, `invoiceDate` date NOT NULL, `custId` varchar(30) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; CREATE TABLE `customer` ( `custId` varchar(30) NOT NULL, `firstName` varchar(20) NOT NULL, `lastName` varchar(20) NOT NULL, `email` varchar(45) NOT NULL, `phoneNo` tinyint(4) NOT NULL, `address` varchar(255) NOT NULL, `county` varchar(50) NOT NULL, `country` varchar(30) NOT NULL, `postcode` varchar(10) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; Would you say these values are appropriate? So the next step is to take a date from the calendar widget and amount of nights they want to stay (for testing sake best to keep it to 7 nights I think). I'll have to go away and write some sort of script for this now as best I can and I'll come back to post it in a bit, it will probably be clunky but I am a learner after all.
  12. Hmmm, looks like it might be a server error and not the script.
  13. Alright Barand, I would love to try out your script but I wonder if you can help me with populating the table first? See, the following scripts work with the older versions of phpmyadmin but yesterday I got a newer version and it's stopped working with a very generic error(500). Reckon you could cast your professional eye accross this code quickly and see if it can be made more efficient or see what needs doing to it? CREATE TABLE `table_setup` ( `number` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY (`number`) ) COLLATE='utf8_general_ci' ENGINE=MyISAM; INSERT INTO table_setup VALUES(NULL); INSERT INTO table_setup SELECT NULL FROM table_setup; INSERT INTO table_setup SELECT NULL FROM table_setup; INSERT INTO table_setup SELECT NULL FROM table_setup; INSERT INTO table_setup SELECT NULL FROM table_setup; INSERT INTO table_setup SELECT NULL FROM table_setup; INSERT INTO table_setup SELECT NULL FROM table_setup; INSERT INTO table_setup SELECT NULL FROM table_setup; INSERT INTO table_setup SELECT NULL FROM table_setup; INSERT INTO table_setup SELECT NULL FROM table_setup; INSERT INTO table_setup SELECT NULL FROM table_setup; INSERT INTO table_setup SELECT NULL FROM table_setup; INSERT INTO table_setup SELECT NULL FROM table_setup; INSERT INTO table_setup SELECT NULL FROM table_setup; INSERT INTO table_setup SELECT NULL FROM table_setup; INSERT INTO table_setup SELECT NULL FROM table_setup; INSERT INTO table_setup SELECT NULL FROM table_setup; INSERT INTO table_setup SELECT NULL FROM table_setup; DELETE FROM table_setup WHERE NUMBER > 18264; CREATE TABLE `BookingsCalendar` ( `AutoIncNo` SMALLINT(2) NOT NULL AUTO_INCREMENT, `BookingDate` DATE NOT NULL, `Status` varchar(10) NOT NULL DEFAULT 'available', `InvoiceRefference` varchar(40) NOT NULL, `CustomerName` varchar(40) NOT NULL, `CustomerEmail` varchar(40) NOT NULL, `CustomerPhone` varchar(30) NOT NULL, `Address` varchar(100) NOT NULL, `County` varchar(40) NOT NULL, `Country` varchar(30) NOT NULL, `PostCode` varchar(10) NOT NULL, PRIMARY KEY (`AutoIncNo`) ) COLLATE='utf8_general_ci' ENGINE=MyISAM; INSERT INTO BookingsCalendar (BookingDate) SELECT DATE_ADD('2013-05-05', INTERVAL number-1 DAY) FROM table_setup WHERE NUMBER < 18264; Gotta say, appreciate this very much. L-Plate
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.