Help needed

I have a mysql database with a 2 column table. date from and date to

I am trying to pull the information in an array

[php]
$strDateFrom = $row_test[‘datefrom’]; // date(‘Y-m-d’, mktime(0 ,0 , 0, $monthFrom, $dayFrom, $yearFrom));
$strDateTo = $row_test[‘dateto’]; // date(‘Y-m-d’, mktime(0 , 0, 0, $monthTo, $dayTo, $yearTo));

function createDateRangeArray($strDateFrom,$strDateTo) {
// takes two dates formatted as YYYY-MM-DD and creates an
// inclusive array of the dates between the from and to dates.

// could test validity of dates here but I’m already doing
// that in the main script

$aryRange=array();

$iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2), substr($strDateFrom,8,2),substr($strDateFrom,0,4));
$iDateTo=mktime(1,0,0,substr($strDateTo,5,2), substr($strDateTo,8,2),substr($strDateTo,0,4));

if ($iDateTo>=$iDateFrom) {
array_push($aryRange,date(‘Y-m-d’,$iDateFrom)); // first entry

while ($iDateFrom<$iDateTo) { 
  $iDateFrom+=86400; // add 24 hours 
  array_push($aryRange,date('Y-m-d',$iDateFrom)); 
} 

}
return $aryRange;
}
[/php]

It is working, sort of. Its only pulling the first line and turning it into an array.

I have tried a repeat region but nothing, its still only showing the first line

[php]
do {
$aryDates=createDateRangeArray($strDateFrom,$strDateTo);
$dates=$aryDates;
} while ($row_test = mysql_fetch_assoc($test));
[/php]

Can anybody help

finaly i found something:

shouldnt it be:
[php]do {
$strDateFrom = $row_test[‘datefrom’];
$strDateTo = $row_test[‘dateto’];
$aryDates=createDateRangeArray($strDateFrom,$strDateTo);
$dates[]=$aryDates;
} while ($row_test = mysql_fetch_assoc($test));[/php]

or
[php]do {
$aryDates=createDateRangeArray($row_test[‘datefrom’],$row_test[‘dateto’]);
$dates[]=$aryDates;
} while ($row_test = mysql_fetch_assoc($test));[/php]

No I have tried them along with foreach and range statements. Here is the code I have that brings the first db entry up

[php]
$strDateFrom = $row_test[‘from’]; // date(‘Y-m-d’, mktime(0 ,0 , 0, $monthFrom, $dayFrom, $yearFrom));
$strDateTo = $row_test[‘to’]; // date(‘Y-m-d’, mktime(0 , 0, 0, $monthTo, $dayTo, $yearTo));

function createDateRangeArray($strDateFrom,$strDateTo) {
// takes two dates formatted as YYYY-MM-DD and creates an
// inclusive array of the dates between the from and to dates.

// could test validity of dates here but I’m already doing
// that in the main script

$aryRange=array();

$iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2), substr($strDateFrom,8,2),substr($strDateFrom,0,4));
$iDateTo=mktime(1,0,0,substr($strDateTo,5,2), substr($strDateTo,8,2),substr($strDateTo,0,4));

if ($iDateTo>=$iDateFrom) {
array_push($aryRange,date(‘Y-m-d’,$iDateFrom)); // first entry

while ($iDateFrom<$iDateTo) { 
  $iDateFrom+=86400; // add 24 hours 
  array_push($aryRange,date('Y-m-d',$iDateFrom)); 
} 

}
return $aryRange;
}

