Range

I’m playing about with a bit of code to select dates from and to

[php]

<?php foreach (range($dayFrom, $dayTo) as $day) { foreach (range($monthFrom, $monthTo) as $month) { foreach (range($yearFrom, $yearTo) as $year) { ?> [/php]

in the tag I have

[php ]

<?php echo $day, $month, $year; ?>
<?php
	}
}

}
?>
[/php ]

I also have a form with 6 list menu’s named as above, I won’t add the code because its to long.

When I run the script with dates 02 - 01 - 2006 to 15 - 01 - 2006 I get the following

212006
312006
412006
512006
612006
712006
812006
912006
1012006
1112006
1212006
1312006
1412006
1512006

What I want to get is

02-01-2006
03-01-2006
04-01-2006
05-01-2006
06-01-2006
07-01-2006
08-01-2006
09-01-2006
10-01-2006
11-01-2006
12-01-2006
13-01-2006
14-01-2006
15-01-2006

Anyone got an idea how I can do this

take a look at printf()
http://php.net/printf
exact description at
http://php.net/sprintf

I now have the range working

[php]

foreach (range($dayFrom, $dayTo) as $day) {
foreach (range($monthFrom, $monthTo) as $month) {
foreach (range($yearFrom, $yearTo) as $year) {
$isodate = sprintf("%04d-%02d-%02d", $year, $month, $day);

[/php]

I am trying to insert the information into a database

[php]
mysql_query(“INSERT INTO temp (times, booking)
VALUES (’$isodate’, ‘$textfield’)”);
[/php]

The problem is its only inserting the beginning date not the array, Can anybody help!!

Depends how you want the data stored?
All dates in a single field? or Each date has its own row?

All dates in a one. Look into explode() or implode() one turns a string into a array and one turns an array into a string. Using a delimiter.

All dates in their own, you can run a simple for loop and run the query over and over again. Just do a count on the array. Then use myarray[$i].

Hope that helps.

I am getting a bit confused

I have the code

[php]

<?php $con = mysql_connect("localhost","dbuser","dbpassword"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("dnname", $con); foreach (range($dayFrom, $dayTo) as $day) { foreach (range($monthFrom, $monthTo) as $month) { foreach (range($yearFrom, $yearTo) as $year) { $isodate = sprintf("%04d-%02d-%02d", $year, $month, $day); ?> <?php ?>
<?php }}} ?>
From

Day 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Month January February March April May June July August September October November December Year 2006 2007 2008 2009 2010 2011 2012 2013

To

Day 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Month January February March April May June July August September October November December Year 2006 2007 2008 2009 2010 2011 2012 2013

[/php]

do I keep the sprintf statement which when echoed in the for gives me an array of dates or to I replace it with a loop statement.

I am currently trawling through google and php.net looking for the answer but in the meantime if anyone could give me a few pointers it would be appreciated

Sorted it in a fashion

[php]

<?php $Booking = explode(";","$isodate"); $Count = count($Booking); for ($i=0; $i < $Count; $i++) { mysql_query("INSERT INTO temp (times) VALUES ('$Booking[$i]')") or die(mysql_error()); } } } } ?>

[/php]

Trouble is it is duplicationg entries, how can I stop this or what is the function called. I tried making the entry unique in my sql cpanel but its returning an error

At this point, I am unaware of any function that would do this.

One way I would accomplish this would be to create a second array from the first…

[php]<?php
for($i = 0; $i < count($myarray); $i++)
{
if(!in_array($myarray[$i], $newarray))
{
array_push($newarray, $myarray[$i]);
}
else
{
}
}
?>[/php]

Functions used:
in_array() - Checks if a certain element is an array.
array_push() - Pushes one or more elements on the end of an array.

Then use the new array as the one to add to the database.

HTH!

I’ve been adding some dates to the database. I have just started to get this error

Duplicate entry ‘127’ for key 1

I deleted the field in mysqladmin and re added it, as soon as the count gets to 127 it throws the error up and wont progress.

Does anyone have a suggestion

Sorted it

A signed tinyint field can hold 128 different values (0 -> 127). Use an alter table command and make your auto-increment value larger (smallint or int).

Is is possible to instead of using list menus to use a single text field and get a date range from that

of cause, but it is better to give the user a list to select a date from then to tell him in what format he has to enter the date.

Sponsor our Newsletter | Privacy Policy | Terms of Service