$aryDates=createDateRangeArray($strDateFrom,$strDateTo);
$dates = implode(’, ',$aryDates);
echo $dates; [/php]

there is no mysql_fetch_array()-loop.

so there is just the first entry. u gonna need a loop. and the one u posted never passed the new row to ur function.

I’ve tried it and it returned nothing. I echoed it and it returned array

use print_r instead of echo

and then to make it look nice a foreach loop.

I’ve tried

[php]$testing = implode(mysql_fetch_array($test));
echo $testing;

$testing = print_r(mysql_fetch_array($test));
echo $testing;[/php]

Its not even echoing anything

print_r() instead of echo
not instead of implode

the echo u said it’s echoing Array. if u exchange that print_r its showing u the content of the array instead of the word Array.

I didnt try them both together, they’re just 2 examples I have tried

I’ve even tried

[php]
foreach (range($row_test[‘from’], $row_test[‘to’]) as $testing) {
echo $testing;
}
[/php]

I am now getting somewhere

[php]

$query_test = “SELECT * FROM test”;
$test = mysql_query($query_test, $connect) or die(mysql_error());
while($row = mysql_fetch_array($test, MYSQL_ASSOC))
{
$strDateFrom = $row[‘from’]; // date(‘Y-m-d’, mktime(0 ,0 , 0, $monthFrom, $dayFrom, $yearFrom));
$strDateTo = $row[‘to’]; // date(‘Y-m-d’, mktime(0 , 0, 0, $monthTo, $dayTo, $yearTo));

echo ‘’.$strDateFrom.’’.$strDateTo;
}

[/php]

from and start date are echoing with the { } but how do I get it to work outside. If i move them down i get

Fatal error: Cannot redeclare createdaterangearray() (previously declared in /data/members/paid/s/u/mydomain.co.uk/htdocs/www/test.php:13) in /data/members/paid/s/u/mydomain.co.uk/htdocs/www/test.php on line 13

When I have

[php]

$strDateFrom = array(); // date(‘Y-m-d’, mktime(0 ,0 , 0, $monthFrom, $dayFrom, $yearFrom));
$strDateTo = array();

while($row = mysql_fetch_array($test, MYSQL_ASSOC))
{
$strDateFrom = $row[‘from’]; // date(‘Y-m-d’, mktime(0 ,0 , 0, $monthFrom, $dayFrom, $yearFrom));
$strDateTo = $row[‘to’]; // date(‘Y-m-d’, mktime(0 , 0, 0, $monthTo, $dayTo, $yearTo));

echo ‘’.$strDateFrom.’-’.$strDateTo.’
’;
}

[/php]

All dates echo

When I have

[php]

while($row = mysql_fetch_array($test, MYSQL_ASSOC))
{
$strDateFrom = $row[‘from’]; // date(‘Y-m-d’, mktime(0 ,0 , 0, $monthFrom, $dayFrom, $yearFrom));
$strDateTo = $row[‘to’]; // date(‘Y-m-d’, mktime(0 , 0, 0, $monthTo, $dayTo, $yearTo));
}
echo ‘’.$strDateFrom.’-’.$strDateTo.’
’;

[/php]

1 date echoes… WHY!!

because echo is just called once (outside the loop). in ur last example the last one should be echoed case inside the loop $strDateFrom and $strDateTo are overwritten over and over again, having only the last assignt value after the loop is done.

about the Fatal error: Cannot redeclare createdaterangearray() (previously declared in /data/members/paid/s/u/mydomain.co.uk/htdocs/www/test.php:13) in /data/members/paid/s/u/mydomain.co.uk/htdocs/www/test.php on line 13
i gess u hab the function definition inside the loop. u may call the function inside the loop, but if u difiene it inside it’s redefined (and thats not posible in php) over an ofer again.

I think I have got everything a bit muddled up

I can call the array by

[php]

mysql_select_db($database_connect, $connect);
$query_test = “SELECT * FROM test”;
$test = mysql_query($query_test, $connect) or die(mysql_error());
$row = mysql_fetch_array($test) or die(mysql_error());
while($row = mysql_fetch_array($test)){
echo $row[‘from’]. " - ". $row[‘to’];
echo “
”;
}

[/php]

What I need to do is, instead of echoing the array as above I need to set a values of $strDateFrom and $strDateTo so it fetches the array outside the parenthesis so it would appear something like this.

[php]

mysql_select_db($database_connect, $connect);
$query_test = “SELECT * FROM test”;
$test = mysql_query($query_test, $connect) or die(mysql_error());
$row = mysql_fetch_array($test) or die(mysql_error());
// *************************************************//
while($row = mysql_fetch_array($test)){
$strDateFrom = $row[‘from’];
$strDateTo = $row[‘to’];
}
// *************************************************//

// I then need to pass each rown in the array through this
// or something similar

// *************************************************//
function createDateRangeArray($strDateFrom,$strDateTo) {

$aryRange=array();

$iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2), substr($strDateFrom,8,2),substr($strDateFrom,0,4));
$iDateTo=mktime(1,0,0,substr($strDateTo,5,2), substr($strDateTo,8,2),substr($strDateTo,0,4));

if ($iDateTo>=$iDateFrom) {
array_push($aryRange,date(‘Y-m-d’,$iDateFrom)); // first entry

while ($iDateFrom<$iDateTo) { 
  $iDateFrom+=86400; // add 24 hours 
  array_push($aryRange,date('Y-m-d',$iDateFrom)); 
} 

}
return $aryRange;
}

[/php]

Is this possible or am I wasting my time!!

now that i’m gettin wat u want. i try to make i a little clearer to u:

ur funktion definition may be anywhere in that file. use ur own structure, but keep all definitions at the same place.

after defining it (if they are in the same file they may be at the end of the file as well) u need to use it inside a loop and echo the values inside a loop.

[php]
mysql_select_db($database_connect, $connect);
$query_test = “SELECT * FROM test”;
$test = mysql_query($query_test, $connect) or die(mysql_error());

while($row = mysql_fetch_array($test)){
$strDateFrom = $row[‘from’];
$strDateTo = $row[‘to’];
$dates=createDateRangeArray($strDateFrom,$strDateTo);
echo ‘

’.implode(’, ‘,$dates).’

’;
}

// *************************************************//
// function definisions
// *************************************************//

function createDateRangeArray($strDateFrom,$strDateTo) {

$aryRange=array();

$iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2), substr($strDateFrom,8,2),substr($strDateFrom,0,4));
$iDateTo=mktime(1,0,0,substr($strDateTo,5,2), substr($strDateTo,8,2),substr($strDateTo,0,4));

if ($iDateTo>=$iDateFrom) {
array_push($aryRange,date(‘Y-m-d’,$iDateFrom)); // first entry

while ($iDateFrom<$iDateTo) {
  $iDateFrom+=86400; // add 24 hours
  array_push($aryRange,date('Y-m-d',$iDateFrom));
}

}
return $aryRange;
}[/php]

hope this is what u want

I’ll show you what the actual page looks like

With the closing parenthesis at end

http://www.sunseekerrentals.co.uk/availability_calendar2.php

With the closing parenthesis at begining

http://www.sunseekerrentals.co.uk/availability_calendar1.php

How it should look. This is from a table with 2 columns. Col 1 ‘id’ Col 2 every date in the array. This is taking up a hell of a lot of space hence the need for the change

http://www.sunseekerrentals.co.uk/availability_calendar.php

Hope this sheds a bit more light on my dilemma

can u tell me how that variable is called. and post a print_r() of the structure needed as it is used in the 3rd example.

or just post the code of the 3rd example again.

i think i now what u want, but not the variables used for the working example.

Above

[php]
$booking_month=array();
while($row=mysql_fetch_assoc($result))
{
$booking_month[]=$row[‘bookings’];
}
[/php]

In

[php]

<?php $d = date('d'); $m = date('m'); $y = date('Y'); for ($m=$m; $m <= 26;$m++) { ?>
<?php $start_day = date('d', mktime(0,0,0,$m,1,$y)); $end_day = date('t', mktime(0,0,0,$m,1,$y)); $weeks = date('w', mktime(0,0,0,$m,0,$y)); ?> <?php for ($i=0;$i<$weeks;$i++) { ?> <?php } for ($days=1;$days<=$end_day;$days++) { $check = mktime(0,0,0,$m,$days,$y); $date = date("Y-m-d", $check); $a_date = date("Y-m-d", $check - 86400); $d_date = date("Y-m-d", $check + 86400); if(in_array($date, $booking_month)){ $arrive = $depart = false; $vtd = ""; if (!in_array($a_date, $booking_month)) { $vtd = "background="styles/images/arrive.jpg""; $arrive = true; } if (!in_array($d_date, $booking_month)){ $vtd = "background="styles/images/depart.jpg""; $depart = true; } if (!$arrive && !$depart){ $vtd = "background="styles/images/booked.jpg""; } } else { $vtd = "background="styles/images/free.jpg""; }

?>
<td style=“font-size:10px; font-weight:normal” width=“20” height=“20” <?php echo $vtd ?> ><?php echo $days ?>
<?php if ($weeks == 6) { ?>

<?php $weeks = -1; } $weeks++; } ?>
<?php echo date('F Y', mktime(0,0,0,$m,1,$y)) ?>
Mo Tu We Th Fr Sa Su
<?php } ?>

[/php]

got it. u need array_merge() to combine multible arrays.
http://php.net/array_merge

[php]
mysql_select_db($database_connect, $connect);
$query_test = “SELECT * FROM test”;
$test = mysql_query($query_test, $connect) or die(mysql_error());

$booking_month = array();
while($row = mysql_fetch_array($test)){
$strDateFrom = $row[‘from’];
$strDateTo = $row[‘to’];
$booking_month = array_merge( $booking_month, createDateRangeArray($strDateFrom,$strDateTo) );
}

/* the rest may stay the same */

$d = date(‘d’);
$m = date(‘m’);
$y = date(‘Y’);
for ($m=$m; $m <= 26;$m++) {
[…]

// *************************************************//
// function definisions
// *************************************************//

function createDateRangeArray($strDateFrom,$strDateTo) {
[…]
[/php]

there is onather small thing u have to think about: daylightsaving time
[php]
$check = mktime(0,0,0,$m,$days,$y);
$date = date(“Y-m-d”, $check);
$a_date = date(“Y-m-d”, $check - 86400);
$d_date = date(“Y-m-d”, $check + 86400);
[/php]

one in a year the day has 25 and once 23 hours. just use 12 o clock to get the timestamp and there will be no prob:
[php]
$check = mktime(12,0,0,$m,$days,$y);
$date = date(“Y-m-d”, $check);
$a_date = date(“Y-m-d”, $check - 86400);
$d_date = date(“Y-m-d”, $check + 86400);
[/php]

Thanks for all your help, that seems to work a treat.

Half the time with PHP its just knowing what to look for and understanding the terminology. I would never have thought to look ar array_merge

Sponsor our Newsletter | Privacy Policy | Terms of Service